Optimize all images within current directory
When trying to get that A rating with Google Page Speed or GTMetrix, one of the easiest ways to earn points is through the optimization of all the PNG and JPG images within your web root. I tend to use optipng and jpegoptim for this. It gives the ability to easily lossless optimize all your images so that you make search engines and your users happy with fast sites, while still showing them good looking images.
Follow these instructions and you’re on your way to having an even faster site.
Step 1) SSH to your system
ssh username@hostname.com
Step 2) CD to directory where your images exist ( This will vary on each hosting environment )
cd /home/username/public_html/
Step 3) Run the optimization scripts
find . -iname *.jpg | xargs jpegoptim --max=90 --all-progressive --strip-all --strip-com --strip-exif --strip-iptc --strip-icc & find . -type f -iname "*.png" -print0 | xargs -I {} -0 optipng -o5 -quiet -keep -preserve -log optipng.log "{}" &
What the above does, is searches everywhere within your current directory and down, for jpeg and PNG images. Once it finds them it will optimize them and replace the larger image right in place.
Keep in mind, that this script can take minutes or even hours depending on how many images you have and how fast your server is, so your best bet is to fire this off and leave it for a while.
And that’s it! You’re on your way to having a faster WordPress site with optimized images.