setImageC...">

Compress PNG images using ImageMagick

To compress a JPEG image, I can do:

$thumb = new Imagick();
$thumb->readImage("url");
$thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
$thumb->setImageCompressionQuality(80);

However, I need to also compress PNG images (preserving alpha transparency) to reduce the size. Is there a way to do this with ImageMagick?

+4
source share
2 answers

pngquanteffectively quantizes or reduces the number of colors in the image until a noticeable drop in quality. You can try something similar in ImageMagick like this ...

First, using the embedded image rose:, check the number of colors in the image - this is 3.019:

convert rose: -format %k info:
3019

and make from it PNGand check the size - it's 6.975 bytes

convert rose: rose.png
ls -l rose.png
-rw-r--r--@ 1 mark  staff  6975  5 Sep 20:57 rose.png

enter image description here

255 - 3,691 :

convert rose: -colors 255 rose255.png
ls -l rose255.png
-rw-r--r--  1 mark  staff   3691  5 Sep 21:02 rose255.png

enter image description here

64 - 2,361 .

convert rose: -colors 64 rose64.png
ls -l rose64.png
-rw-r--r--  1 mark  staff  2361  5 Sep 21:04 rose64.png

enter image description here

PNG -strip - , , , .

, ... , , . , PNG, -alpha background.

convert -size 512x512 xc:gray +noise random a.png                                      # create an image of random noise
-rw-r--r--@ 1 mark  staff  1576107  6 Sep 11:37 a.png                                  # 157kB

convert -size 512x512 xc:gray +noise random -alpha transparent a.png                   # recreate but make transparent
-rw-r--r--@ 1 mark  staff  1793567  6 Sep 11:38 a.png                                  # 179kB, extra transparency channel

convert -size 512x512 xc:gray +noise random -alpha transparent -alpha background a.png # make all transparent pixels black
-rw-r--r--@ 1 mark  staff  1812  6 Sep 11:38 a.png                                     # Presto!
+3

-set colorspace Gray PNG, :

-define png:compression-level=9 -define png:format=8 -define png:color-type=0 -define png:bit-depth=8

8- PNG. 3 ×, ( ), 3 (RGB).

0

Source: https://habr.com/ru/post/1606057/


All Articles