Image Optimize Command

repository·master·Indexed 41 days ago

https://github.com/typisttech/image-optimize-command

A WP-CLI wrapper for spatie/image-optimizer that allows developers to optimize gif, jpeg, jpg, png, svg, and webp images directly from the WordPress command line. It provides commands to optimize specific attachments, process batches of non-optimized images, and scan directories such as themes, plugins, and wp-admin. The tool includes functionality to backup original files, restore optimized images, and customize the optimization process via the TypistTech/ImageOptimizeCommand/OptimizerChain filter.

Tokens
6.9K
Snippets
36
Records
41
Agent score
50%

What's inside Image Optimize Command

  1. Prevent re-optimization of attachments

    master

    The package uses WordPress meta fields to track optimized attachments. By default, once an attachment is optimized, a boolean flag is added to its meta to prevent the command from re-optimizing it in subsequent runs.

    Note: If you manually change image files (e.g., via resizing or regenerating thumbnails), you must manually reset these meta flags so the command can re-optimize them.

  2. Install Image Optimize Command via WP-CLI

    master

    Install the stable version of the image-optimize-command package using the WP-CLI package command.

    Requirements:

    • PHP v7.2 or later
    • WP-CLI v2.3.0 or greater
    • Important: Because of a dependency incompatibility between wp-cli/wp-cli-bundle and spatie/image-optimizer, you must have WP-CLI installed via composer rather than using the standard bundle.
    $ wp package install typisttech/image-optimize-command:@stable
  3. Optimize a WordPress site for the first time

    master

    If you are performing a first-time optimization, it is best practice to regenerate all thumbnails first to ensure they exist, then run a large batch optimization.

    Recommended workflow:

    1. Regenerate thumbnails: wp media regenerate
    2. Optimize all: wp image-optimize batch --limit=9999999
    3. (Optional) Optimize specific system directories: wp image-optimize plugins, etc.
    $ wp media regenerate
    $ wp image-optimize batch --limit=9999999
  4. Optimize images with wp image-optimize

    master

    The image-optimize-command package provides a WP-CLI namespace image-optimize to manage image optimization and restoration within WordPress.

    Prerequisites

    To use the optimization features, the following tools must be installed on your system:

    • JpegOptim
    • Optipng
    • Pngquant2
    • SVGO
    • Gifsicle
    # Optimize specific attachments
    $ wp image-optimize attachment 123 223 323
    
    # Optimize certain number of attachments
    $ wp image-optimize batch --limit=20
    
    # Restore the full sized images of specific attachments
    $ wp image-optimize restore 123 223 323
    
    # Restore all full sized images and drop all meta flags
    $ wp image-optimize reset
    
    # Find and optimize images under a given directory (without backup)
    $ wp image-optimize find /path/to/my/directory
  5. How image backups are handled

    master

    When the image-optimize-command performs an optimization, it creates a backup of the original full-sized image to allow for restoration. Backups are stored in the same directory as the original file, using the suffix .original appended to the filename.

    For example, if the original file is image.jpg, the backup will be image.jpg.original.

    If a backup with the .original extension already exists for a specific path, the tool will skip creating a new one to avoid overwriting existing backups.

  6. Customize the optimization process

    master

    You can customize how images are optimized by hooking into the TypistTech/ImageOptimizeCommand/OptimizerChain filter. This allows you to modify the existing Spatie\ImageOptimizer\OptimizerChain (e.g., by setting timeouts or adding custom optimizers) or replace it entirely with a new instance. This is useful if you need to add specific binaries or change the logging behavior.

    use Spatie\ImageOptimizer\OptimizerChain;
    
    add_filter('TypistTech/ImageOptimizeCommand/OptimizerChain', function (OptimizerChain $optimizerChain): OptimizerChain {
        // Option A: Send messages to $optimizerChain.
        $optimizerChain->setTimeout($xxx);
        $optimizerChain->useLogger($yyy);
        $optimizerChain->addOptimizer($zzz);
    
        // Option B: Make a new $optimizerChain.
        // See: https://github.com/spatie/image-optimizer/blob/master/src/OptimizerChainFactory.php
        $optimizerChain = new OptimizerChain();
        $optimizerChain->addOptimizer($zzz);
    
        // Finally
        return $optimizerChain;
    });
  7. Skip backups and restorations

    master

    ⚠️ Warning: Optimization is lossy. If you skip backing up the original files, you cannot recover them.

    To skip backups or restorations, you must implement and return a 'null' version of the Backup and Restore classes via the following filters:

    use TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Backup;
    use TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Restore;
    
    add_filter('TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Backup', function (): Backup {
        // TODO: You have to implement a null backup class.
        return $myNullBackupObject;
    });
    
    add_filter('TypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Restore', function (): Restore {
        // TODO: You have to implement a null restore class.
        return $myNullRestoreObject;
    });
  8. Requirements for SVG and WebP optimization

    master

    While the package handles many formats, you must have specific binaries installed on your server if you intend to use them:

    • SVGO: Required if you have enabled WordPress SVG support, have uploaded SVGs to the media library, or are using the find subcommand with --extensions=svg.
    • cwebp: Required if you are working with WebP images.
  9. Optimize specific WordPress attachments

    master

    Use the attachment subcommand to optimize specific media items by their attachment IDs. This command backs up the full-sized image before optimization.

    # optimize specific attachments
    $ wp image-optimize attachment 123 223 323
  10. Use shortcuts to optimize plugin, theme, or core directories

    master

    The tool provides shortcuts to find and optimize images within common WordPress directories. Warning: These shortcuts do not create backups.

    # shortcusts for `wp image-optimize find` **without backup**
    $ wp image-optimize mu-plugins
    $ wp image-optimize plugins
    $ wp image-optimize themes
    $ wp image-optimize wp-admin
    $ wp image-optimize wp-includes