PHP fatal libpng error: zlib error
Posted November 4th, 2008 in PHP
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 like this:
ImagePNG($image, 'filename.jpg, 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.
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:
- Image headers with PHP (Tuesday, July 22nd 2008)
- Get an image size with PHP (Wednesday, July 9th 2008)
- PHP error Call to undefined function ImageCreateFromPNG (Wednesday, January 9th 2008)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

