> For the complete documentation index, see [llms.txt](https://learnphp.gitbook.io/learnphp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnphp.gitbook.io/learnphp/wordpress/how-to-use-the-image-optimizer-package-for-wp-cli-ok.md).

# How To Use the Image Optimizer Package for WP-CLI (ok)

https\://www\.liquidweb.com/kb/image-optimizer-package-wp-cli/

## How To Use the Image Optimizer Package for WP-CLI

Posted on July 8, 2019 by [Luke Cavanagh](https://www.liquidweb.com/kb/author/lcavanagh/)Category: [Common Fixes](https://www.liquidweb.com/kb/category/common-fixes/) | Tags: [Image, ](https://www.liquidweb.com/kb/tag/image/)[optimization, ](https://www.liquidweb.com/kb/tag/optimization/)[Performance, ](https://www.liquidweb.com/kb/tag/performance/)[speed, ](https://www.liquidweb.com/kb/tag/speed/)[WordPress](https://www.liquidweb.com/kb/tag/wordpress/)Reading Time: 2 minutes

There will be many times when you will need to optimize all images in a site media library. If you are familiar with using WP-CLI, then there is a very handy package which can be installed. The package is called “image-optimize” and it will simplify the process of getting your images ready for web hosting.

This package is not for “managed hosts” since the libraries needed will not be able to be installed having without [root access](https://www.liquidweb.com/kb/how-to-change-the-root-password-in-whm/) and it can be CPU resource intensive.

### Preparing to Run Commands

The package for WP-CLI is called [image-optimize](https://github.com/TypistTech/image-optimize-command). To be able to use this package, you will need to [login to your site’s server](https://help.liquidweb.com/s/article/Logging-into-Your-Server-via-Secure-Shell-SSH) and update WP-CLI. You can update WP-CLI by running the following command:

`wp cli update`

Next, you will need to install a number of libraries that the package uses to optimizes jpeg, png and gif images with these commands:

`sudo apt-get install jpegoptim`\
`sudo apt-get install optipng`\
`sudo apt-get install pngquant`\
`sudo apt-get install gifsicle`

Now you can install the stable version of the image-optimize package with this command:

`wp package install typisttech/image-optimize-command:@stable`

### Optimizing Site Images

The following are examples of the commands to run after a WordPress core update:

`wp image-optimize mu-plugins`\
`wp image-optimize plugins`\
`wp image-optimize themes`\
`wp image-optimize wp-admin`\
`wp image-optimize wp-includes`

You can use this command to regenerate all thumbnails on a site.

`wp media regenerate --yes`\
You may need to limit how many images that image-optimize will process in a single back. To limit the batch size,  you just need to add the –limit flag to the end of the batch command and specify the amount, as shown in these examples:

`wp image-optimize batch --limit=500`\
`wp image-optimize batch --limit=1000`\
`wp image-optimize batch --limit=2500`\
`wp image-optimize batch --limit=5000`\
When using the image-optimize WP-CLI command, server CPU usage may be intensive, so run the batch commands in smaller sizes during the off hours times on your site. You can track CPU usage whilst running a batch optimize command by using [htop](https://hisham.hm/htop/). You can install and run htop using the following commands:

`sudo apt-get install htop`\
`htop`

To use htop to monitor server load, keep a terminal window open while the batch optimize command is running in another terminal window. In our testing, the CPU usage was not too high.

`1.61GB/3.74GB Memory usage`\
`180M - 3.86GB Swap`

### Restoring Optimized Images

Before images are optimized backup versions are created, which means that you can restore at any time to a backup file and replace out the optimized version.

For example, Attachment 123 was optimized using this command:\
`wp image-optimize attachment 123`

To restore the attachment for 123 the command to run would be:\
`wp image-optimize restore 123`

You can use the wp media regenerate command to regenerate a specific media file.\
`wp media regenerate 123`

Being able to  optimize the images in your WordPress sites media library will reduce the amount of storage needed for your site. Optimization will also improve the speed and performance of your site for visitors, improving user experience and satisfaction.

#### Related Articles:

* [How to Fix Typical WordPress Errors](https://www.liquidweb.com/kb/locating-wordpress-error-logs/)
* [How to Edit Your Hosts File in Windows 10](https://www.liquidweb.com/kb/edit-host-file-windows-10/)
* [How to Solve the VSFTPD 500 OOPS Error](https://www.liquidweb.com/kb/error-500-oops-vsftpd-refusing-to-run-with-writable-root-inside-chroot-solved/)
* [How to Upgrade Ubuntu 16.04 to Ubuntu 18.04](https://www.liquidweb.com/kb/how-to-upgrade-ubuntu-16-04-to-ubuntu-18-04/)
* [How to Solve the Upgrade Ubuntu Install Updates Error](https://www.liquidweb.com/kb/troubleshooting-please-install-available-updates-release-upgrading/)
* [\[SOLVED\] Apache Error: No matching DirectoryIndex](https://www.liquidweb.com/kb/apache-error-no-matching-directoryindex-index-html-found-solved/)
