Install
This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.
Install straight from GitHub with WP-CLI:
wp plugin install https://github.com/flynsarmy/wp-flyn-image-optimizer-plugin/archive/refs/heads/master.zipFlyn Image Optimizer for WordPress
This plugin automatically compresses and scales down overly large images on upload.
What it Does
On upload:
- the full-size image will be checked for min/max size constraints and scale if necessary
- It will then be optimized for filesize
- After thumbs are generated, a WebP copy of each size is created with the same filename +
.webpsuffix. - If enabled, all
<img>tags on your sites frontend will be replaced with<picture>tags that use the WebP file with original image as a fallback for browsers without WebP support.
Installation
git cloneto /wp-content/plugins/flyn-image-optimizercomposer install- For Ubuntu:
apt-get update && apt-get install advancecomp pngcrush gifsicle jpegoptim imagemagick pngnq optipng pngquant - For OSX:
brew install advancecomp pngcrush gifsicle jpegoptim imagemagick pngnq optipng pngquant svgo wp-cli - Restart
apacheandphp-fpmservices wp-cliis required for batch operations
Usage
For maximum performance, this plugins functionality is changed through filters rather than an admin page. See the Filters section below for details.
WebP generation is enabled by default but automatic replacing on frontend images isn't. This is because you probably don't have WebP files generated for all your existing images yet.
Run the CLI command wp flynio regenerate --scale --optimize before enabling this functionality.
Filters
Setting minimum/maximum upload dimensions
By default uploaded images are scaled to a minimum of 100x100 or maximum of 3840x3840. This can be changed with:
add_filter('flynio-limit-dimensions', function ($dimensions) {
return [
[150, 150], // minimum x/y dimensions
[1920, 1080], // maximum x/y dimensions
];
});
Setting which image types to convert to JPG on upload
By default bitmaps and tiffs are converted to jpeg on upload. This can be overridden with:
add_filter('flynio-mimes-to-convert', function ($mimes) {
return ['image/bmp', 'image/tif', 'image/tiff'];
});
Setting optimizer options
If using homebrew you'll likely want to overwrite the image optimizer's default filepaths like so:
add_filter('flynio-optimizer-options', function ($options) {
return array_merge($options, [
'jpegtran_bin' => '/usr/local/bin/jpegtran',
'jpegoptim_bin' => '/usr/local/bin/jpegoptim',
'pngcrush_bin' => '/usr/local/bin/pngcrush',
'optipng_bin' => '/usr/local/bin/optipng',
'pngquant_bin' => '/usr/local/bin/pngquant',
]);
});
Custom optimizer logger
By default NullLogger is used by the image optimizer. Set a custom logger with the following:
class StdoutLogger extends \Psr\Log\AbstractLogger
{
public function log($level, $message, array $context = [])
{
if (!empty($context['exception'])) {
echo $context['exception']->getMessage();
} else {
echo $message;
}
echo "<br/>\n";
}
}
add_filter('flynio-optimizer-logger', function ($logger) {
return new StdoutLogger();
});
Don't want automatic WebP image generation?
add_filter('flynio-generate-webp-images', '__return_false');
Enable
to conversion on site frontend
add_filter('flynio-use-webp-images', '__return_true');