Optimize Images for Faster Loading - Ubuntu
03-04-2016, 11:59 AM
(This post was last modified: 03-05-2016, 09:31 AM by pinguy.)
To optimize all the images in the public_html folder once a month. Run this:
F3 will save the file and Ctrl+X will exit nano.
A bit of a warning. OptiPNG is very slow. May take a few hours to complete if you have a large amount of .pngs
Code:
apt-get install jpegoptim optipng
Code:
nano /etc/cron.monthly/optimgs
Code:
#!/bin/bash
cd /var/sentora/hostdata/zadmin/public_html
find -iregex '.*\.\(png\)$' -print0 | xargs -0 optipng -o7 -preserve
find -iregex '.*\.\(jpg\|jpeg\)$' -print0 | xargs -0 jpegoptim --max=90 --strip-all –all-progressive --preserve --totals
find -iregex '.*\.\(jpg\|png\|jpeg\)$' -exec chmod 644 {} \;
find -iregex '.*\.\(jpg\|png\|jpeg\)$' -exec chown www-data:www-data {} \;
exit 0
F3 will save the file and Ctrl+X will exit nano.
Code:
sudo chmod +x '/etc/cron.monthly/optimgs'
sudo chmod 755 '/etc/cron.monthly/optimgs'
A bit of a warning. OptiPNG is very slow. May take a few hours to complete if you have a large amount of .pngs