|
| 1 | +# typisttech/image-optimize-command |
| 2 | + |
| 3 | +[](https://packagist.org/packages/typisttech/image-optimize-command) |
| 4 | +[](https://packagist.org/packages/typisttech/image-optimize-command) |
| 5 | +[](https://travis-ci.org/TypistTech/image-optimize-command) |
| 6 | +[](https://travis-ci.org/TypistTech/image-optimize-command) |
| 7 | +[](https://styleci.io/repos/119003751) |
| 8 | +[](https://gemnasium.com/github.com/TypistTech/image-optimize-command) |
| 9 | +[](https://packagist.org/packages/typisttech/image-optimize-command) |
| 10 | +[](https://typist.tech/donate/image-optimize-command/) |
| 11 | +[](https://typist.tech/contact/) |
| 12 | + |
| 13 | +Easily optimize images using WP CLI. |
| 14 | + |
| 15 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 16 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 17 | + |
| 18 | + |
| 19 | +- [Using](#using) |
| 20 | +- [Installing](#installing) |
| 21 | + - [Optimization tools](#optimization-tools) |
| 22 | +- [FAQs](#faqs) |
| 23 | + - [What kind of optimization it does?](#what-kind-of-optimization-it-does) |
| 24 | + - [Why the optimize command stopped for no reason?](#why-the-optimize-command-stopped-for-no-reason) |
| 25 | + - [Does running `wp image-optimize run` multiple times trigger multiple optimization for the same attachments?](#does-running-wp-image-optimize-run-multiple-times-trigger-multiple-optimization-for-the-same-attachments) |
| 26 | + - [Why my GIFs stopped animating?](#why-my-gifs-stopped-animating) |
| 27 | + - [Is it for everyone?](#is-it-for-everyone) |
| 28 | +- [Support](#support) |
| 29 | + - [Why don't you hire me?](#why-dont-you-hire-me) |
| 30 | + - [Want to help in other way? Want to be a sponsor?](#want-to-help-in-other-way-want-to-be-a-sponsor) |
| 31 | +- [Credits](#credits) |
| 32 | +- [Contributing](#contributing) |
| 33 | + - [Reporting a bug](#reporting-a-bug) |
| 34 | + - [Creating a pull request](#creating-a-pull-request) |
| 35 | + |
| 36 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 37 | + |
| 38 | +WP CLI wrapper for [spatie/image-optimizer](https://github.com/spatie/image-optimizer). Optimizing PNGs, JPGs, SVGs and GIFs by running them through a chain of various image [optimization tools](#Optimization-tools). |
| 39 | + |
| 40 | +## Using |
| 41 | + |
| 42 | +```bash |
| 43 | +# Optimize 10 attachments |
| 44 | +$ wp image-optimize run --limit=10 |
| 45 | + |
| 46 | +# Optimize after thumbnail regeneration |
| 47 | +$ wp media regenerate --yes |
| 48 | +$ wp image-optimize reset --yes |
| 49 | +$ wp image-optimize run --limit=9999999 |
| 50 | +``` |
| 51 | + |
| 52 | +## Installing |
| 53 | + |
| 54 | +Installing this package requires WP-CLI v1.4.1 or greater. Update to the latest stable release with `wp cli update`. |
| 55 | + |
| 56 | +Once you've done so, you can install this package with: |
| 57 | + |
| 58 | + wp package install https://github.com/TypistTech/image-optimize-command.git |
| 59 | + |
| 60 | +### Optimization tools |
| 61 | + |
| 62 | +Under the hood, `image-optimize-command` invokes [spatie/image-optimizer](https://github.com/spatie/image-optimizer) which requires these binaries installed: |
| 63 | + |
| 64 | +- [JpegOptim](http://freecode.com/projects/jpegoptim) |
| 65 | +- [Optipng](http://optipng.sourceforge.net/) |
| 66 | +- [Pngquant 2](https://pngquant.org/) |
| 67 | +- [SVGO](https://github.com/svg/svgo) |
| 68 | +- [Gifsicle](http://www.lcdf.org/gifsicle/) |
| 69 | + |
| 70 | +Check spatie/image-optimizer's readme for [install instructions](https://github.com/spatie/image-optimizer#optimization-tools). |
| 71 | + |
| 72 | +Note that WordPress doesn't support SVG out of the box. You can omit [SVGO](https://github.com/svg/svgo). |
| 73 | + |
| 74 | +## FAQs |
| 75 | + |
| 76 | +### What kind of optimization it does? |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +Check Freek Van der Herten's [article](https://murze.be/easily-optimize-images-using-php-and-some-binaries) explaining `spatie/image-optimizer`'s [*sane default configuration*](https://github.com/spatie/image-optimizer/blob/124da0d/src/OptimizerChainFactory.php) |
| 81 | + |
| 82 | +### Why the optimize command stopped for no reason? |
| 83 | + |
| 84 | +Expected result: |
| 85 | +```bash |
| 86 | +$ wp image-optimize run --limit=10 |
| 87 | +Success: 10 unoptimized attachment(s) found. Starting... |
| 88 | +...omitted... |
| 89 | +Success 10 attachment(s) optimized |
| 90 | + |
| 91 | +$ wp image-optimize run --limit=10 |
| 92 | +No unoptimized attachment found. Abort! |
| 93 | +``` |
| 94 | + |
| 95 | +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: |
| 96 | +```bash |
| 97 | +$ wp media regenerate --yes |
| 98 | +$ wp image-optimize reset --yes |
| 99 | +$ wp image-optimize run --limit=9999999 |
| 100 | +``` |
| 101 | + |
| 102 | +### Does running `wp image-optimize run` multiple times trigger multiple optimization for the same attachments? |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +### Why my GIFs stopped animating? |
| 107 | + |
| 108 | +> 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**... |
| 109 | +> --- [wpbeginner](http://www.wpbeginner.com/wp-tutorials/how-to-add-animated-gifs-in-wordpress/) |
| 110 | +
|
| 111 | +Luckily for you, Lasse M. Tvedt showed how to disable stop WordPress from resizing GIFs on [StackExchange](https://wordpress.stackexchange.com/a/229724). |
| 112 | + |
| 113 | +### Is it for everyone? |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +## Support |
| 118 | + |
| 119 | +Love `image-optimize-command`? Help me maintain it, a [donation here](https://typist.tech/donation/) can help with it. |
| 120 | + |
| 121 | +### Why don't you hire me? |
| 122 | + |
| 123 | +Ready to take freelance WordPress jobs. Contact me via the contact form [here ](https://typist.tech/contact/) or, via email [[email protected]](mailto:[email protected]) |
| 124 | + |
| 125 | +### Want to help in other way? Want to be a sponsor? |
| 126 | + |
| 127 | +Contact: [Tang Rufus ](mailto:[email protected]) |
| 128 | + |
| 129 | +## Credits |
| 130 | + |
| 131 | +[`image-optimize-command`](https://github.com/TypistTech/image-optimize-command) is a [Typist Tech](https://typist.tech) project and maintained by [Tang Rufus](https://twitter.com/TangRufus), freelance developer for [hire](https://www.typist.tech/contact/). |
| 132 | + |
| 133 | +Full list of contributors can be found [here](https://github.com/TypistTech/image-optimize-command/graphs/contributors). |
| 134 | + |
| 135 | +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. |
| 136 | + |
| 137 | +## Contributing |
| 138 | + |
| 139 | +We appreciate you taking the initiative to contribute to this project. |
| 140 | + |
| 141 | +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. |
| 142 | + |
| 143 | +For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines. |
| 144 | + |
| 145 | +### Reporting a bug |
| 146 | + |
| 147 | +Think you’ve found a bug? We’d love for you to help us get it fixed. |
| 148 | + |
| 149 | +Before you create a new issue, you should [search existing issues](https://github.com/typisttech/image-optimize-command/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version. |
| 150 | + |
| 151 | +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](https://github.com/typisttech/image-optimize-command/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, [review our bug report documentation](https://make.wordpress.org/cli/handbook/bug-reports/). |
| 152 | + |
| 153 | +### Creating a pull request |
| 154 | + |
| 155 | +Want to contribute a new feature? Please first [open a new issue](https://github.com/typisttech/image-optimize-command/issues/new) to discuss whether the feature is a good fit for the project. |
| 156 | + |
| 157 | +Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally. |
0 commit comments