Large images means a large page size which means slow WordPress page load times for users. The most common culprit is image size or lack of compression. If you never had any image size restrictions or automatic resizing on your site, it can be extremely convenient to batch resize those images in Linux.
This guide shows you how to batch resize jpg and png files using Imagemagick on Ubuntu or Debian. If you want to install Imagemagick on CentOS see this guide.
Batch Resize Images using Linux Command Line and Imagemagick
First we need to install Imagemagick from the repository on Debian or Ubuntu
We can use the identify
command to get the width (%w
) and height (%h
)
You will see the resolution, width is first then height.
3960x2120
You can resize the image if it is larger than the specified dimensions. This will automatically preserve the aspect ratio of the image too.
Note that this overwrites your original image!
Check the image was resized
All good!
600x321
Now we can move on to batch resizing
Batch Resize Images with Linux with Imagemagick
Create the following script
Paste the script below, remember to change the FOLDER
variable!
FOLDER
is the absolute path to your image folder
WIDTH
is the max width and HEIGHT
is the max height.
It will overwrite your original images so make sure you have a backup before running this!
Ctrl+X, Y and Enter to save and exit.
Check the folder size
Check output size
220MB
Screen will ensure the batch command keeps on running even if your SSH session is terminated.
Create a new screen session, press space or enter at the intro screen
Execute the script
Detach the screen with Ctrl+A and pressing D (detach).
You can use the top command and look for jpegoptim processes once in a while.
Reattach the screen like so
Check the folder size again
You should see an improvement
186MB