Wednesday 19 September 2018

PHP fatal libpng error: zlib error

While working with some image functions in PHP I changed the output image from JPEG to PNG (from the ImageJPEG to ImagePNG functions) and got the error message "gd-png: fatal libpng error: zlib error". This post looks at the cause of the error and the solution.
The full error message looks like this:
Fatal error: imagepng() [<a href='function.imagepng'>function.imagepng</a>]: 
  gd-png: fatal libpng error: zlib error in /path/to/filename.php on line 123
My original code looked like this:
ImageJPEG($image, 'filename.jpg, 100);
and I simply changed the function name and filename like this:
ImagePNG($image, 'filename.png, 100);
The third parameter is for the quality of the image. The images being generated are the highest quality possible which for JPEG images is a range from 0 (lowest quality, smallest file size) to 100 (highest quality, largest file size).
The reason for the error is that the quality range for the ImagePNG function is from 0 to 9 and anything outside that won't work. So simply change the value to between 0 and 9 and the error will go away. The resulting code looks like this, where there is no compression:
ImagePNG($image, 'filename.png, 0);
Note that 0 means no compression and 9 means the highest compression possible i.e. the scale is reversed when compared with JPEG images.

Related posts:

0 comments:

Post a Comment