华岁呀Huasui

华岁呀Huasui

Use OptiPNG and Jpegoptim to batch compress and optimize website images.

Most blogs probably have images, and as the blog updates, there are more and more images. If you use a CDN or if the server has traffic restrictions, you need to consider how to save traffic, especially for images!

So it's better to optimize and compress the images if conditions permit. Although the compression ratio may not be very high, it can still improve loading speed. Quantity leads to quality improvement, right? Hi Here are two tools recommended for batch compressing images in Linux:

  • Jpegoptim - A small tool for lossless compression and optimization of JPEG files.
  • OptiPNG - A small program for optimizing and compressing PNG files without losing any information.

Installing Jpegoptim#

Download Jpegoptim

wget https://www.kokkonen.net/tjko/src/jpegoptim-1.5.1.tar.gz

Unzip

tar xzvf jpegoptim-1.5.1.tar.gz

Compile and install jpegoptim-1.5.1

cd jpegoptim-1.5.1
./configure
make
make strip
make install

Verify if Jpegoptim has been updated to the latest version

jpegoptim -V
jpeg-v

Installing OptiPNG#

Download

wget https://nchc.dl.sourceforge.net/project/optipng/OptiPNG/optipng-0.7.7/optipng-0.7.7.tar.gz

Unzip

tar xzvf optipng-0.7.7.tar.gz

Compile and install OptiPNG

cd optipng-0.7.7
./configure
make
make install

Verify OptiPNG version

optipng -V
optipng-v
OptiPNG and Jpegoptim can be used in conjunction with the find and xargs commands to automatically compress and optimize jpg and jpeg image files in the /www directory.

find /www -name "*.jpg" -o -name "*.jpeg" | xargs jpegoptim
```<br/>
Automatically compress and optimize PNG image files in the /www directory.<br/>

find /www -name "*.png" | xargs optipng


# Using Shell scripts to automatically compress images

[xiaoz](https://www.xiaoz.me/archives/13511) has written a shell script to call OptiPNG and jpegoptim tools to automatically compress images in batches.

> ## Supported image formats
> Currently supports .jpg/.jpeg/.bmp/.png format images
> 
> ## Implementation principle
> Use the find command to find images in the specified directory (while filtering by image size and time)<br/>
Call different compression tools based on different image formats<br/>
Use the crontab tool to automatically compress images in batches

Download the batch compression script#

wget https://raw.githubusercontent.com/helloxz/shell/master/img_compress.sh

Execute the shell script#

bash img_compress.sh /data/wwwroot/imgurl

Replace /data/wwwroot/imgurl with your own image directory (use absolute path)

## Using crontab for scheduled execution

Give the script execute permission first#

chmod +x img_compress.sh

Add a scheduled task#

crontab -e

Copy the content to the end and save (execute every hour)#

*/60 * * * * /yourpath/img_compress.sh /data/wwwroot/imgurl > /dev/null

Reload crontab#

service crond reload

- /yourpath/img_compress.sh is the absolute path of the script
- /data/wwwroot/imgurl is the absolute path of the image

# Finally
> The script by default finds image files that have been modified within 60 minutes (plus the scheduled task runs every hour, so it won't compress duplicate images), and only compresses images larger than 100kb. You can also download the script and make modifications to these settings yourself (you can also adjust the compression level of the images). This solution is suitable for Linux server environments where images are stored. - Xiaoz Blog

**Reference article**<br/>
[Xiaoz Blog - Batch Compress Images Automatically Using Shell Scripts in Linux](https://www.xiaoz.me/archives/13511)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.