Compressing images with CLI

There are many WordPress plugins that optimize images, most do that via an external service and has some limits (like the number of images compressed per month). If you're using a VPS or a dedicated server, you can easily optimize images with command line. It's super fast and just takes a few minutes. And the compression is lossless, e.g. the quality of images will remain, so it doesn't affect anything on your site.

Optimize PNG with optipng

optipng is a free utility that compresses PNG images to a smaller size, without losing any information. Installing it is easy as follows:

apt install optipng

And using it is simple:

# Go to your "wp-content/uploads" folder
cd /var/www/example.com/wp-content/uploads/

# Find all images and compress them recursively
find . -name "*.png" | xargs -d "\n" optipng --strip all

optipng has many options such as compression level that you can explore with man optipng. Here we use the default compression and the option --strip all to remove all extra info.

Optimize JPEG with jpegoptim

Similar to PNG, JPEG has jpegoptim, a free utility to optimize/compress JPEG files. Installing and using it is very similar to optipng:

# Install
apt install jpegoptim

# Go to your "wp-content/uploads" folder
cd /var/www/example.com/wp-content/uploads/

# Find all images and compress them recursively
find . -name "*.jpg" | -d "\n" xargs jpegoptim --strip-all -t

The compression is lossless, and we also strip all extra info from images. To learn more about the options, please visit jpegoptim's man page.

What else I can do with CLI?

Using CLI to optimize images, you have a direct, obvious benefits like:

  • Saving disk space
  • Reducing image file size, thus increases loading speed

CLI is very fast: comparing to an external service, using CLI can save you hours if you have a lot of images.

Besides of that, using CLI allows you to automate the process. You can put the commands in your bash script and/or add it to your cronjob. It's very similar to what I did with the backup script. If you manage a lot of sites on a server (or multiple servers), then the automation is a life saver!

Bonus: regenerate image sizes

After compressing images, the image data is changed and you might want to tell WordPress about that. Fortunately, you can do that easily with WP-CLI:

# Regenerate all thumbnails, without confirmation
wp media regenerate --yes

You can also remove unneeded image sizes with command line to save more disk space, especially after you switch themes as each theme can register different image sizes. Keeping them on your website might be redundant.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

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

Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO