1

I'm trying to write an Joomla plugin to add width and height tag to each <img> in HTML file. Some image file names are Persian, and getimagesize faces error.

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:

Warning: getimagesize(images\banners\س.jpg): failed to open stream: No such file or directory in C:\wamp64\www\plugin.php

How can I solve this?

Hossein
  • 21
  • 4
  • FWIW: [How do I use filesystem functions in PHP, using UTF-8 strings?](https://stackoverflow.com/questions/1525830/how-do-i-use-filesystem-functions-in-php-using-utf-8-strings) – ficuscr Sep 29 '20 at 20:59
  • Even using ```urlencode``` doesn't help: Warning: getimagesize(images\banners\%D8%B3.jpg): failed to open stream: No such file or directory in C:\wamp64\www\plugin.php – Hossein Sep 29 '20 at 21:06
  • Oh you are on WIndows? I should have picked up on that. Think you are subject to the codepage. Not sure this is even possible. PHP version? – ficuscr Sep 29 '20 at 21:19
  • Thank you @ficuscr Now I'm working on WAMP server, soon I will migrate to Linux server. WAMP server PHP is 5.6.31 – Hossein Sep 29 '20 at 21:21
  • Maybe the answer from @hersly here will help: https://stackoverflow.com/questions/6467501/php-how-to-create-unicode-filenames – ficuscr Sep 29 '20 at 21:22
  • PHP/5.6 hadn't fixed yet encoding issues on Windows file systems. The simplest fixes would be to either upgrade or avoid non-ASCII names altogether. – Álvaro González Sep 30 '20 at 07:35
  • Is this a DIRECTORY_SEPARATOR issue and the multibyte filename is a red herring? – mickmackusa Oct 05 '20 at 23:38

1 Answers1

0

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.


        $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.
  2. if some script (or whatever, div, p, ....) should be added to body tag, you can use appendChild method.
  3. <!DOCTYPE html> should be added to final DOM object output :)
Hossein
  • 21
  • 4