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:
<divalign="center">
[](https://github.com/sponsors/TangRufus)
[](https://typist.tech/go/paypal-donate/)
**Image Optimize Command** is a WP CLI wrapper for [spatie/image-optimizer](https://github.com/spatie/image-optimizer) which **optimize `gif`, `jpeg`, `jpg`, `png`, `svg`, `webp` images by running them through a chain of various image <a href="https://github.com/spatie/image-optimizer#optimization-tools">optimization tools</a>.** Read [the introductory blog post](https://typist.tech/articles/easily-optimize-wordpress-images-using-wp-cli-and-some-binaries/) about why I built it.
Since [`wp-cli/wp-cli-bundle` bundles an older version of `symfony/process`](https://github.com/wp-cli/wp-cli-bundle/blob/7e3d89c415db7922e923703d5f06613f0a60b8a9/composer.lock#L1204-L1205) which incompatible with [`spatie/image-optimizer`](https://github.com/spatie/image-optimizer/blob/48f71b968f2764eca8f4885a8a5401d61e920ba8/composer.json#L22), WP-CLI must be [installed via composer](https://make.wordpress.org/cli/handbook/installing/#installing-via-composer).
Under the hood, `image-optimize-command` invokes [`spatie/image-optimizer`](https://github.com/spatie/image-optimizer) which requires these binaries installed:
Note that [WordPress doesn't support svg files](https://core.trac.wordpress.org/ticket/24251) out of the box. You can omit [SVGO](https://github.com/svg/svgo).
However, if you have [enabled WordPress svg support](https://kinsta.com/blog/wordpress-svg/?kaid=CGCHYHJJJMMF) and uploaded svg files to WordPress media library, you must install SVGO. Otherwise, the command will fail.
Mostly applying compression, removing metadata and reducing the number of colors to `gif`, `jpeg`, `jpg`, `png`, `svg`, `webp` images. The package is smart enough pick the right tool for the right image.
:warning: The optimzation is [lossy](https://github.com/TypistTech/image-optimize-command#will-the-images-look-different-after-optimization). **If you skip backing up the original files, there is no way to recover the original files.** Proceed with caution.
```php
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;
});
```
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.
By default, boolean flags ([meta fields](https://developer.wordpress.org/reference/functions/add_post_meta/)) 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.
> 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**...
No, you can't use this on managed hosting such as [Kinsta](https://typist.tech/go/kinsta), [Flywheel](https://typist.tech/go/flywheel) or [WP Engine](https://typist.tech/go/wp-engine) because they prohibit installing the binaries.
### `PHP Fatal error: Allowed memory size of 999999 bytes exhausted (tried to allocate 99 bytes)`
This is a common WP CLI issue. See: [https://bit.ly/wpclimem](https://make.wordpress.org/cli/handbook/common-issues/#php-fatal-error-allowed-memory-size-of-999999-bytes-exhausted-tried-to-allocate-99-bytes)
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.
- [tweet](https://twitter.com/intent/tweet?text=Image%20Optimize%20Command%20-%20%40wpcli%20wrapper%20for%20%40spatie_be%20image%20optimizer&url=https://github.com/TypistTech/image-optimize-command&hashtags=webdev,wordpress&via=TangRufus&url=https://github.com/TypistTech/image-optimize-command&hashtags=webdev,wordpress&via=TangRufus) something good with mentioning [@TangRufus](https://twitter.com/tangrufus)
- ★ star [the Github repo](https://github.com/TypistTech/image-optimize-command)
- [👀 watch](https://github.com/TypistTech/image-optimize-command/subscription) the Github repo
**Please provide feedback!** We want to make this project as useful as possible.
Please [submit an issue](https://github.com/TypistTech/image-optimize-command/issues/new) and point out what you do and don't like, or fork the project and [send pull requests](https://github.com/TypistTech/image-optimize-command/pulls/).
If you discover a security vulnerability within this project, please email us at [image-optimize-command@typist.tech](mailto:image-optimize-command@typist.tech).
All security vulnerabilities will be promptly addressed.
[Image Optimize Command](https://github.com/TypistTech/image-optimize-command) is a [Typist Tech](https://www.typist.tech) project and maintained by [Tang Rufus](https://twitter.com/Tangrufus), freelance developer for [hire](https://www.typist.tech/contact/).
Special thanks to [Freek Van der Herten](https://github.com/freekmurze/) whose [`spatie/image-optimizer`](https://github.com/spatie/image-optimizer) package makes this project possible.
[Image Optimize Command](https://github.com/TypistTech/image-optimize-command) is released under the [MIT License](https://opensource.org/licenses/MIT).