Vector
Vector

Reputation: 560

Find out the php file type

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

Upvotes: 2

Views: 127

Answers (1)

ZeroWorks
ZeroWorks

Reputation: 1638

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.

Upvotes: 1

Related Questions