Image Optimization for Pagespeed Insights

On Debian/Ubuntu systems install required packages:

apt install -y advancecomp optipng pngcrush jpegoptim

Create a bash File in

nano /usr/local/bin/optimizeImages

and paste:

#!/bin/bash

find . -type f -iname "*.png" -exec optipng -nb -nc {} \;
find . -type f -iname "*.png" -exec advpng -z4 {} \;
find . -type f -iname "*.png" -exec pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow {} \;
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec jpegoptim -f --strip-all {} \;


Save the file (press F2 in nano). Then make the script executable:

chmod a+x /usr/local/bin/optimizeImages

The running time will be depending on the folder size. Go to your image Folder, for example

cd /var/www/myproject/web/images/

and run

optimizeImages

Now it can take a long time. Stand by … thats all!