Images make a WordPress site looks good. However, images also make up the bulk of the size of the entire page. Nobody wants to wait for a slow site. There are some tools available such as JpegOptim, Optipng, Pngguant 2, SVGO and Gifsicle. Manually running these tools after WordPress resizing attachment images is tedious job. Luckily for you, with my newly published WP CLI package, optimizing WordPress media library's PNGs, JPGs, SVGs and GIFs is just one command away.
image-optimizer composer package helping us passing PNGs, JPGs, SVGs and GIFs to JpegOptim, Optipng, Pngguant 2, SVGO and Gifsicle via PHP. The results are stunning. On average, we see 30% smaller image size, even better than manually throwing the images to ImageOptim. Unfortunately, I can't disclose my client's data. Check the conversions example on spatie/image-optimizer's readme.spatie/image-optimizer. It comes with a sane default configuration which smart enough to decide which tools to use on a particular image. get_attached_file. With some modification on this StackExchange answer, this is what I come up with:
/**
* Get all(original and chopped) paths for a single attachment.
*
* @see https://wordpress.stackexchange.com/questions/182477/wp-get-attachment-image-src-and-server-path
*
* @param int $id Attachment id.
*
* @return string[]
*/
private static function pathsForSingle(int $id): array
{
$metadata = wp_get_attachment_metadata($id);
$originalFilePath = get_attached_file($id, true);
$sizes = array_keys(
wp_list_pluck($metadata['sizes'] ?? [], 'file')
);
$choppedFilePaths = array_map(function (string $size) use ($id, $originalFilePath): string {
$info = image_get_intermediate_size($id, $size);
return str_replace(wp_basename($originalFilePath), $info['file'], $originalFilePath);
}, $sizes);
return array_merge($choppedFilePaths, [
$originalFilePath,
]);
}
If you know a better way do to it, please leave a comment below. Or, submit a pull request via GitHub.
$ wp cli update
$ sudo apt-get update
$ sudo apt-get install jpegoptim
$ sudo apt-get install optipng
$ sudo apt-get install pngquant
$ sudo apt-get install gifsicle
$ wp package install typisttech/image-optimize-command:stable
Note that WordPress doesn't support SVG out of the box. SVGO is omitted here.
Optimizing #WordPress PNGs, JPGs, SVGs and GIFs just one command away.
Well... That's the easy part:
# Optimize 10 attachments
$ wp image-optimize run --limit=10
Boom! You just reduced 10 attachments' file sizes including all their smaller thumbnails.
By default, optimized flags (post meta) are given to attachments after optimization. This is to prevent wasting time on re-optimizing an already optimized attachment. If you changed the image files (e.g.: resize / regenerate thumbnail), you must first reset their meta flags by running $ wp image-optimize reset. Example:
$ wp media regenerate --yes
$ wp image-optimize reset --yes
$ wp image-optimize run --limit=9999999
typisttech/image-optimize-command requires WP CLI and the binaries to be installed and it's a burden for server resources. There are lots SaaS solutions available as WordPress plugins such as WP Smush Pro, EWWW and Imagify. Most of them provide free trial with some limitation such as maximum file size, monthly quota, etc. Do your research to find the best tools.
Besides, we need more test coverage. Pull requests are accepted!
Originally published at Typist Tech by Tang Rufus, freelance web developer for hire.