2.3 KiB
2.3 KiB
Bash script to optimize your images and convert them in WebP
Prerequisite
- jpegoptim
- optipng
- WebP
Debian/Ubuntu :
sudo apt install jpegoptim optipng webp -y
Centos 7 :
sudo yum install optipng jpegoptim libwebp-tools -y
Installation
- Download the script
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
- Add an alias in your bashrc
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $HOME/.bashrc
Usage
Usage: img-optimize [options] <images path>
If images path is not defined, img-optimize will use the current directory
Options:
--jpg <images path> ..... optimize all jpg images
--png <images path> ..... optimize all png images
--webp <images path> ..... convert all images in webp
--nowebp <images path> ..... optimize all png & jpg images
--all <images path> ..... optimize all images (png + jpg + webp)
Other options :
-h, --help, help ... displays this help information
Examples:
optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images
Update the script
To update the script, just run :
git -C $HOME/.img-optimize pull
source .bashrc
Setup daily cronjob
You copy the scripts to /etc/cron.daily :
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
Then just edit your websites path set with the variables sites
at the beginning of the cron scripts.
Warning
Conversion process can take a while, you can use tmux
to launch the script and be able to close your ssh connection without interrupting conversion. Then just use tmux attach
to login back in your tmux session.
Credits
WebP conversion script was inspired by this DigitalOcean Community Tutorial