Skip to content

Commit c9ac051

Browse files
committed
Merge branch 'readme'
2 parents adc7617 + 33bf909 commit c9ac051

File tree

1 file changed

+151
-54
lines changed

1 file changed

+151
-54
lines changed

README.md

Lines changed: 151 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# typisttech/image-optimize-command
22

3+
[![Packagist](https://img.shields.io/packagist/v/typisttech/image-optimize-command.svg)](https://packagist.org/packages/typisttech/image-optimize-command)
4+
[![Packagist](https://img.shields.io/packagist/dt/typisttech/image-optimize-command.svg)](https://packagist.org/packages/typisttech/image-optimize-command)
35
[![Build Status](https://travis-ci.org/TypistTech/image-optimize-command.svg?branch=master)](https://travis-ci.org/TypistTech/image-optimize-command)
4-
[![PHP Versions Tested](http://php-eye.com/badge/typisttech/image-optimize-command/tested.svg)](https://travis-ci.org/TypistTech/image-optimize-command)
5-
[![StyleCI](https://styleci.io/repos/119003751/shield?branch=master)](https://styleci.io/repos/119003751)
6-
[![License](https://poser.pugx.org/typisttech/image-optimize-command/license)](https://packagist.org/packages/typisttech/image-optimize-command)
6+
[![codecov](https://codecov.io/gh/TypistTech/image-optimize-command/branch/master/graph/badge.svg)](https://codecov.io/gh/TypistTech/image-optimize-command)
7+
[![GitHub](https://img.shields.io/github/license/TypistTech/image-optimize-command.svg)](https://github.com/TypistTech/image-optimize-command/blob/master/LICENSE.md)
78
[![Donate via PayPal](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://typist.tech/donate/image-optimize-command/)
89
[![Hire Typist Tech](https://img.shields.io/badge/Hire-Typist%20Tech-ff69b4.svg)](https://typist.tech/contact/)
910

@@ -13,52 +14,72 @@ Easily optimize images using WP CLI.
1314
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1415

1516

16-
- [Using](#using)
17+
- [Usage](#usage)
1718
- [Installing](#installing)
1819
- [Optimization tools](#optimization-tools)
20+
- [Use Cases](#use-cases)
21+
- [First use](#first-use)
22+
- [Restore the originals](#restore-the-originals)
23+
- [Migrate from image-optimize-command v0.1.x](#migrate-from-image-optimize-command-v01x)
1924
- [FAQs](#faqs)
2025
- [What kind of optimization it does?](#what-kind-of-optimization-it-does)
21-
- [Why the optimize command stopped for no reason?](#why-the-optimize-command-stopped-for-no-reason)
22-
- [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+
- [Can I customize the optimization?](#can-i-customize-the-optimization)
27+
- [Does running `wp image-optimize attachment / batch` multiple times trigger multiple optimization for the same attachments?](#does-running-wp-image-optimize-attachment--batch-multiple-times-trigger-multiple-optimization-for-the-same-attachments)
2328
- [Will the images look different after optimization?](#will-the-images-look-different-after-optimization)
2429
- [Why my GIFs stopped animating?](#why-my-gifs-stopped-animating)
30+
- [Can I use this on managed hosting?](#can-i-use-this-on-managed-hosting)
31+
- [Do I have to install `SVGO`?](#do-i-have-to-install-svgo)
32+
- [`PHP Fatal error: Allowed memory size of 999999 bytes exhausted (tried to allocate 99 bytes)`](#php-fatal-error-allowed-memory-size-of-999999-bytes-exhausted-tried-to-allocate-99-bytes)
2533
- [Does it has any limitation?](#does-it-has-any-limitation)
34+
- [Will you add support for older PHP versions?](#will-you-add-support-for-older-php-versions)
2635
- [Is it for everyone?](#is-it-for-everyone)
2736
- [It looks awesome. Where can I find some more goodies like this?](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)
2837
- [This package isn't on wp.org. Where can I give a :star::star::star::star::star: review?](#this-package-isnt-on-wporg-where-can-i-give-a-starstarstarstarstar-review)
2938
- [Support](#support)
3039
- [Why don't you hire me?](#why-dont-you-hire-me)
3140
- [Want to help in other way? Want to be a sponsor?](#want-to-help-in-other-way-want-to-be-a-sponsor)
41+
- [Running the Tests](#running-the-tests)
42+
- [Feedback](#feedback)
43+
- [Change log](#change-log)
44+
- [Security](#security)
3245
- [Credits](#credits)
33-
- [Contributing](#contributing)
34-
- [Reporting a bug](#reporting-a-bug)
35-
- [Creating a pull request](#creating-a-pull-request)
46+
- [License](#license)
3647

3748
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3849

3950
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).** Check this project's [introductory blog post](https://typist.tech/articles/easily-optimize-wordpress-images-using-wp-cli-and-some-binaries/) about why I built it.
4051

4152

42-
## Using
53+
## Usage
4354

4455
```bash
45-
# Optimize 10 attachments
46-
$ wp image-optimize run --limit=10
56+
# optimize specific attachments
57+
$ wp image-optimize attachment 123 223 323
4758

48-
# Optimize after thumbnail regeneration
49-
$ wp media regenerate --yes
50-
$ wp image-optimize reset --yes
51-
$ wp image-optimize run --limit=9999999
59+
# optimize certain number of attachments
60+
$ wp image-optimize batch --limit=20
61+
62+
# restore the full sized images of specific attachments.
63+
$ wp image-optimize restore 123 223 323
64+
$ wp media regenerate 123 223 323
65+
66+
# restore all full sized images and drop all meta flags
67+
$ wp image-optimize reset
68+
$ wp media regenerate
69+
70+
# learn more
71+
$ wp help image-optimize
72+
$ wp help image-optimize <subcommand>
5273
```
5374

5475
## Installing
5576

56-
Installing this package requires WP-CLI v1.4.1 or greater. Update to the latest stable release with `wp cli update`.
77+
Installing this package requires [WP-CLI v2.0.0](https://wp-cli.org/) or greater. Update to the latest stable release with `wp cli update`.
5778

5879
Once you've done so, you can install this package with:
5980

6081
```bash
61-
$ wp package install typisttech/image-optimize-command:^0.1.2
82+
$ wp package install typisttech/image-optimize-command:@stable
6283
```
6384

6485
### Optimization tools
@@ -74,7 +95,50 @@ Under the hood, `image-optimize-command` invokes [spatie/image-optimizer](https:
7495
Check spatie/image-optimizer's readme for [install instructions](https://github.com/spatie/image-optimizer#optimization-tools).
7596

7697
Note that [WordPress doesn't support SVG](https://core.trac.wordpress.org/ticket/24251) out of the box. You can omit [SVGO](https://github.com/svg/svgo).
77-
However, if you have [enabled WordPress SVG support](https://kinsta.com/blog/wordpress-svg/?kaid=CGCHYHJJJMMF), you must install SVGO. Otherwise, the command will fail.
98+
However, if you have [enabled WordPress SVG support](https://kinsta.com/blog/wordpress-svg/?kaid=CGCHYHJJJMMF) and uploaded SVGs to WordPress media library, you must install SVGO. Otherwise, the command will fail.
99+
100+
## Use Cases
101+
102+
### First use
103+
104+
This command optimize both the full sized image(the one you uploaded) and the thumbnails(WordPress auto-resize these images for you).
105+
106+
Chances are the thumbnails are missing or never generated:
107+
108+
- theme switched after upload
109+
- plugins activated after upload
110+
- deleted the images from disk but not updated WordPress' database
111+
112+
Simplest solution is to regenerate thumbnails then optimize:
113+
114+
```bash
115+
$ wp media regenerate
116+
$ wp image-optimize batch --limit=9999999
117+
```
118+
119+
### Restore the originals
120+
121+
This command backs up the full sized images before optimization. If you want to restore them:
122+
123+
```bash
124+
# optimize
125+
$ wp image-optimize attachment 123
126+
127+
# restore the full sized image
128+
$ wp image-optimize restore 123
129+
# regenerate the thumbnails from the original full sized image
130+
$ wp media regenerate 123
131+
```
132+
133+
### Migrate from image-optimize-command v0.1.x
134+
135+
Starting from v0.2, this command backs up the full sized images before optimization. To migrate from image-optimize-command v0.1.x:
136+
137+
```bash
138+
$ wp image-optimize reset
139+
$ wp media regenerate
140+
$ wp image-optimize batch --limit=9999999
141+
```
78142

79143
## FAQs
80144

@@ -84,30 +148,30 @@ Mostly applying compression, removing metadata and reducing the number of colors
84148

85149
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).
86150

87-
### Why the optimize command stopped for no reason?
151+
### Can I customize the optimization?
88152

89-
Expected outputs:
90-
```bash
91-
$ wp image-optimize run --limit=3
92-
Success: 3 unoptimized attachment(s) found. Starting...
93-
Start optimizing /app/public/wp-content/uploads/2018/01/source-150x150.gif
94-
Using optimizer: `Spatie\ImageOptimizer\Optimizers\Gifsicle`
95-
Executing `"gifsicle" -b -O3 '/app/public/wp-content/uploads/2018/01/source-150x150.gif'`
96-
...omitted...
97-
Success: 3 attachment(s) optimized
98-
99-
$ wp image-optimize run --limit=10
100-
Warning: No unoptimized attachment found. Abort!
101-
```
153+
Yes.
102154

103-
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:
104-
```bash
105-
$ wp media regenerate --yes
106-
$ wp image-optimize reset --yes
107-
$ wp image-optimize run --limit=9999999
155+
```php
156+
use Spatie\ImageOptimizer\OptimizerChain;
157+
158+
add_filter('typist_tech_image_optimized_optimizer_chain', function (OptimizerChain $optimizerChain): OptimizerChain {
159+
// Option A: Send messages to $optimizerChain.
160+
$optimizerChain->setTimeout($xxx);
161+
$optimizerChain->useLogger($yyy);
162+
$optimizerChain->addOptimizer($zzz);
163+
164+
// Option B: Make a new $optimizerChain.
165+
// See: https://github.com/spatie/image-optimizer/blob/master/src/OptimizerChainFactory.php
166+
$optimizerChain = new OptimizerChain();
167+
$optimizerChain->addOptimizer($zzz);
168+
169+
// Finally
170+
return $optimizerChain;
171+
});
108172
```
109173

110-
### Does running `wp image-optimize run` multiple times trigger multiple optimization for the same attachments?
174+
### Does running `wp image-optimize attachment / batch` multiple times trigger multiple optimization for the same attachments?
111175

112176
No.
113177

@@ -127,10 +191,38 @@ See [spatie/image-optimizer](https://github.com/spatie/image-optimizer#which-too
127191
128192
Luckily for you, Lasse M. Tvedt showed how to stop WordPress from resizing GIFs on [StackExchange](https://wordpress.stackexchange.com/a/229724).
129193

194+
### Can I use this on managed hosting?
195+
196+
No, you can't use this on managed hosting such as [Kinsta](http://bit.ly/kinsta-com) or [WP Engine](https://typist.tech/go/wp-engine) because they don't allow you to install those binaries.
197+
198+
If you must use it on managed hosting, [hire a developer](https://typist.tech/contact/) to add SaaS provider integration:
199+
200+
- [EWWW](https://typist.tech/go/ewww/)
201+
- [Kraken](https://typist.tech/go/kraken/)
202+
- [ImageOptim](https://typist.tech/go/imageoptim-api/)
203+
- [Imagify](https://typist.tech/go/imagify/)
204+
205+
206+
### Do I have to install `SVGO`?
207+
208+
Yes, if you have [enabled WordPress SVG support](https://kinsta.com/blog/wordpress-svg/?kaid=CGCHYHJJJMMF) and uploaded SVGs to WordPress media library.
209+
210+
No, if you don't have any SVGs in WordPress media library.
211+
212+
### `PHP Fatal error: Allowed memory size of 999999 bytes exhausted (tried to allocate 99 bytes)`
213+
214+
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)
215+
130216
### Does it has any limitation?
131217

132218
No, unlike other SaaS alternatives, this package runs on your server without any limitation on file sizes or monthly quota. Totally free of charge.
133219

220+
### Will you add support for older PHP versions?
221+
222+
Never! This plugin will only works on [actively supported PHP versions](https://secure.php.net/supported-versions.php).
223+
224+
Don't use it on **end of life** or **security fixes only** PHP versions.
225+
134226
### Is it for everyone?
135227

136228
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.
@@ -164,32 +256,37 @@ Ready to take freelance WordPress jobs. Contact me via the contact form [here](h
164256

165257
Contact: [Tang Rufus](mailto:[email protected])
166258

167-
## Credits
259+
## Running the Tests
168260

169-
[`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/).
261+
Run the tests:
170262

171-
Full list of contributors can be found [here](https://github.com/TypistTech/image-optimize-command/graphs/contributors).
263+
``` bash
264+
$ composer test
265+
$ composer check-style
266+
```
172267

173-
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.
268+
## Feedback
174269

175-
## Contributing
270+
**Please provide feedback!** We want to make this library useful in as many projects as possible.
271+
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 make suggestions.
272+
**No issue is too small.**
176273

177-
We appreciate you taking the initiative to contribute to this project.
274+
## Change log
178275

179-
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.
276+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
180277

181-
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.
278+
## Security
182279

183-
### Reporting a bug
280+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
184281

185-
Think you’ve found a bug? We’d love for you to help us get it fixed.
282+
## Credits
186283

187-
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.
284+
[`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/).
188285

189-
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/).
286+
Full list of contributors can be found [here](https://github.com/TypistTech/image-optimize-command/graphs/contributors).
190287

191-
### Creating a pull request
288+
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.
192289

193-
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.
290+
## License
194291

195-
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.
292+
The MIT License (MIT). Please see [License File](./LICENSE.md) for more information.

0 commit comments

Comments
 (0)