Easily optimize images using WP CLI.
WP CLI wrapper for spatie/image-optimizer. Optimizing PNGs, JPGs, SVGs and GIFs by running them through a chain of various image optimization tools.
# Optimize 10 attachments $ wp image-optimize run --limit=10 # Optimize after thumbnail regeneration $ wp media regenerate --yes $ wp image-optimize reset --yes $ wp image-optimize run --limit=9999999
Installing this package requires WP-CLI v1.4.1 or greater. Update to the latest stable release with wp cli update
.
Once you've done so, you can install this package with:
$ wp package install typisttech/image-optimize-command:@stable
Under the hood, image-optimize-command
invokes spatie/image-optimizer which requires these binaries installed:
Check spatie/image-optimizer's readme for install instructions.
Note that WordPress doesn't support SVG out of the box. You can omit SVGO.
Mostly applying compression, removing metadata and reducing the number of colors to PNGs, JPGs, SVGs and GIFs. The package is smart enough pick the right tool for the right image.
Check Freek Van der Herten's article explaining spatie/image-optimizer
's sane default configuration
Expected outputs:
$ wp image-optimize run --limit=3 Success: 3 unoptimized attachment(s) found. Starting... Start optimizing /app/public/wp-content/uploads/2018/01/source-150x150.gif Using optimizer: `Spatie\ImageOptimizer\Optimizers\Gifsicle` Executing `"gifsicle" -b -O3 '/app/public/wp-content/uploads/2018/01/source-150x150.gif'` ...omitted... Success: 3 attachment(s) optimized $ wp image-optimize run --limit=10 No unoptimized attachment found. Abort!
If it stopped halfway, most likely you deleted the images from disk but not updated WordPress' database. Simplest solution is to regenerate thumbnails then optimize again:
$ wp media regenerate --yes $ wp image-optimize reset --yes $ wp image-optimize run --limit=9999999
Does running wp image-optimize run
multiple times trigger multiple optimization for the same attachments?
No, by default, optimized flags (meta fields) are given to attachments after optimization ($ wp image-optimize run --limit=999
). This is to prevent re-optimizing an already optimized attachment. If you changed the image files (e.g.: resize / regenerate thumbnail), you must first reset their meta flags.
When you upload an image using the media uploader, WordPress automatically creates several copies of that image in different sizes...When creating new image sizes for animated GIFs, WordPress ends up saving only the first frame of the GIF...
--- wpbeginner
Luckily for you, Lasse M. Tvedt showed how to disable stop WordPress from resizing GIFs on StackExchange.
No, it comes at a cost. Optimization is CPU intensive. Expect CPU usage rockets up to 100% during optimization. Schedule it to run at late night in small batches.
Love image-optimize-command
? Help me maintain it, a donation here can help with it.
Ready to take freelance WordPress jobs. Contact me via the contact form here or, via email info@typist.tech
Contact: Tang Rufus
image-optimize-command
is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.
Full list of contributors can be found here.
Special thanks to Freek Van der Herten whose spatie/image-optimizer package makes this project possible.
We appreciate you taking the initiative to contribute to this project.
Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.
For a more thorough introduction, check out WP-CLI's guide to contributing. This package follows those policy and guidelines.
Think you’ve found a bug? We’d love for you to help us get it fixed.
Before you create a new issue, you should search existing issues to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.
Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please create a new issue. Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, review our bug report documentation.
Want to contribute a new feature? Please first open a new issue to discuss whether the feature is a good fit for the project.
Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience. See "Setting up" for details specific to working on this package locally.