Most Efficient Way to Compress JPG files on Your Server

By April 16, 2019 No Comments

Imagine the scenario. You have a WordPress website that has over 10GB of images resulting in poor MySQL performance and slowing the website down. This was me not long ago when working on a clients website.

The most obviously thing to do was to compress the images and if you have been in a similar situation you would of turned to a WordPress plugin to compress these images. Chances are what you found was a bunch of free plugins that did the job but limited on the amount of images you could compress, forcing you to pay to upgrade to a premium plugin to complete the job. Even if you did this i bet it took hours or days to complete.

Today im going to show you a better solution that’s not only free but a hell of a lot faster too.

Introducing jpegoptim

jpegoptim is a great lightweight script that run on your server and will compress your images using “lossy” compression.

Quoting Wikipediea
lossy compression or irreversible compression is the class of data encoding methods that uses inexact approximations and partial data discarding to represent the content. These techniques are used to reduce data size for storing, handling, and transmitting content.”

The installation of jpegoptim on Centos is super simple.

1. Install EPL Repository by running the following command

yum install epel-release

2. Install by running this command

yum install jpegoptim

For Debian sysytems the following command

apt-get install jpegoptim

Thats it folks….

Now for the nitty gritty.

Lets say you want to compress the images located in wp-content/uploads/2019/04

Simply SSH into the server and CD to this folder location

Run the following command

jpegoptim --size=30% *.jpg

and jpegoptim will find all .jpg files and reduce the size of each down to 30% of the original size.

You can change the % of reduction as much or as less as you want but bare in mind, the smaller you compress the image the more reduction in image quality occurs.

Some directorys will be too large for the script to run. In this instance we use a different command which is slightly slower to complete

find -name "*.jpg" -exec jpegoptim --size=30% {} \;

Again you can change the % of decrease as you see fit.

The script will also work with folder depths. For example instead of reducing images found in /2019/04/ you can CD into the /2019/ folder and have the script find images inside lower directorys automatically.

jpegoptim has a lot more functionailty than what i have mentioned here so for more information visit the official siteĀ or github.