Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error when compressing PNG image – imagepng(): gd-png: fatal libpng error: bad parameters to zlib

I am getting an error in php when I try to compress my png images. Here is my code:

public function compress() {
   $filePath = "/path/image.png";
   $image = @imagecreatefrompng($filePath);
   if (!$image) return;
   
   // compress with 60% of quality
   imagepng($image, $filePath, 60);
}

Here is the error message:

imagepng(): gd-png: fatal libpng error: bad parameters to zlib

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am getting this error when I execute "imagepng" function. Why I am getting this error? thank you

>Solution :

The quality parameter of the imagepng() function only takes values from 0 – 9.

  • 0 = no compression
  • 9 = max compression.

Which means the function can’t use your input 60

Reference:

https://www.php.net/manual/en/function.imagepng says:

quality: Compression level: from 0 (no compression) to 9. The default (-1) uses the zlib compression default. For more information see the » zlib manual.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading