2

Please tell me how can I check the file for its real type? When executing the imagecreatefrompng() function, it displays an error: is not a valid PNG file. mime_content_type() says image/png file. This file...

<?php
$file_name = $_SERVER['DOCUMENT_ROOT']."/walko.png";
$new_file_name = $_SERVER['DOCUMENT_ROOT']."/walko.webp";
$img = imagecreatefrompng($file_name);
    imagepalettetotruecolor($img);
    imagealphablending($img, true);
    imagesavealpha($img, true);
    imagewebp($img, $new_file_name);
    imagedestroy($img);
?>

PHP 7.4.9

Vector
  • 109
  • 5
  • Could you please add the code you are using to recreate the error? – ZeroWorks Apr 22 '21 at 18:16
  • @ZeroWorks Added code, please see. – Vector Apr 22 '21 at 18:20
  • When you `echo($new_file_name);` what is shown in output? you provided a file named `walko_(1)_(1).png` not `walko.png` that it's in your code... could you please tell us the realfile name? or give us more information? – ZeroWorks Apr 22 '21 at 18:39
  • @ZeroWorks Changed the name of the file. echo? What are you speaking about? The error goes on the first line, the file with the webp extension was not created ... – Vector Apr 22 '21 at 18:45
  • I only try to understand what is happening... could you try to use `$img=imagecreatefromstring(file_get_contents($new_file_name));` this should work with JPEG, PNG, GIF, WBMP y GD2 images. The file you provided as a link works perfectly with your code. – ZeroWorks Apr 22 '21 at 18:54
  • @ZeroWorks The variable `$new_file_name` contains only the name of the future webp file. Moreover, it is not created, it is empty, since the error occurs earlier. – Vector Apr 22 '21 at 18:55
  • Sorry I meant `$img=imagecreatefromstring(file_get_contents($file_name));`... – ZeroWorks Apr 22 '21 at 18:57
  • @ZeroWorks `Warning: imagecreatefromstring(): gd-png error: cannot allocate image data` – Vector Apr 22 '21 at 19:00
  • PNG image is too big. https://stackoverflow.com/questions/31844401/gd-png-cannot-allocate-image-data and https://libgd.github.io/pages/faq.html read the section: Why does gd cause my PHP script to run out of memory? incresase PHP memory limit or reduce file size and your code should work. – ZeroWorks Apr 22 '21 at 19:04
  • @ZeroWorks I am not using the GD library. But most likely the problem is due to the large png file. – Vector Apr 22 '21 at 19:18
  • @ZeroWorks Answer me, I will accept it. Thank you. – Vector Apr 22 '21 at 19:24

1 Answers1

1

It seems that your png file is too big, and PHP is unable to allocate enough memory.

When you run:

$img=imagecreatefromstring(file_get_contents($new_file_name));

You get more accurate error description:

Warning: imagecreatefromstring(): gd-png error: cannot allocate image data

Then it seems that gd is unable to allocate memory and as it's said in libgd FAQ:

Why does gd cause my PHP script to run out of memory? Most often, the problem is that the memory_limit parameter in php.ini is set to something very conservative, like 8M (eight megabytes). Increase that setting and restart the web server. Of course, opening truly huge images can cause real memory problems, if several are open at once. 8,000 pixels times 8,000 pixels times four bytes for truecolor equals a walloping 256 megabytes.

That seems the cause of your problem.

Also there's a related question here:

gd-png cannot allocate image data

So if you recude png size it should work also you can increase PHP memory limit memory_limit in PHP ini. In my case your initial script worked because I have a high value.

ZeroWorks
  • 1,618
  • 1
  • 18
  • 22
  • memory_limit in php.ini is 512000000 but that's another question. Thank you for helping me figure it out. – Vector Apr 23 '21 at 07:56