Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error

How to fix these PHP errors: Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error and Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition

I was in the process of creating a Website that offers free desktop wallpapers. Everytime I designed a new wallpaper I had to resize it to about 10 different screen resolutions, which consumed a lot of time. So I thought about creating a PHP function that given a high resolution image, it would resize it to the 10 different screen resolutions and create all 10 PNG files with just one click. So I did. I will post a tutorial on how to do this later.

But I kept on getting this error:

Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /myfolders/resizer.php on line 162

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /myfolders/resizer.php on line 162

This is line 162:

imagepng($resizedImage, $uploadToFolder . strtolower($newFilename.'_'.$dimensions.$ext), 100);

I googled this error and browsed through several Websites until I found this explanation: From PHP 4 to PHP 5.. PHP Fatal zlib error: imagepng()

It said to “Fix it easily by changing your compression variable of imagepng, imagegif or imagejpg/imagejpeg into a 1-10 ranged number – instead of the PHP 4 1-100 standard”. I had entered a 100 as the quality because I had a function that worked with JPEGs and I wanted to resize PNG wallpapers, so I just changed all the jpeg functions to png functions. I did as the mentioned page instructed, changed line 162 to:

imagepng($resizedImage, $uploadToFolder . strtolower($newFilename.'_'.$dimensions.$ext), 10);

I still got the same error. Then I went to php.net to read imagepng documentation. On the Parameters section under quality (the 3rd paramenter) said:

Compression level: from 0 (no compression) to 9.

I edited line 162 once more:

imagepng($resizedImage, $uploadToFolder . strtolower($newFilename.'_'.$dimensions.$ext), 9);

It finally worked. If you read this article: From PHP 4 to PHP 5.. PHP Fatal zlib error: imagepng(), you will see that the 10 value for the quality parameter worked for some people. I don’t know why that is. Maybe it their PHP configuration or version. Maybe someone can comment on it by using the form below.

Tags: ,

Comments are closed.