Compress and optimise WordPress images with Linux command-line tools

Compress WordPress images on the server with command-line tools

Years of uploads from phones and cameras leave the uploads folder full of JPEGs that are far bigger than they need to be. Tools like jpegoptim, optipng and pngquant shrink them on the server in one run, without a plugin and without uploading anything to an external service. Done carelessly, the same tools can also ruin images or leave WordPress with wrong metadata, so the order of steps matters.

Problem: The uploads folder has grown to several gigabytes, many images are large unoptimised JPEGs and PNGs, and the host does not allow image optimisation plugins.

Solution: Make a backup of uploads, try the settings on a copy of a few files, then optimise JPEGs with jpegoptim and PNGs with optipng (lossless) or pngquant (lossy) in place. Do not resize originals behind WordPress’ back; let WordPress create smaller sizes instead.

Install the tools

sudo apt update
sudo apt install -y jpegoptim optipng pngquant

Backup and a test

cd /var/www/html/wp-content
tar -czf ~/uploads-before-optimise.tar.gz uploads
du -sh uploads

# Try on a copy first
mkdir -p ~/img-test && cp uploads/2022/05/*.jpg ~/img-test/
jpegoptim --max=82 --strip-all ~/img-test/*.jpg

Open a few of the test files and compare them with the originals at full size. Quality 82 is close to WordPress’ own default for generated sizes and usually invisible; go lower only after checking.

JPEGs

find uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -size +100k \
    -exec jpegoptim --max=82 --strip-all --preserve --quiet {} +

--max=82 recompresses only files above that quality, files already smaller stay as they are. --strip-all removes EXIF data, which also removes the camera’s location data from photos, usually a good thing for privacy. --preserve keeps the file dates.

PNGs

# Lossless: smaller files, identical pixels
find uploads -type f -iname '*.png' -size +50k -exec optipng -o2 -quiet {} +

# Lossy: much smaller, fewer colours; fine for screenshots and flat graphics
find uploads -type f -iname '*.png' -size +200k -exec pngquant --quality=70-85 --skip-if-larger --ext .png --force {} +

pngquant reduces the number of colours. For photos saved as PNG it can produce visible banding; run it only on folders or files you checked.

What not to do

Resizing originals in place (for example with ImageMagick’s convert -resize over all files) changes their dimensions, while WordPress keeps the old width and height in each attachment’s metadata. Images then get wrong width/height attributes and wrong srcset entries. If originals are too large, either regenerate the metadata afterwards (wp media regenerate --yes) or, better, leave the originals and rely on the sizes WordPress creates; since WordPress 5.3, new uploads above 2560 pixels are scaled down automatically.

Running the tools again later only touches files that still need it, so a monthly cron job keeps new uploads small:

0 3 1 * * find /var/www/html/wp-content/uploads -newermt "-35 days" -type f -iname '*.jpg' -exec jpegoptim --max=82 --strip-all --quiet {} +

The result

du -sh uploads

Typical savings on an old media library are 20–50% for JPEGs. The generated sizes (photo-300×200.jpg) are optimised by the same commands, since they are files in the same folders. Clear the page cache and the CDN cache afterwards, so visitors get the new files.

Making WordPress create WebP versions of new uploads is described in serving WebP images without a plugin.

NOTE: Compression is lossy and cannot be undone without the backup. Keep the archive from the first step until you have checked the important images on the live site, especially photography, product photos and images with text.