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
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.