內容采集、編輯器粘貼外鏈圖片時,需將遠程圖片下載到本地服務器,並替換內容中的 src 地址,防止圖片丟失、盜鏈或跨域問題。
preg_match_all 正則提取 HTML 中所有 src="https://..." 的圖片 URL;Content-Type; $url,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 1,
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_REFERER => $ref ?: '',
CURLOPT_AUTOREFERER => !$ref,
CURLOPT_COOKIEJAR => $cookie_file,
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => $head ? 1 : 0,
CURLOPT_RETURNTRANSFER => 1,
]);
$data = curl_exec($curl);
$errno = curl_errno($curl);
$info = $head ? curl_getinfo($curl) : [];
curl_close($curl);
if ($errno) error_log("cURL Error: " . curl_error($curl));
return ['data' => $data, 'head' => $info];
}
/** 遞歸創建目錄 */
function MkdirAll($truepath) {
global $cfg_dir_purview;
if (!file_exists($truepath)) {
MkdirAll(dirname($truepath));
mkdir($truepath, $cfg_dir_purview);
chmod($truepath, $cfg_dir_purview);
}
return true;
}
/** 核心:遠程圖片本地化 */
function GetCurContent($body) {
global $cfg_basedir, $cfg_dir_purview;
$basehost = "https://" . $_SERVER["HTTP_HOST"];
$cfg_uploaddir = '/uploads';
$imgUrl = $cfg_uploaddir . '/' . date("ymd");
$imgPath = $cfg_basedir . $imgUrl;
if (!is_dir($imgPath)) MkdirAll($imgPath);
// 提取所有 https:// 開頭的 gif/jpg/png 圖片
preg_match_all('/src=["\'\s]*((https?:\/\/[^>"\']+\.(gif|jpg|png)))/isU', $body, $matches);
$img_array = array_unique($matches[1]);
foreach ($img_array as $key => $value) {
// 跳過本站圖片
if (strpos($value, $basehost) !== false) continue;
if (!preg_match('#^https?://#i', $value)) continue;
$http = pget($value, $value, true);
$ctype = $http['head']['content_type'] ?? '';
// 根據 Content-Type 確定副檔名
if (preg_match('#image/gif#i', $ctype)) $ext = '.gif';
elseif (preg_match('#image/png#i', $ctype)) $ext = '.png';
else $ext = '.jpg';
$randName = rand(1000,9999) . rand(1000,9999) . '-' . $key . $ext;
$rndFileName = $imgPath . '/' . $randName;
$fileurl = $imgUrl . '/' . $randName;
$fp = fopen($rndFileName, 'wb');
fwrite($fp, $http['data']);
fclose($fp);
if (file_exists($rndFileName)) {
$body = str_replace($value, $fileurl, $body);
}
}
return $body;
}
// 使用示例
$content = '
';
echo GetCurContent($content);
.*? 避免跨標簽匹配;CURLOPT_FOLLOWLOCATION 處理 302 跳轉;CURLOPT_SSL_VERIFYPEER=0 兼容 HTTPS 自簽名證書;data:image/...;base64,... 格式內聯圖片解析;