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:

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”bash”]

    cd /var/www/html/wp-content

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

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

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

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

     

    [codesyntax lang=”bash”]

    sudo crontab -e

    [/codesyntax]

     

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

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”bash”]

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

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”python”]

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

    [/codesyntax]

     

    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:

     

    [codesyntax lang=”perl”]

    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

    [/codesyntax]

     

    Here’s that same script modified for OptiPng:

     

    [codesyntax lang=”perl”]

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

    [/codesyntax]

     

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

     

    [codesyntax lang=”perl”]

    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

    [/codesyntax]

    Tags:

    • Show Comments (0)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    comment *

    • name *

    • email *

    • website *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Support our site

    If you find this site helpful, please consider using our affiliate links. Ayrne may earn a small commission from your purchase, but it won’t cost you anything at all and in some cases, it may even save you money! We use affiliate links instead of banner ads to provide you with a cleaner, more focused experience on our website.

    You May Also Like

    How to Harden NginX Security

    While there are many ways to harden the security on your NginX server with ...

    How to Install Google PAM Multi-Factor SSH Auth on a Ubuntu Server

    If you're looking to add an extra layer of iron clad security to your ...

    How to Install Redis Server on Ubuntu 16.04 with WordPress

    The quick easy and totally correct way to setup Redis object cache to work ...