简体   繁体   English

带有波斯文件名的php getimagesize

[英]php getimagesize with persian file name

I'm trying to write an Joomla plugin to add width and height tag to each <img> in HTML file.我正在尝试编写一个 Joomla 插件来为 HTML 文件中的每个<img>添加宽度和高度标签。 Some image file names are Persian, and getimagesize faces error.一些图像文件名是波斯语,并且 getimagesize 面临错误。

The code is this:代码是这样的:

   @$dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <img src="images\banners\س.jpg" style="max-width: 90%;" >
    </body>
    </html>
');

   $x = new DOMXPath($dom);

    foreach($x->query("//img") as $node)
    {   
        $imgtag = $node->getAttribute("src");
        
        $imgtag = pathinfo($imgtag);
        $imgtag = $imgtag['dirname'].'\\'.$imgtag['basename'];
        $imgtag = getimagesize($imgtag);
        
        $node->setAttribute("width",$imgtag[0]);
        $node->setAttribute("height",$imgtag[1]);
    }
    $newHtml = urldecode($dom->saveHtml($dom->documentElement));

And when Persian characters exist in file name, getimagesize shows:当文件名中存在波斯字符时,getimagesize 显示:

Warning: getimagesize(images\\banners\\س.jpg): failed to open stream: No such file or directory in C:\\wamp64\\www\\plugin.php警告:getimagesize(images\\banners\\س.jpg): 无法打开流:C:\\wamp64\\www\\plugin.php 中没有这样的文件或目录

How can I solve this?我该如何解决这个问题?

Thanks to all, I couldn't reach to results on WAMP server (local server on Windows), but when I migrated to Linux server, finally this code worked properly.感谢所有人,我无法在 WAMP 服务器(Windows 上的本地服务器)上获得结果,但是当我迁移到 Linux 服务器时,最终这段代码可以正常工作。


        $html = $app->getBody();

        setlocale(LC_ALL, '');
        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        $x = new DOMXPath($dom);

        foreach($x->query("//img") as $node)
        {   
                $imgtag = $node->getAttribute("src");
            if(strpos($imgtag,"data:image")===false)
            {

                $imgtag = getimagesize($imgtag);
                
                $node->setAttribute("width",$imgtag[0]);
                $node->setAttribute("height",$imgtag[1]);
            }
        }

        $bodytag = $x->query("//body");
        $node = $dom->createElement("script", ' /* java script which may be necessary on client */ '); 

        $bodytag[0]->appendChild($node);

        $html = '<!DOCTYPE html>'."\n" . $dom->saveHtml($dom->documentElement);

Some hints:一些提示:

  1. the code, shouldn't touch base64 image sources, so I added an condition to the code.代码不应该触及 base64 图像源,所以我在代码中添加了一个条件。
  2. if some script (or whatever, div, p, ....) should be added to body tag, you can use appendChild method.如果某些脚本(或其他脚本,div、p、....)应该添加到 body 标记中,您可以使用 appendChild 方法。
  3. <!DOCTYPE html> should be added to final DOM object output :) <!DOCTYPE html>应该添加到最终的 DOM 对象输出中:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM