Easily optimize images using WP CLI.
- Usage
- Installing
- Use Cases
- FAQs
- What kind of optimization it does?
- Can I customize the optimization?
- Does running
wp image-optimize attachment / batch
multiple times trigger multiple optimization for the same attachments? - Will the images look different after optimization?
- Why my GIFs stopped animating?
- Can I use this on managed hosting?
- Do I have to install
SVGO
? PHP Fatal error: Allowed memory size of 999999 bytes exhausted (tried to allocate 99 bytes)
- Does it has any limitation?
- Will you add support for older PHP versions?
- Is it for everyone?
- It looks awesome. Where can I find some more goodies like this?
- This package isn't on wp.org. Where can I give a ⭐⭐⭐⭐⭐ review?
- Support
- Running the Tests
- Feedback
- Change log
- Security
- Credits
- License
WP CLI wrapper for spatie/image-optimizer. Optimizing PNGs, JPGs, SVGs and GIFs by running them through a chain of various image optimization tools. Check this project's introductory blog post about why I built it.
# 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
$ wp media regenerate 123 223 323
# restore all full sized images and drop all meta flags
$ wp image-optimize reset
$ wp media regenerate
# Find and optimize images under a given directory **without backup**
$ wp image-optimize find /path/to/my/directory --extensions=gif,jpeg,jpg,png,svg
# 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
# learn more
$ wp help image-optimize
$ wp help image-optimize <subcommand>
Installing this package requires WP-CLI v2.0.0 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. However, if you have enabled WordPress SVG support and uploaded SVGs to WordPress media library, you must install SVGO. Otherwise, the command will fail.
This command optimize both the full sized image(the one you uploaded) and the thumbnails(WordPress auto-resize these images for you).
Chances are the thumbnails are missing or never generated:
- theme switched after upload
- plugins activated after upload
- deleted the images from disk but not updated WordPress' database
Simplest solution is to regenerate thumbnails then optimize:
$ wp media regenerate
$ wp image-optimize batch --limit=9999999
$ wp image-optimize mu-plugins
$ wp image-optimize plugins
$ wp image-optimize themes
$ wp image-optimize wp-admin
$ wp image-optimize wp-includes
$ wp image-optimize mu-plugins
$ wp image-optimize plugins
$ wp image-optimize themes
$ wp image-optimize wp-admin
$ wp image-optimize wp-includes
This command backs up the full sized images before optimizing attachments. If you want to restore them:
# optimize
$ wp image-optimize attachment 123
# restore the full sized image
$ wp image-optimize restore 123
# regenerate the thumbnails from the original full sized image
$ wp media regenerate 123
Starting from v0.2, this command backs up the full sized images before optimizing attachments. To migrate from image-optimize-command v0.1.x:
$ wp image-optimize reset
$ wp media regenerate
$ wp image-optimize batch --limit=9999999
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.
Yes.
use Spatie\ImageOptimizer\OptimizerChain;
add_filter('typist_tech_image_optimized_optimizer_chain', 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;
});
Does running wp image-optimize attachment / batch
multiple times trigger multiple optimization for the same attachments?
No.
By default, boolean flags (meta fields) are given to attachments after optimization. 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.
Note: The find
subcommand and its shortcuts don't create meta flags.
Yes, a little bit. This is lossy optimization. However, you won't notice the difference unless you have a trained eye for that.
See spatie/image-optimizer's readme on binary options used.
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 stop WordPress from resizing GIFs on StackExchange.
No, you can't use this on managed hosting such as Kinsta or WP Engine because they don't allow you to install those binaries.
If you must use it on managed hosting, hire a developer to add SaaS provider integration:
Yes, if you have:
- enabled WordPress SVG support
- uploaded SVGs to WordPress media library
- using the
find
subcommand (or its shortcuts) with--extensions=svg
No, if you don't have any SVGs in WordPress media library.
This is a common WP CLI issue. See: https://bit.ly/wpclimem
No, unlike other SaaS alternatives, this package runs on your server without any limitation on file sizes or monthly quota. Totally free of charge.
Never! This package will only works on actively supported PHP versions.
Don't use it on end of life or security fixes only PHP versions.
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.
Learn more on this article.
- Articles on Typist Tech's blog
- Tang Rufus' WordPress plugins on wp.org
- More projects on Typist Tech's GitHub profile
- Stay tuned on Typist Tech's newsletter
- Follow Tang Rufus' Twitter account
- Hire Tang Rufus to build your next awesome site
Thanks!
Consider writing a blog post, submitting pull requests, donating or hiring me instead.
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 [email protected]
Contact: Tang Rufus
Run the tests:
$ composer test
$ composer check-style
Please provide feedback! We want to make this library useful in as many projects as possible. Please submit an issue and point out what you do and don't like, or fork the project and make suggestions. No issue is too small.
Please see CHANGELOG for more information on what has changed recently.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
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.
The MIT License (MIT). Please see License File for more information.