我將文本中的影像鏈接替換為以下格式。
{#img='xxxx-xxxx-xxxx-xxxx.abc', alt=''}
在更改之前,我src從影像鏈接中獲取部分并在 CURL 的幫助下將其下載到服務器。我為下載的每個影像進行 UUID 命名。
到目前為止一切都很好!
$newImageName = create_uuid();
$ch = curl_init($img->getAttribute('src'));
$fp = fopen('/PATH_SAMPLE/' . $newImageName . '.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
然而; 每個影像都有不同的 UUID,但由于重新格式化,最終的 UUID 被渲染為文本;例如像這樣;
asdasd {#img='19a1cb87-009b-4495-be22-68fb08db8a76', alt=''} asdasd {#img='19a1cb87-009b-4495-be22-68fb08db8a76', alt=''}
所有代碼;
$jsonFile = "asdasd <img src='https://example.com/image_1.png'> asdasd <img src='https://example.com/image_1.jpg'>";
$dom = new DOMDocument;
$dom->loadHTML($jsonFile);
$imgs = $dom->getElementsByTagName('img');
$imgURLs = [];
foreach ($imgs as $img) {
if (!$img->hasAttribute('src')) {
continue;
} else {
$newImageName = create_uuid();
$ch = curl_init($img->getAttribute('src'));
$fp = fopen('/PATH_SAMPLE/' . $newImageName . '.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$str = preg_replace('/<img[^>]*src=([\'"])(.*?)\1>/', "{#img='" . $newImageName . "', alt=''}", $jsonFile);
}
}
如何解決影像的 UUID 問題?
uj5u.com熱心網友回復:
$newImageName = create_uuid().'.jpg';
假設ofc您可以將png檔案錯誤地命名為jpg。
順便說一句,如果你愿意的話,我可以建議而不是 uuid
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image_binary=curl_exec($ch);
$image_name=hash('sha224',$image_binary).'.jpg';
$image_path = '/PATH_SAMPLE/' . $image_name;
if(!file_exists($image_path)){
file_put_contents($image_path,$image_binary);
}
您將避免保存重復的影像,浪費磁盤空間。(如果您真的擔心記憶體使用情況,也可以使用 tmpfile() CURLOPT_FILE hash_file() rename(stream_get_meta_data($tmpfile)['uri']) 優化此選項以不將整個影像保存在 ram 中 ^ ^)
aaalso,切掉這個廢話
$str = preg_replace('/<img[^>]*src=([\'"])(.*?)\1>/', "{#img='" . $newImageName . "', alt=''}", $jsonFile);
只是做$img->setAttribute("src",$image_name);,當你準備好更新 $jsonFile 時,做$jsonFile=$dom->saveHTML();。
我個人可能會這樣寫
$jsonFile = "asdasd <img src='https://example.com/image_1.png'> asdasd <img src='https://example.com/image_1.jpg'>";
$dom = new DOMDocument ();
$rootName = "root" . bin2hex ( random_bytes ( 10 ) );
$dom->loadHTML ( "<?xml encoding=\"UTF-8\"><{$rootName}>{$jsonFile}</{$rootName}>" );
$imgs = $dom->getElementsByTagName ( 'img' );
$imgURLs = [ ];
$ch = curl_init ();
foreach ( $imgs as $img ) {
if (!$img->hasAttribute ( 'src' )) {
continue;
} else {
$tmphandle = tmpfile ();
$tmpfile = stream_get_meta_data ( $tmphandle ) ['uri'];
curl_setopt_array ( $ch, array (
CURLOPT_FILE => $tmphandle,
CURLOPT_URL => $img->getAttribute ( "src" )
) );
curl_exec ( $ch );
// optimization note: the file hashing could be done incrementally in-ram by using CURLOPT_WRITEFUNCTION hash_init() hash_update() instead of hash_file()
$image_name = hash_file ( 'sha224', $tmpfile ) . '.jpg';
$fp = '/PATH_SAMPLE/' . $image_name;
if (file_exists ( $fp )) {
// this is a duplicate image
} else {
if (PHP_OS_FAMILY === "Windows") {
// optimization note, on pretty much every OS except Windows, you can move files with open handles, but not on Windows..
// this is slower, but Windows-compatible
copy ( $tmpfile, $fp );
} else {
// this is faster, but not Windows-compatible
rename ( $tmpfile, $fp );
}
}
fclose ( $tmpfile );
$img->setAttribute ( "src", $image_name );
}
}
$str = $dom->saveHTML ( $dom->getElementsByTagName ( $rootName )->item ( 0 ) );
$str = substr ( $str, strlen ( "<{$rootName}>" ), -strlen ( "</{$rootName}>" ) );
curl_close ( $ch );
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453980.html
