Optimize Images for WordPress with the `jpegoptim`

20. March 2018 Note ,

I am trying to make my blog faster, so I wrote a code to optimize *.jpg images with the jpegoptim.


jpegoptim is one of a command line tool, so this code is using exec().


Following picture was optimized from 235.18KB to 124.88KB.



  • If you want to use this code, you have to install jpegoptim into your server and please change the path to jpegoptim in this code.
  • This code is optimizing only resized ( and edited?) images, so the full-size images won’t be optimized.
  • If you run WP-CLI command wp media generate, this code will be fired too.
  • The jpeg quality is 60, so I guess you feel it is low. Please edit it if you need.
  • I don’t want to be customizable the path to the jpegoptim for security reason, so I don’t publish this as a WordPress plugin. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *