Page 1 of 1

[HOWTO] Lossless image optimization of all VestaCP sites

Posted: Thu Dec 28, 2017 4:14 pm
by lukapaunovic
If you are looking for a way to losslessly optimize all JPG and PNG images of your sites hosted with VestaCP, search no more.

This is great because you will:
  1. Decrease disk usage
  2. Optimize page loading time
  3. Improve SEO
So let's begin.

Install jpegoptim and optipng

CentOS

Code: Select all

yum install jpegoptim optipng -y
Ubuntu

Code: Select all

sudo apt-get install jpegoptim optipng -y
Now run this simple scripts which will do all the job for you.

Code: Select all

for i in `/usr/local/vesta/bin/v-list-sys-users | awk '{if(NR>2)print}'`; do find /home/$i/web/ -name 'public_html' -type d -print0 | xargs -0 -I {} find {} -iname '*.jpg' -type f -print0 | xargs -0 -I {} sudo -H -u $i jpegoptim --strip-all {}; done
(You may want to run above again just replace *.jpg with *.jpeg

Code: Select all

for i in `/usr/local/vesta/bin/v-list-sys-users | awk '{if(NR>2)print}'`; do find /home/$i/web/ -name 'public_html' -type d -print0 | xargs -0 -I {} find {} -iname '*.png' -type f -print0 | xargs -0 -I {} sudo -H -u$i optipng {}; done
I recommend running this inside a screen especially if you have a lot of sites.

If you do not know what screen is.

The script always runs as the user owning the file, file ownership will remain correct. Also it's 100% safe to use.

Re: [HOWTO] Lossless image optimization of all VestaCP sites

Posted: Thu Apr 26, 2018 6:03 am
by moh3n
does this need to run every time after adding new images?
is there any way to add this to cron to run it rapidly?

Re: [HOWTO] Lossless image optimization of all VestaCP sites

Posted: Tue May 01, 2018 7:58 pm
by lukapaunovic
Yes, it could be added to cron. But the only flaw here is that when you run it again it will go through all already optimized images, it will detect they are optimized but that loses a lot of time.

Here is an example of a cron

create /home/images.sh with content:

Code: Select all

VESTA=/usr/local/vesta
export VESTA
    
PATH=$PATH:/usr/local/vesta/bin
export PATH


for i in `/usr/local/vesta/bin/v-list-sys-users | awk '{if(NR>2)print}'`; do find /home/$i/web/ -name 'public_html' -type d -print0 | xargs -0 -I {} find {} -iname '*.jpg' -type f -print0 | xargs -0 -I {} sudo -H -u $i jpegoptim --strip-all {}; done

for i in `/usr/local/vesta/bin/v-list-sys-users | awk '{if(NR>2)print}'`; do find /home/$i/web/ -name 'public_html' -type d -print0 | xargs -0 -I {} find {} -iname '*.png' -type f -print0 | xargs -0 -I {} sudo -H -u$i optipng {}; done
Then chmod +x /home/cron.sh

Then add to crontab -e

Code: Select all

0 0 * * * /usr/bin/bash /home/images.sh