使用 ImageMagick 压缩 PNG 图像
Compress a PNG image with ImageMagick
要压缩 JPEG 图片,我可以这样做:
$thumb = new Imagick();
$thumb->readImage("url");
$thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
$thumb->setImageCompressionQuality(80);
但是,我还需要压缩 PNG 图像(保持 alpha 透明度)以减小尺寸。有没有办法用 ImageMagick 做到这一点?
pngquant
有效地量化或减少图像中的颜色数量,直到质量出现明显下降之前。您可以像这样在 ImageMagick 中尝试类似的东西...
首先,使用 built-in rose:
图像,检查图像中的颜色数 - 它是 3,019:
convert rose: -format %k info:
3019
并制作一个 PNG
并检查大小 - 它是 6,975 字节
convert rose: rose.png
ls -l rose.png
-rw-r--r--@ 1 mark staff 6975 5 Sep 20:57 rose.png
现在将玫瑰色转换为 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
现在将玫瑰色转换为 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
另一种优化或减小 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!
使用 -set colorspace Gray
设置灰度不会减小 PNG 的文件大小,除非还使用了这些选项:
-define png:compression-level=9 -define png:format=8 -define png:color-type=0 -define png:bit-depth=8
这会生成具有最高 PNG 压缩率的 8 位灰度。添加这些选项将我的图像大小减小了 3 倍,因为现在它是一个通道(灰度),而之前是 3 个(RGB)。
要压缩 JPEG 图片,我可以这样做:
$thumb = new Imagick();
$thumb->readImage("url");
$thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
$thumb->setImageCompressionQuality(80);
但是,我还需要压缩 PNG 图像(保持 alpha 透明度)以减小尺寸。有没有办法用 ImageMagick 做到这一点?
pngquant
有效地量化或减少图像中的颜色数量,直到质量出现明显下降之前。您可以像这样在 ImageMagick 中尝试类似的东西...
首先,使用 built-in rose:
图像,检查图像中的颜色数 - 它是 3,019:
convert rose: -format %k info:
3019
并制作一个 PNG
并检查大小 - 它是 6,975 字节
convert rose: rose.png
ls -l rose.png
-rw-r--r--@ 1 mark staff 6975 5 Sep 20:57 rose.png
现在将玫瑰色转换为 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
现在将玫瑰色转换为 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
另一种优化或减小 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!
使用 -set colorspace Gray
设置灰度不会减小 PNG 的文件大小,除非还使用了这些选项:
-define png:compression-level=9 -define png:format=8 -define png:color-type=0 -define png:bit-depth=8
这会生成具有最高 PNG 压缩率的 8 位灰度。添加这些选项将我的图像大小减小了 3 倍,因为现在它是一个通道(灰度),而之前是 3 个(RGB)。