diff --git a/README.md b/README.md index 126fcaa..8ded270 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,54 @@ -# WP-optimize +## Bash script to optimize your images and convert them in WebP -### What does the script do ? +### Prerequisite -- install jpegoptim and optipng -- optimize all your images in /var/www +* jpegoptim +* optipng +* WebP -To run the script : +Debian/Ubuntu : +```bash +sudo apt install jpegoptim optipng webp -y ``` -#For debian/Ubuntu -bash <(wget --no-check-certificate -O - https://raw.githubusercontent.com/VirtuBox/wp-optimize/master/deb-optimize.sh) -wget -qO optimize.sh -#For Centos/RedHat -cd /var/www -bash <(wget --no-check-certificate -O - https://raw.githubusercontent.com/VirtuBox/wp-optimize/master/centos-optimize.sh) +Centos 7 : +```bash +sudo yum install optipng jpegoptim libwebp-tools -y ``` + +### What does the script do + +1) optimize jpg images with jpegoptim +2) optimize png images with optipng +3) convert jpg & png images in WebP (without deleting them) + +WebP image name example for mybackground.png : mybackground.png.webp + +--- + +### Usage + +1) Download the script and make it executable + +```bash +wget https://raw.githubusercontent.com/VirtuBox/wp-optimize/master/optimize.sh +chmod +x optimize.sh +``` + +2) Launch the script and set the path of your images as first argument + +```bash +./optimize.sh /path/to/your/images +``` + +To avoid permissions issues, you can run the script with another user with sudo + +```bash +sudo -u www-data ./optimize.sh /path/to/your/images +``` + +### 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. \ No newline at end of file diff --git a/centos-optimize.sh b/centos-optimize.sh deleted file mode 100644 index 2944e2a..0000000 --- a/centos-optimize.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -echo "" -echo "Welcome to wp-optimize script" -echo "" -yum update &>/dev/null -yum install optipng jpegoptim -y &>/dev/null -find . -name *.jp* | xargs jpegoptim --strip-all -m76 -find . -iname '*.png' -print0 | xargs -0 optipng -o7 -preserve -echo "" -echo "Optimization Process done :)" -echo "" diff --git a/deb-optimize.sh b/deb-optimize.sh deleted file mode 100644 index cdbbab3..0000000 --- a/deb-optimize.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -echo "" -echo "Welcome to wp-optimize script" -echo "" -apt-get update &>/dev/null -apt-get install optipng jpegoptim -y &>/dev/null -cd /var/www -find . -name *.jp* | xargs jpegoptim --strip-all -m76 -find . -iname '*.png' -print0 | xargs -0 optipng -o7 -preserve -echo "" -echo "Optimization Process done :)" -echo "" diff --git a/optimize.sh b/optimize.sh new file mode 100644 index 0000000..ddffa47 --- /dev/null +++ b/optimize.sh @@ -0,0 +1,24 @@ +#!/bin/bash + + +# optimize jpg +find "$1" -iname "*.jp*" -print0 | xargs -0 jpegoptim --strip-all -m76 + +# optimize png +find "$1" -iname '*.png' -print0 | xargs -0 optipng -o7 -preserve + +# convert png to webp +find "$1" -iname "*.png" -print0 | xargs -0 -I {} \ +bash -c ' +webp_version="$0".webp +if [ ! -f "$webp_version" ]; then +{ cwebp -lossless {} -o {}.webp; } +fi' + +# convert jpg to webp +find "$1" -iname "*.jp*" -print0 | xargs -0 -I {} \ +bash -c ' +webp_version="$0".webp +if [ ! -f "$webp_version" ]; then +{ cwebp -lossless {} -o {}.webp; } +fi'