在文章《使用TinyPNG来优化您的网站图片》介绍过TinyPNG提供的压缩服务,效果非常棒。ImgURL 图床也是使用TinyPNG来提供图片压缩,不过TinyPNG有免费次数限制。于是xiaoz寻找了Linux系统下的其它图片压缩方案,希望后期能够用到。
OptiPNG
OptiPNG是一种PNG优化器,可将图像文件重新压缩为更小的尺寸,而不会丢失任何信息。同时还可以压缩.bmp
.gif
等格式,安装方法如下:
#请先确保已经安装epel源,然后直接yum安装
yum -y install optipng
#也可以下载源码编译安装
#下载OptiPNG源码
wget -O optipng.tar.gz http://prdownloads.sourceforge.net/optipng/optipng-0.7.7.tar.gz?download
#解压
tar -zxvf optipng.tar.gz
#进入目录
cd optipng-0.7.7/
#编译安装
./configure
make && make install
如果不出意外,安装成功后可以输入optipng -V
查看当前版本,如下截图。
直接输入命令optipng xxx.png
来压缩一张.png
的图片,optipng 还有更多的参数,可以输入optipng -h
来查看帮助。
Synopsis:
optipng [options] files ...
Files:
Image files of type: PNG, BMP, GIF, PNM or TIFF
Basic options:
-?, -h, -help show this help
-o <level> optimization level (0-7) [default: 2]
-v run in verbose mode / show copyright and version info
General options:
-backup, -keep keep a backup of the modified files
-clobber overwrite existing files
-fix enable error recovery
-force enforce writing of a new output file
-preserve preserve file attributes if possible
-quiet, -silent run in quiet mode
-simulate run in simulation mode
-out <file> write output file to <file>
-dir <directory> write output file(s) to <directory>
-log <file> log messages to <file>
-- stop option switch parsing
Optimization options:
-f <filters> PNG delta filters (0-5) [default: 0,5]
-i <type> PNG interlace type (0-1)
-zc <levels> zlib compression levels (1-9) [default: 9]
-zm <levels> zlib memory levels (1-9) [default: 8]
-zs <strategies> zlib compression strategies (0-3) [default: 0-3]
-zw <size> zlib window size (256,512,1k,2k,4k,8k,16k,32k)
-full produce a full report on IDAT (might reduce speed)
-nb no bit depth reduction
-nc no color type reduction
-np no palette reduction
-nx no reductions
-nz no IDAT recoding
Editing options:
-snip cut one image out of multi-image or animation files
-strip <objects> strip metadata objects (e.g. "all")
Optimization levels:
-o0 <=> -o1 -nx -nz (0 or 1 trials)
-o1 <=> -zc9 -zm8 -zs0 -f0 (1 trial)
(or...) -zc9 -zm8 -zs1 -f5 (1 trial)
-o2 <=> -zc9 -zm8 -zs0-3 -f0,5 (8 trials)
-o3 <=> -zc9 -zm8-9 -zs0-3 -f0,5 (16 trials)
-o4 <=> -zc9 -zm8 -zs0-3 -f0-5 (24 trials)
-o5 <=> -zc9 -zm8-9 -zs0-3 -f0-5 (48 trials)
-o6 <=> -zc1-9 -zm8 -zs0-3 -f0-5 (120 trials)
-o7 <=> -zc1-9 -zm8-9 -zs0-3 -f0-5 (240 trials)
-o7 -zm1-9 <=> -zc1-9 -zm1-9 -zs0-3 -f0-5 (1080 trials)
Notes:
The combination for -o1 is chosen heuristically.
Exhaustive combinations such as "-o7 -zm1-9" are not generally recommended.
Examples:
optipng file.png (default speed)
optipng -o5 file.png (slow)
optipng -o7 file.png (very slow)
OptiPNG主页:http://optipng.sourceforge.net/
jpegoptim
jpegoptim是一款开源的图片压缩工具,只能压缩.jpg
格式,直接使用yum
命令安装,安装完毕后可以输入jpegoptim -V
查看版本。
yum -y install jpegoptim
输入命令jpegoptim -m 80 xxx.jpg
来压缩图片,-m
指的优化质量,可选值从0-100,数值越大速度越快、质量越好,但压缩效果更差,更多参数可以输入jpegoptim -h
查看。
jpegoptim v1.4.6 Copyright (C) 1996-2018, Timo Kokkonen
Usage: jpegoptim [options] <filenames>
-d<path>, --dest=<path>
specify alternative destination directory for
optimized files (default is to overwrite originals)
-f, --force force optimization
-h, --help display this help and exit
-m<quality>, --max=<quality>
set maximum image quality factor (disables lossless
optimization mode, which is by default on)
Valid quality values: 0 - 100
-n, --noaction don't really optimize files, just print results
-S<size>, --size=<size>
Try to optimize file to given size (disables lossless
optimization mode). Target size is specified either in
kilo bytes (1 - n) or as percentage (1% - 99%)
-T<threshold>, --threshold=<threshold>
keep old file if the gain is below a threshold (%)
-b, --csv print progress info in CSV format
-o, --overwrite overwrite target file even if it exists (meaningful
only when used with -d, --dest option)
-p, --preserve preserve file timestamps
-P, --preserve-perms
preserve original file permissions by overwriting it
-q, --quiet quiet mode
-t, --totals print totals after processing all files
-v, --verbose enable verbose mode (positively chatty)
-V, --version print program version
-s, --strip-all strip all markers from output file
--strip-none do not strip any markers
--strip-com strip Comment markers from output file
--strip-exif strip Exif markers from output file
--strip-iptc strip IPTC/Photoshop (APP13) markers from output file
--strip-icc strip ICC profile markers from output file
--strip-xmp strip XMP markers markers from output file
--all-normal force all output files to be non-progressive
--all-progressive force all output files to be progressive
--stdout send output to standard output (instead of a file)
--stdin read input from standard input (instead of a file)
jpegoptim项目地址:https://github.com/tjko/jpegoptim
Google webp
WebP是一种现代图像格式,可为Web上的图像提供出色的无损和有损压缩。使用WebP,网站管理员和Web开发人员可以创建更小,更丰富的图像,使网络更快。
CentOS可以使用yum
安装:
yum -y install libwebp-devel libwebp-tools
使用cwebp命令行上PNG或JPEG图像文件转换为WebP的格式。您可以使用以下命令将PNG图像文件转换为质量范围为80的WebP图像:
cwebp -q 80 image.png -o image.webp
更多关于webp的使用说明可以参考Google官方文档: new image format for the Web,顺带一提,国内的又拍云CDN 已经支持自动将图片转换为webp格式。
总结
无论哪种图片压缩方案都无法真正做到100%无损,只能尽可能不降低图片质量的情况下减小图片体积。建议大家对网站图片进行适当压缩,提高网站速度。如果您还有更优秀的压缩方案,请留言推荐。
可以试一试这个: https://www.picdiet.com/zh-cn
好的,谢谢推荐。