Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork19
Easily optimize images using WP CLI
License
typisttech/image-optimize-command
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Easily optimize images using WP CLI
Built with ♥ byTypist Tech
Image Optimize Command is an open source project and completely free to use.
However, the amount of effort needed to maintain and develop new features is not sustainable without proper financial backing. If you have the capability, please consider donating using the links below:
Image Optimize Command is a WP CLI wrapper forspatie/image-optimizer whichoptimizegif
,jpeg
,jpg
,png
,svg
,webp
images by running them through a chain of various imageoptimization tools. Readthe 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,webp#shortcustsfor`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$wphelp image-optimize$wphelp image-optimize<subcommand>
Typist Tech is ready to build your next awesome WordPress site.Hire us!
- PHP v7.2 or later
- WP-CLI v2.3.0 or greater
Sincewp-cli/wp-cli-bundle
bundles an older version ofsymfony/process
which incompatible withspatie/image-optimizer
, WP-CLI must beinstalled via composer.
$wp package install typisttech/image-optimize-command:@stable
Under the hood,image-optimize-command
invokesspatie/image-optimizer
which requires these binaries installed:
Checkspatie/image-optimizer
's readme forinstall instructions.
Note thatWordPress doesn't support svg files out of the box. You can omitSVGO.However, if you haveenabled WordPress svg support and uploaded svg files to WordPress media library, you must install SVGO. Otherwise, the command will fail.
Same goes for cwebp.
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 togif
,jpeg
,jpg
,png
,svg
,webp
images. The package is smart enough pick the right tool for the right image.
Check Freek Van der Herten'sarticle explainingspatie/image-optimizer
'ssane default configuration.
useSpatie\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 =newOptimizerChain();$optimizerChain->addOptimizer($zzz);// Finallyreturn$optimizerChain;});
useTypistTech\ImageOptimizeCommand\Operations\AttachmentImages\Backup;useTypistTech\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;});
It would be nice to have those 2 null operations in a separate package. Submit pull requests to mention it in this readme if you have implemented it.
Does runningwp 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: Thefind
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.
Seespatie/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 showedhow to stop WordPress from resizing GIFs on StackExchange.
No, you can't use this on managed hosting such asKinsta,Flywheel orWP Engine because they prohibit installing the binaries.
If you must use it on managed hosting,hire a developer to add SaaS provider integration:
No, if you don't have any svg files in WordPress media library.
Yes, if you have:
- enabled WordPress SVG support
- uploaded SVGs to WordPress media library
- using the
find
subcommand (or its shortcuts) with--extensions=svg
Same goes for cwebp.
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 a monthly quota. Totally free of charge.
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 onthis article.
Never! This plugin will only work onactively supported PHP versions.
Don't use it onend of life orsecurity fixes only PHP versions.
- Articles onTypist Tech's blog
- Tang Rufus' WordPress plugins on wp.org
- More projects onTypist Tech's GitHub profile
- Stay tuned onTypist Tech's newsletter
- FollowTang Rufus' Twitter account
- HireTang Rufus to build your next awesome site
Thanks! Glad you like it. It's important to let me knows somebody is using this project. Please consider:
- tweet something good with mentioning@TangRufus
- ★ starthe Github repo
- 👀 watch the Github repo
- write tutorials and blog posts
- hire Typist Tech
Run the tests:
$composertest$composer style:check
Please provide feedback! We want to make this project as useful as possible.Pleasesubmit an issue and point out what you do and don't like, or fork the project andsend pull requests.No issue is too small.
If you discover a security vulnerability within this project, please email us atimage-optimize-command@typist.tech.All security vulnerabilities will be promptly addressed.
Image Optimize Command is aTypist Tech project and maintained byTang Rufus, freelance developer forhire.
Special thanks toFreek Van der Herten whosespatie/image-optimizer
package makes this project possible.
Full list of contributors can be foundhere.
Image Optimize Command is released under theMIT License.
About
Easily optimize images using WP CLI
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.