One of the keys in Web Performance is the optimization of images, something that on WordPress can be done through some plugins, but also on the server itself.
When you upload an image, the original is maintained and the smaller images are generated. But those default images are not optimized and are generated by PHP itself, inheriting the noise that the original image may include.
And since we want to be able to optimize the images on our own server, let’s see how to get it.
This tutorial has been created on a Clouding.io VPS. You can create your own VPS from 3€/month.
In addition, you have the possibility to create your VPS with the WordPress image in one click.
We need to have WP-CLI installed and an operating system such as Ubuntu or CentOS. It currently requires PHP 7 to work.
In addition, we will install a series of applications that will allow us to optimize each of the types of images:
- Jpegoptim: for JPEG optimization.
- OptiPNG: for PNG optimization.
- pngquant: for PNG optimization.
- Gifsicle: for optimization and GIF.
- SVGO: for SVG optimization.
- cwebp: for WebP optimization.
We can install them on Ubuntu
apt -y install jpegoptim optipng pngquant gifsicle webp npm install -g firstname.lastname@example.org
Or we can install them on CentOS
dnf -y install jpegoptim optipng pngquant gifsicle libwebp-tools npm install -g email@example.com
To install it and make it work, we will first check if we have PHP 7 or another version by default. If we already have a PHP 7.2+ the system will work directly. If we have PHP 8.0 we will have to do a little trick.
wp cli info
Once we know if we have PHP 7 or 8, we will use the commands in one way or another. As in general PHP 8 is already a default in systems, I will apply the command with “the trick”.
To begin with, we will install the package that allows optimization.
/usr/bin/php7.4 "$(which wp)" package install typisttech/image-optimize-command:@stable --allow-root
wp package install typisttech/image-optimize-command:@stable
Once we have it, we can see the help of the command.
wp help image-optimize
And we can launch a massive update of all the images we have in the Media of our WordPress.
/usr/bin/php7.4 "$(which wp)" media regenerate --allow-root /usr/bin/php7.4 "$(which wp)" image-optimize batch --limit=9999999 --allow-root
wp media regenerate wp image-optimize batch --limit=9999999
From this moment we will have our images optimized so that they give us a very good note in the tools that analyze the Web Performance.
About this document
This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.