阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS系统下多种图片压缩方案

185次阅读
没有评论

共计 4491 个字符,预计需要花费 12 分钟才能阅读完成。




在文章《使用 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% 无损,只能尽可能不降低图片质量的情况下减小图片体积。建议大家对网站图片进行适当压缩,提高网站速度。如果您还有更优秀的压缩方案,请留言推荐。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计4491字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中