Best compression level for PNG

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Humanoid
Posts: 5
Joined: 2017-01-21T01:52:05-07:00
Authentication code: 1151
Location: The Netherlands

Best compression level for PNG

Post 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.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Best compression level for PNG

Post by glennrp »

Compression level 9 (like GIMP) is "-quality 90" (for no PNG filtering) or "-quality 95" (for adaptive PNG filtering).
Humanoid
Posts: 5
Joined: 2017-01-21T01:52:05-07:00
Authentication code: 1151
Location: The Netherlands

Re: Best compression level for PNG

Post 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.
Last edited by Humanoid on 2017-01-21T13:30:42-07:00, edited 1 time in total.
Humanoid
Posts: 5
Joined: 2017-01-21T01:52:05-07:00
Authentication code: 1151
Location: The Netherlands

Re: Best compression level for PNG

Post by Humanoid »

Maybe it is also important to note I'm getting these images first from layers inside a PSD-file?
Humanoid
Posts: 5
Joined: 2017-01-21T01:52:05-07:00
Authentication code: 1151
Location: The Netherlands

Re: Best compression level for PNG

Post 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");
  }
}
Humanoid
Posts: 5
Joined: 2017-01-21T01:52:05-07:00
Authentication code: 1151
Location: The Netherlands

Re: Best compression level for PNG

Post 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. :-)
Post Reply