Flynsarmy/wp-flyn-image-optimizer-plugin

Automatically compresses and scales down overly large images on upload.

★ 1Forks 0PHPGitHub ↗Compare

README

Flyn Image Optimizer for WordPress

Software License Build Status - PHP Code Quality

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 + .webp suffix.
  • 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 clone to /wp-content/plugins/flyn-image-optimizer
  • composer 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 apache and php-fpm services
  • wp-cli is 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');

Contributors

Flynsarmyscrutinizer-auto-fixer

Issues