Batch Resize Images using Linux Command Line and Imagemagick

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

sudo apt-get update
sudo apt-get install imagemagick -y

We can use the identify command to get the width (%w) and height (%h)

identify -format "%wx%h" image.jpg

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!

convert image.jpg -resize 600x400\> image.jpg

Check the image was resized

identify -format "%wx%h" image.jpg

All good!

600x321

Now we can move on to batch resizing

Batch Resize Images with Linux with Imagemagick

Create the following script

mkdir -p ~/scripts
nano ~/scripts/batch-image-resize.sh

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!

#!/usr/bin/env bash
# Purpose: batch image resizer
# Source: https://guides.wp-bullet.com
# Author: Mike

# absolute path to image folder
FOLDER="/var/www/wp-bullet.com/wp-content/uploads"

# max width
WIDTH=540

# max height
HEIGHT=300

#resize png or jpg to either height or width, keeps proportions using imagemagick
#find ${FOLDER} -iname '*.jpg' -o -iname '*.png' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

#resize png to either height or width, keeps proportions using imagemagick
#find ${FOLDER} -iname '*.png' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

#resize jpg only to either height or width, keeps proportions using imagemagick
find ${FOLDER} -iname '*.jpg' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

# alternative
#mogrify -path ${FOLDER} -resize ${WIDTH}x${HEIGHT}% *.png -verbose

Ctrl+X, Y and Enter to save and exit.

Check the folder size

du -sh foldername

Check output size

220MB

Screen will ensure the batch command keeps on running even if your SSH session is terminated.

sudo apt-get install screen

Create a new screen session, press space or enter at the intro screen

screen

Execute the script

bash ~/scripts/batch-image-resize.sh

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

screen -r

Check the folder size again

du -sh foldername

You should see an improvement

186MB

Make sure to compress your images with lossless (guide) or lossy compression (guide).

If you need to do some more advanced find commands that use regex this should help.

Sources

Using find to Search for Multiple Extensions
How to get Image Information with Linux
Imagemagick Shrink
Resize Images and Keep Ratio