This post shows how to save server disk space using images compression. The following commands do not loss the picture quality, but only remove unnecessary data from them.
! Replace ‘/var/www’ with your dir path in the commands below !
To compress all JPG images in the directory /var/www
run this command:
1 2 3 |
find /var/www -type f -name "*.jpg" | xargs jpegoptim -m85 --strip-all --all-progressive |
To compress all PNG images in the directory /var/www
run this command:
1 2 3 |
find . -type f -name "*.png" | xargs pngquant --quality=85 --ext '.png.compressed'; find . -type f -name "*.png.compressed" | xargs rename -f -v 's/.png.compressed/.png/' |
In this way you can add these commands to CRON:
1 2 3 |
0 3 * * * 0 find /var/www -type f -name "*.jpg" | xargs jpegoptim -m85 --strip-all --all-progressive |
Above task will run every Sunday at 03:00 am.
The compression consumes a lot of CPU resources, sometimes close to 100%. I recommend you perform compression when there are few visitors.