While there are lots of WordPress plugins that compress image files, they’re mostly paid, or worse, based on a never ending subscription fee. Setting up open-source native image compression on a Linux server is actually surprisingly easy to do and the best part is, it’s free. If you have root access to your Ubuntu server, setting up PNG and JPG compression is super easy and should give you significantly better scores on tools like Google’s Page Speed Insights.

First, install JPEGOptim and OptiPNG:

sudo apt-get install jpegoptim
sudo apt-get install optipng

Optimizing Images with Bash

Now that we’ve got JPEGOptim and OptiPNG installed (pretty hard, huh?) we need to open the parent directory that all of the images we want to optimize live in or under. In this case, I’m optimizing the images in a WordPress 4.9.5 instance, so I’m going to CD to the “WP-Content” folder:

cd /var/www/html/wp-content

Now that I’m in the WP-Content folder, I can compress all of the images that live in this folder, as well as all of the other files that are contained in the sub-folders below it:

find . -name "*.png" -exec optipng '{}' \;
find . -name "*.jpg" -exec jpegoptim '{}' \;

We could also run this command from the root of the server and optimize all the jpgs and pngs in every folder:

find / -name "*.png" -exec optipng '{}' \;
find / -name "*.jpg" -exec jpegoptim '{}' \;

You can also configure a cron tab so that optimization will happen automatically each day at a certain time. First, open a cron tab:

sudo crontab -e

Then, create a line for the task you want cron to run. I’m going to set it run each night at 11:30pm:

30 23 * * * find / -name "*.jpg" -exec jpegoptim '{}' \;
30 23 * * * find / -name "*.png" -exec optipng '{}' \;

Well that was easy, wasn’t it? If you want to go one step further and make your JPGs even more optimized, you could use the  –allprogressive in your JPEGOptim script to add an extra layer of optimization:

find / -name "*.jpg" -exec jpegoptim --all-progressive '{}' \;

Progressive optimization will allow requests to be made while JPGs are still loading (versus each JPG having to load in sequence before the next request can begin).

We can optimize this even more by adding –strip-all, which will strip all of the metadata stored in your JPG images, making them even less bloated:

find / -name "*.jpg" -exec jpegoptim --all-progressive --strip-all '{}' \;

Optimizing Images with Python

You could also do this in Python using for x in $ with $PWD (print working directory) and -iname instead of find and -name. I personally tend to prefer the first example more because it’s a bit simpler and cleaner:

for x in $(find $PWD -iname "*.jpg"); do jpegoptim --all-progressive $x; done

Optimizing Images with PERL

Lastly, here’s one way you could script this in PERL. This example strips all meta data associated with images, compresses them, and optimizes them to be progressive:

optimize() {
  jpegoptim *.jpg --strip-all --all-progressive
  for i in *
  do
    if test -d $i
    then
      cd $i
      echo $i
      optimize
      cd ..
    fi
  done
  echo
}
optimize

Here’s that same script modified for OptiPng:

optimize() {
  optipng *.png
  for i in *
  do
    if test -d $i
    then
      cd $i
      echo $i
      optimize
      cd ..
    fi
  done
  echo
}
optimize

Now, let’s make it even better by combining them:

optimize() {
  jpegoptim *.jpg --strip-all --all-progressive
  optipng *.png
  for i in *
  do
    if test -d $i
    then
      cd $i
      echo $i
      optimize
      cd ..
    fi
  done
  echo
}
optimize
Share this post