有时我们采集了目标站并下载了图,但是还会出现某些图片出于默写原因没有下载下来,但是你已经记录了图片地址,网页上却显示没有图片,我们这时如果一两条手动更新就搞定了,但是如果很多这样的信息怎么办?手动是不可能的,只能程序来查询还替换了!
今天小编搞了一下午,终于搞定了!下面时代码:
<?php
ini_set('max_execution_time', 0);
ini_set('memory_limit', '512M');
define('EmpireCMSAdmin', '1');
require("../class/connect.php");
require("../class/db_sql.php");
require("../class/functions.php");
require LoadLang("pub/fun.php");
require("../class/delpath.php");
require("../class/copypath.php");
require("../class/t_functions.php");
require("../data/dbcache/class.php");
require("../data/dbcache/MemberLevel.php");
$link = db_connect(); // Connect to MySQL
$empire = new mysqlquery(); // Declare database operation class
$batchSize = 50; // Number of records per page
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Get total record count
$totalQuery = $empire->query("SELECT COUNT(*) AS total FROM {$dbtbpre}ecms_news");
$totalResult = $empire->fetch($totalQuery);
$totalRows = $totalResult['total'];
$totalPages = ceil($totalRows / $batchSize);
$offset = ($page - 1) * $batchSize;
if ($page <= $totalPages) {
echo "<h1>Processing Page $page of $totalPages</h1>";
// 查询当前页面的数据
$sql = "SELECT id, titlepic, cjid, classid FROM {$dbtbpre}ecms_news LIMIT $offset, $batchSize";
$query = $empire->query($sql);
while ($row = $empire->fetch($query)) {
$id = $row['id'];
$titlepic = $row['titlepic'];
$cjid = $row['cjid'];
$classid = $row['classid'];
echo "处理记录ID: $id... ";
if (strpos($titlepic, '/d/file/p/') !== false) { //这里时查询图片地址是不是包含特定目录
$titlepic = '你的网址' . $titlepic; //因为我是没有设置附件域名的,所以要替换你的主域名记得后面不要加/
// 检查图片是否存在
if (!checkImage($titlepic)) {
$url = $cjid;
$html = getUrlContent($url);
if ($html !== false) { //下面是找源站的图片地址,根据自己的替换
if (preg_match('/var J_photo = \[(.*?)\];/s', $html, $matches)) {
$photos = json_decode("[" . $matches[1] . "]", true);
if (!empty($photos)) {
$newImage = $photos[0]['src'];
$newImage = preg_replace([
'/\?.*/',
'/^https?:\/\/((?!i3\.目标网址\.com)[^\/]+)/' //这里是替换图片地址,因为有些图片地址采用cdn的,有防盗链和加水印,我们直接替换成没有水印的图片域名
], [
'',
'https://i3.目标网址.com'
], $newImage);
$r = DoTranUrl($newImage, $classid);
if (!empty($r['tran'])) {
$r['filesize'] = (int)$r['filesize'];
$filepass = time();
// Resize image if larger than 100KB 这里是查询图片是不是超过100kb超过就按比例缩小图片
if ($r['filesize'] > 100 * 1024) {
$resizedPath = resizeImage($r['yname'], 500); //这里的500是缩小图片的宽度
if ($resizedPath) {
echo "<fong color='blue'>图片缩小成功!</font>";
}
}
$username = 'picurl';
$pubid=ReturnInfoPubid($classid,$id);
$pubid=RepPostVar($pubid);
$sql = eInsertFileTable($r['filename'], $r['filesize'], $r['filepath'], $username, $classid,$r['filename'], 1, $id, 0, $public_r['fpath'], $pubid, $modtype, $fstb); //这里是添加附件图片地址到图片附件库
$empire->query( "UPDATE {$dbtbpre}ecms_news SET titlepic = '" . $r['url'] . "' WHERE id = $id"); //这里是修改当前图片地址
echo "<fong color='red'>处理完毕。</font><br>";
} else {
echo "处理图像失败。<br>";
}
sleep(1);
}
}
}
} else {
echo "图像已经存在。<br>";
}
} else {
echo "无需采取任何行动。<br>";
}
}
// 在处理完数据后,输出一个隐藏的字段或JavaScript代码来设置自动跳转
echo "<script type='text/javascript'>";
echo "function redirectToNextPage() {";
echo "var currentPage = parseInt(window.location.search.substring(1).split('=')[1]);"; // 解析当前页码从URL参数中
echo "var totalPages = $totalPages;"; // 总页码数从PHP变量中获取,注意这里需要确保JavaScript能够正确解析这个数字
echo "if (currentPage < totalPages) {";
echo "window.location.href = '?page=' + (currentPage + 1);"; // 跳转到下一页
echo "} else {";
echo "alert('所有页面均已处理。');"; // 如果已经是最后一页,则显示提示信息
echo "}";
echo "}";
// 设置1秒后调用redirectToNextPage函数
echo "setTimeout(redirectToNextPage, 1000);"; // 1000毫秒等于1秒
echo "</script>";
echo "<a href='?page=" . ($page + 1) . "'>下一页</a>";
} else {
echo "<h1>所有页面均已处理。</h1>";
}
db_close();
$empire = null;
/**
* Check if an image exists
*
* @param string $url
* @return bool
*/
function checkImage($url)
{
$headers = @get_headers($url);
return $headers && strpos($headers[0], '404') === false;
}
/**
* Resize an image
*
* @param string $filePath
* @param int $newWidth
* @return string|false
*/
function resizeImage($filePath, $newWidth)
{
list($width, $height, $type) = getimagesize($filePath);
$newHeight = intval($height * $newWidth / $width);
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
switch ($type) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($filePath);
imagecopyresampled($imageResized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newPath = str_replace('.jpg', '_resized.jpg', $filePath);
imagejpeg($imageResized, $newPath);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($filePath);
imagecopyresampled($imageResized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newPath = str_replace('.png', '_resized.png', $filePath);
imagepng($imageResized, $newPath);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($filePath);
imagecopyresampled($imageResized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newPath = str_replace('.gif', '_resized.gif', $filePath);
imagegif($imageResized, $newPath);
break;
default:
return false;
}
return $newPath;
}
function getUrlContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果你访问的是HTTPS,并且证书有问题,可以设置为false
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
$error = curl_error($ch);
echo "cURL Error: $error";
return null;
}
return $data;
}