Czechnology
Czechnology

Reputation: 14992

PHP+Imagick - PNG Compression

How do I efficiently compress a PNG? In my case, the images are small grayscale images with transparency.

Currently I'm playing with this:

// ...

$im->setImageFormat('png');
$im->setImageColorspace(\Imagick::COLORSPACE_GRAY);
$im->setImageCompression(\Imagick::COMPRESSION_LZW);
$im->setImageCompressionQuality(9);
$im->stripImage();
$im->writeImage($url_t);

As Imagick doesn't offer COMPRESSION_PNG, I've tried LZW but there's almost no change in the filesize (usually it's even bigger than before).

If I open the image in GIMP and simply save it, the filesize gets drastically reduced (e.g. 11,341 B --> 3,763 B or 11,057 B --> 3,538).

What is the correct way of saving a compressed PNG with Imagick?

Upvotes: 8

Views: 11403

Answers (2)

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90203

Have a look at the first part of this answer:

It explains the meaning + syntax of ImageMagick's -quality setting for PNGs.

Upvotes: 3

Giedrius
Giedrius

Reputation: 1688

I'm definitely not sure if it is correct way to save PNG, but my way is:

$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);

This gives me perfect quality of the image and file size very similar to PS6 saved 'Save for Web'. Sometimes even smaller sizes!

Upvotes: 0

Related Questions