1

My simple goal was to compress PNG files with the following code:

$image = imagecreatefrompng("test.png");
imagepng($image, "result.png", 9, PNG_ALL_FILTERS);

I've also tried with zero and default compression, with the same results.

Original file is like this:

source image

But the result on some (not all) images was like this:

broken result

The only way, I can produce good results is with this additional code:

imagealphablending($image, false);
imagesavealpha($image, true);

Why does this simple image manipulation produce this kind of artefacts? And by saving full alpha information, do I get into troubles with some browsers or unneeded filesize?

Is there any other foolproof solution, which would losslessly compress png files?

zeldi
  • 4,833
  • 3
  • 19
  • 18
  • Maybe because you have set "9" as compression level which means the maximum compression, try to set 0 or 1 ? – weeger Jun 18 '14 at 17:35
  • @weeger: I've also tried 0 and without, so that can't be the problem – zeldi Jun 18 '14 at 18:13
  • 1
    @zeldi, You're using this in the correct manner. The reason for the strange transparency is that without `imagesavealpha` on 24-bit images the opaque pixels are resampled in single transparency mode. – l'L'l Jun 18 '14 at 19:40
  • @I'L'I: doesn't this info get read and set from source PNG file? Do I have to set it manually? – zeldi Jun 19 '14 at 11:14

0 Answers0