正确匹配每个图像文件的名称 [PHP]

Matching the name of each image file correctly [PHP]

我把文中的图片链接替换成下面的格式

{#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=([\'"])(.*?)>/', "{#img='" . $newImageName . "', alt=''}", $jsonFile);
            
        }
    }

如何解决图片的 UUID 问题?

$newImageName = create_uuid().'.jpg';

假设您不介意将 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)[[=29 优化此选项以不将整个图像保存在 ram 中=]]) 如果你真的担心内存使用 ^^)

aa还有,cut this crap

$str = preg_replace('/<img[^>]*src=([\'"])(.*?)>/', "{#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 );