Multiple Image Compression Solutions on CentOS System

Publish: 2018-11-15 | Modify: 2018-11-15

In the article "Optimizing Your Website Images with TinyPNG", TinyPNG's compression service is introduced, which has excellent results. ImgURL also uses TinyPNG for image compression, but there are free usage limits. So Xiaoz searched for other image compression solutions for Linux systems for future use.

OptiPNG

OptiPNG is a PNG optimizer that recompresses image files to smaller sizes without losing any information. It can also compress formats such as .bmp and .gif. The installation method is as follows:

# Make sure that epel source is installed first, then install it directly with yum
yum -y install optipng
# Alternatively, you can download the source code and compile it
# Download OptiPNG source code
wget -O optipng.tar.gz http://prdownloads.sourceforge.net/optipng/optipng-0.7.7.tar.gz?download
# Unpack
tar -zxvf optipng.tar.gz
# Enter the directory
cd optipng-0.7.7/
# Compile and install
./configure
make && make install

If everything goes well, you can enter optipng -V to check the current version, as shown in the screenshot below.

You can compress a .png image by simply typing the command optipng xxx.png. OptiPNG has more parameters, which you can view by typing 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 homepage: http://optipng.sourceforge.net/

jpegoptim

jpegoptim is an open source image compression tool that can only compress .jpg format. It can be installed using the yum command. After installation, you can enter jpegoptim -V to check the version.

yum -y install jpegoptim

You can compress images by typing the command jpegoptim -m 80 xxx.jpg, where -m indicates the optimization quality. Valid quality values range from 0 to 100. A higher value means faster speed and better quality but worse compression. You can view more parameters by typing 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 project address: https://github.com/tjko/jpegoptim

Google WebP

WebP is a modern image format that provides excellent lossless and lossy compression for images on the web. With WebP, website administrators and web developers can create smaller, richer images that make the web faster.

On CentOS, you can use yum to install it:

yum -y install libwebp-devel libwebp-tools

Use the cwebp command line tool to convert PNG or JPEG image files to the WebP format. You can use the following command to convert a PNG image file to a WebP image with a quality range of 80:

cwebp -q 80 image.png -o image.webp

For more information on using webp, you can refer to the official Google documentation: new image format for the Web. By the way, Upyun CDN in China already supports automatically converting images to webp format.

Summary

No image compression solution can achieve 100% lossless compression. It can only reduce the size of the image as much as possible without reducing the quality. It is recommended to compress website images appropriately to improve website speed. If you have a better compression solution, please leave a comment to recommend it.


Comments