Page 1 of 1

Best compression level for PNG

Posted: 2017-01-21T02:16:36-07:00
by Humanoid
I'm using ImageMagick for a PHP-website. It works fantastic :-) But I can't seem to get PNG-compression working, even after a lot of tries with lots of different compression quality numbers.

This is what I'm using:
  • ImageMagick 6.9.6-4 Q16 amd64 (2016-12-19)
  • imagick 3.4.1
  • PHP 7.0.14
In GIMP when saving a PNG I can choose between compression level 0 (no compression) to 9 (best compression).

Can anybody help me on my way to use compression level '9' (like GIMP) using PHP's imagick?

I know there are 2 digits for specifying the compression level with setImageCompressionQuality. I've also read this on StackOverflow:
http://stackoverflow.com/a/26997997
(and other stuff)

But I can't seem to get it working.

Any help's much appreciated.

Re: Best compression level for PNG

Posted: 2017-01-21T08:50:28-07:00
by glennrp
Compression level 9 (like GIMP) is "-quality 90" (for no PNG filtering) or "-quality 95" (for adaptive PNG filtering).

Re: Best compression level for PNG

Posted: 2017-01-21T13:24:27-07:00
by Humanoid
Thanks! But for some reason no matter what quality setting I choose, the size remains exactly the same I've noticed. I must be doing something completely wrong?

Maybe I should be doing things in some other order. And if necessary, which value would I need to set for setImageCompression?

I'm doing something like this, in specified order:

Code: Select all

stripImage() // to strip metadata
setImageCompression(\Imagick::COMPRESSION_UNDEFINED) // also tried other values
setImageCompressionQuality(90) // also tried other values (and also tried setCompressionQuality instead)
setImageFormat('png')
scaleImage(...)
writeImage(...)
Update:
Also tried to put setImageFormat before the setImageCompression-line. Size still remains the same.

Re: Best compression level for PNG

Posted: 2017-01-21T13:29:13-07:00
by Humanoid
Maybe it is also important to note I'm getting these images first from layers inside a PSD-file?

Re: Best compression level for PNG

Posted: 2017-01-25T11:58:36-07:00
by Humanoid
Thanks glennrp.

Edit:
Corrected a mistake and ran the code again. The original PNG image is 203 KB. The smallest file with compression is 212 KB and the biggest file with compression is 319 KB.

Edit 2:
Sorry, original size seems to be 227 KB, so it *does* compress: . Sorry!

Code: Select all

<?php
$imagickSrc = new Imagick("test1.png");
$compressionList = [Imagick::COMPRESSION_UNDEFINED,
                    Imagick::COMPRESSION_BZIP,
                    Imagick::COMPRESSION_LZW,
                    Imagick::COMPRESSION_RLE,
                    Imagick::COMPRESSION_ZIP];

for ($s = 0; $s < count($compressionList); $s++) {
  for ($i = 0; $i < 100; $i++) {
    $imagickDst = new Imagick();
    $imagickDst->setCompression($compressionList[$s]);
    $imagickDst->setCompressionQuality($i);
    $imagickDst->newPseudoImage(
          $imagickSrc->getImageWidth(),
          $imagickSrc->getImageHeight(),
          'canvas:white'
    );

    $imagickDst->compositeImage(
        $imagickSrc,
        Imagick::COMPOSITE_ATOP,
        0,
        0
    );
    $imagickDst->setImageFormat("png");
    $imagickDst->writeImage("test1_compressed{$s}_{$i}.png");
  }
}

Re: Best compression level for PNG

Posted: 2017-01-25T12:18:34-07:00
by Humanoid
Anyway. Probably going to choose JPEG over PNG. Of course it has a trade-off but we can make it a reasonable one. :-)