Bash Script For Optimizing ClassicPress & WordPress Images in Bulk

Last Updated on: September 6, 2020

Out of frustration of the way different plugin optimizes images (which is super limited, and not effective; it has never been for me)in the WordPress world, I decided to create a script that does it on a server level.

This script would recursively optimize PNG or JPEG images using the convert command and the pngquant respectively.

Note: Tested on Ubuntu only

Another Note: Make Sure You Bakcup Your Website Before Optimizing

Step 1: Install Dependencies

The script uses convert and pngquant as its dependencies. convert is part of ImageMagick, and pngquant is a program by kornelski. You will need to install both first, do so, using:

					

sudo apt install imagemagick
sudo apt install pngquant

Step 2: Changing Command Path

The $path environment variable is a number of directories in which Linux will look for scripts and or programs.

Linux will only check for the executable in the path environment only when the full or relative path to the program is given, so, let’s configure the path, and this would be done via .bash_profile, chances are it’s not available in your shell. so, create and open using the following command:

					

touch ~/.bash_profile && nano .bash_profile

Add the following line in the file:

					

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

What the above command is saying, is Mr.Bash, please look into my home directory ($HOME) in a directory called (/bin), I have programs in there, and I want you to look into that folder whenever I run a program.

You know why this is useful, it is useful in the sense that the PATH variable prevents you from having to write out the entire path to a program or scripts every time you run it. Basically, it stores the shortcut to the program file.

Save, and Close the file. To reload changes immediately without logout, enter source ~/.profile

Verify it using echo $PATH

Lastly, let’s create a directory named bin in our user home directory, this would enable us to find the script by name without the file path:

					

test -d $HOME/bin && echo "Exist/Not Creating" || mkdir $HOME/bin

What the above command does is to check if a bin directory already existed in your user home directory, if it already exists, output “Exist/Not Creating“, and if does not already exist, create the folder.

Step 3: Install and Run The Script

create file dr_optimization.sh in your bin directory:

					

nano $HOME/bin/dr_optimization.sh

Copy and Paste the following script:

					

#!/bin/bash
# Author: The_Devsrealm_Guy
# Website: https://devsrealm.com
# Script to Optimize ClassicPress Images
# Last Edited: September 2020

# Check If The Process Isn't Running Already
if pidof -o %PPID -x "$0"; then
   exit 1
fi

echo
echo "|- A Program That Optimizes Your ClassicPress Images! -|"
echo
echo "Tip: For Agressive Optimization, You choose H, For Medium Optimization, Choose M, and L for Low Optimization"
echo

read -p "Your ClassicPress Uploads Directory Path: " dir_name

read -p "Image format you want to optimize, png or jpg: " image_format

read -p "Choose Level of Optimization H, M or L: " image_optimization

# Check if variable isset

if [[ -z $dir_name || -z $image_optimization || -z $image_format ]]; then
echo "You Must Provide The Directory Path, Image Format, and Image Optimization Level "
exit 1
fi

# Check If User Selected png, and begin png optimization
if [[ $image_format = "png" ]]; then
echo "Image Size Before Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total
case $image_optimization in

H) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=40 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total
;;

M) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=65 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total
;;

L) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=85 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total
;;

esac
# Check If User Selected jpg, and begin jpg optimization
elif [[ $image_format = "jpg" ]]; then
echo "Image Size Before Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total
case $image_optimization in

H) find $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 40 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total
;;

M) find $dir_name $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 65 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total
;;

L) find $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 85 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total
;;

esac
else
echo "Image Format Not Recognized"   
fi

# return

Save with Ctrl + x and then Y

Add permission bit: chmod +x $HOME/bin/dr_optimization.sh

Now you can run the script from anywhere:

Here is an output:

Bash Script For Optimizing ClassicPress & WordPress Images in Bulk

Basically, you can choose three level of optimization, H for Higher compression, M for medium and L for low image optimization.

Point to your CP or WP image uploads directory, choose the format you want to compress, and choose the compression level.

It doesn’t have to be pointed to your CP uploads directory though, point it to anywhere as long as it’s a directory.

I’ll update it with more features, for now, bye!

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

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