Main menu

Optimize and compress images with Linux command

As i am having the WordPress site, need to tweak a lot of seeings to make it smoother. Optimizing and compressing images one of them. For a long time i am using the WordPress plugin “WP Smush“. I was looking a way to compress images with Linux command and found some useful utility for bulk compression.

Installing the utilities in CentOS

# yum install jpegoptim optipng gifsicle

There are three main utilities we are using here for optimizing the images.
jpegoptim = jpegoptim utility provide lossless and lossy optimization for jpg files.

# jpegoptim image.jpg

gifsicle = Gifsicle is a command-line tool for creating, editing GIF images and animations. You can combile multiple png or jpg files into a gif files. It also perform the compression on gif image.

# gifsicle --colors 256 --batch -i image.gif

optipng = optipng compress the png files and also convert other formats to png format. It also perform integirty checks on png files.

# optipng image.png

Note : –

  • looseless : optimize based on huffman tables without loosing the quality.
  • loosy : optimize based on settings and quality.

Bulk compress images with Linux command

Let’s find all files in your website and perform compression. We used the find command for specific extension in your website directory and execute the compression utility to compress them.

# find /var/www -type f -name "*.jpg" -exec jpegoptim {} \;
# find/var/www -type f -name "*.gif" -exec gifsicle --colors 256 --batch -i {} \;
# find /var/www -type f -name "*.png" -exec optipng {} \;

Now you can schedule your command like this to find files older then 7 days and compress it.

# find /var/www -type f -name "*.jpg" -mtime +7 -exec jpegoptim {} \;
# find/var/www -type f -name "*.gif" -mtime +7 -exec gifsicle --colors 256 --batch -i {} \;
# find /var/www -type f -name "*.png" -mtime +7 -exec optipng {} \;

This way you can optimize your website images weekly without any hassle.

FacebookTwitterGoogle+RSS