From 673889e740a51f931138575dc088b8fb93f59012 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Thu, 29 Aug 2019 18:27:51 +0200 Subject: [PATCH 01/13] Fix bash -c --- .editorconfig | 9 +++++++++ optimize.sh | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1e114ad --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false \ No newline at end of file diff --git a/optimize.sh b/optimize.sh index fa2689f..ff4b06e 100755 --- a/optimize.sh +++ b/optimize.sh @@ -193,10 +193,10 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt -quiet "{}" -o "{}.webp"; }' + bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }" else find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt -quiet "{}" -o "{}.webp"; }' + bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }" fi echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' @@ -206,10 +206,10 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -quiet -q 82 -mt "{}" -o "{}.webp"; }' + bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp'; }" else find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -quiet -q 82 -mt "{}" -o "{}.webp"; }' + bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp'; }" fi echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" From c1b6708ec299918f119e8ece1a12cd62dbe6413d Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Fri, 6 Dec 2019 01:02:27 +0100 Subject: [PATCH 02/13] Update script --- optimize.sh | 2 +- scripts/install-optipng.sh | 5 +++-- scripts/install-webp.sh | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/optimize.sh b/optimize.sh index ff4b06e..69e2a95 100755 --- a/optimize.sh +++ b/optimize.sh @@ -7,7 +7,7 @@ # Author: VirtuBox # License: M.I.T # ---------------------------------------------------------------------------- -# Version 1.1 - 2019-07-12 +# Version 1.1 - 2019-09-24 # ---------------------------------------------------------------------------- CSI='\033[' diff --git a/scripts/install-optipng.sh b/scripts/install-optipng.sh index be8d60f..d1d6426 100644 --- a/scripts/install-optipng.sh +++ b/scripts/install-optipng.sh @@ -24,7 +24,8 @@ curl -sL "$OPTIPNGLATEST" | tar -I pigz -xf - cd optipng-* || exit 1 # configure and compile optipng -./configure --prefix=/usr +./configure make -j "$(nproc)" strip --strip-unneeded /usr/local/src/optipng-*/src/optipng/optipng -make install \ No newline at end of file +make install +ldconfig \ No newline at end of file diff --git a/scripts/install-webp.sh b/scripts/install-webp.sh index e4eee37..31410c8 100644 --- a/scripts/install-webp.sh +++ b/scripts/install-webp.sh @@ -24,7 +24,8 @@ curl -sL "https:$LATEST_WEBP" | tar -I pigz -xf - cd libwebp-* || exit # configure libwebp and launch compilation -./configure --prefix=/usr +./configure make -j "$(nproc)" strip --strip-unneeded /usr/local/src/libwebp-*/examples/.libs/{cwebp,dwebp} make install +ldconfig From 54457264a4d2fa08427a3785418e72fad54b01f6 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sun, 8 Dec 2019 02:35:34 +0100 Subject: [PATCH 03/13] update travis --- .travis.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 90b22f3..7900a62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ -sudo: required +os: linux dist: bionic -language: bash +language: shell cache: apt: true @@ -10,6 +10,7 @@ git: quiet: true before_script: + - sudo apt-get install tree -y - sudo apt-get -qq autoremove --purge addons: apt: @@ -17,8 +18,8 @@ addons: script: - sudo apt-get install jpegoptim -y - - sudo bash scripts/install-webp.sh - - sudo bash scripts/install-optipng.sh + - sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1 + - sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1 - sudo cp optimize.sh /usr/local/bin/img-optimize - sudo chmod 755 /usr/local/bin/img-optimize - /usr/local/bin/img-optimize --all @@ -26,5 +27,6 @@ script: - /usr/local/bin/img-optimize --png -q - rm images/*.webp - /usr/local/bin/img-optimize --webp + - tree -L 2 . - rm images/*.webp - /usr/local/bin/img-optimize --webp --quiet \ No newline at end of file From 1b458c423c6c2ba2eaf1317ecdaedd1ec0956f0b Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sun, 8 Dec 2019 14:52:02 +0100 Subject: [PATCH 04/13] update readme --- README.md | 240 +++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index 4e8dfaa..1368cbc 100644 --- a/README.md +++ b/README.md @@ -1,120 +1,120 @@ -# Image optimization bash script - -![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) - -[![Travis](https://img.shields.io/travis/virtubox/img-optimize.svg)](https://travis-ci.org/VirtuBox/img-optimize) ![GitHub](https://img.shields.io/github/license/VirtuBox/img-optimize.svg) ![GitHub release](https://img.shields.io/github/release/VirtuBox/img-optimize.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/VirtuBox/img-optimize.svg) ![Github stars](https://img.shields.io/github/stars/VirtuBox/img-optimize.svg) - -## Prerequisite - -- jpegoptim for jpg optimization -- optipng for png optimization -- cwebp for WebP conversion - -### From APT repositories - -Debian/Ubuntu : - -```bash -sudo apt install jpegoptim optipng webp -y -``` - -Centos 7 : - -```bash -sudo yum install optipng jpegoptim libwebp-tools -y -``` - -### Compile the latest release (optipng & libwebp) - -For Debian/Ubuntu (available in scripts folder) : - -```bash -# optipng -curl -sL git.io/fjddn | sudo -E bash - -# libwebp -curl -sL git.io/fjdd6 | sudo -E bash -``` - --------------------------------------------------------------------------------- - -## Installation - -1) Clone the repository - -```bash -git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize -``` - -2) Install the script - -**Method 1** : Add an alias in .bashrc - -With this method img-optimize can only be used by the current user - -```bash -echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc -source $HOME/.bashrc -``` - -**Method 2** : Add an alias to the script in /usr/local/bin - -With this method img-optimize can be used by all users - -```bash -sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -sudo chmod +x /usr/local/bin/img-optimize -``` - -## Usage - -```bash -Bash script to optimize your images and convert them in WebP -Usage: img-optimize [options] -If images path isn't defined, img-optimize will use the current directory - Options: - --jpg ..... optimize all jpg images - --png ..... optimize all png images - --webp ..... convert all images in webp - --nowebp ..... optimize all png & jpg images - --all ..... optimize all images (png + jpg + webp) - -i, --interactive ..... run img-optimize in interactive mode - -q, --quiet ..... run image optimization quietly - 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 : - -```bash -git -C $HOME/.img-optimize pull -``` - -## Setup daily cronjob - -You just have to copy the scripts to /etc/cron.daily : - -```bash -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](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website) - -- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) +# Image optimization bash script + +![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) + +[![Travis](https://travis-ci.org/VirtuBox/img-optimize.svg?branch=master)](https://travis-ci.org/VirtuBox/img-optimize) ![GitHub](https://img.shields.io/github/license/VirtuBox/img-optimize.svg) ![GitHub release](https://img.shields.io/github/release/VirtuBox/img-optimize.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/VirtuBox/img-optimize.svg) ![Github stars](https://img.shields.io/github/stars/VirtuBox/img-optimize.svg) + +## Prerequisite + +- jpegoptim for jpg optimization +- optipng for png optimization +- cwebp for WebP conversion + +### From APT repositories + +Debian/Ubuntu : + +```bash +sudo apt install jpegoptim optipng webp -y +``` + +Centos 7 : + +```bash +sudo yum install optipng jpegoptim libwebp-tools -y +``` + +### Compile the latest release (optipng & libwebp) + +For Debian/Ubuntu (available in scripts folder) : + +```bash +# optipng +curl -sL git.io/fjddn | sudo -E bash + +# libwebp +curl -sL git.io/fjdd6 | sudo -E bash +``` + +-------------------------------------------------------------------------------- + +## Installation + +1) Clone the repository + +```bash +git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize +``` + +2) Install the script + +**Method 1** : Add an alias in .bashrc + +With this method img-optimize can only be used by the current user + +```bash +echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc +source $HOME/.bashrc +``` + +**Method 2** : Add an alias to the script in /usr/local/bin + +With this method img-optimize can be used by all users + +```bash +sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize +sudo chmod +x /usr/local/bin/img-optimize +``` + +## Usage + +```bash +Bash script to optimize your images and convert them in WebP +Usage: img-optimize [options] +If images path isn't defined, img-optimize will use the current directory + Options: + --jpg ..... optimize all jpg images + --png ..... optimize all png images + --webp ..... convert all images in webp + --nowebp ..... optimize all png & jpg images + --all ..... optimize all images (png + jpg + webp) + -i, --interactive ..... run img-optimize in interactive mode + -q, --quiet ..... run image optimization quietly + 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 : + +```bash +git -C $HOME/.img-optimize pull +``` + +## Setup daily cronjob + +You just have to copy the scripts to /etc/cron.daily : + +```bash +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](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website) + +- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) From 32e33d1f34b3ed453a2f466c4e325add07db30be Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sun, 18 Oct 2020 18:49:56 +0200 Subject: [PATCH 05/13] Add avif support --- .gitattributes | 39 ++++++++++++++++++++++++++++++++++++++ .travis.yml | 2 ++ optimize.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b8caf52 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,39 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Documents +*.md text eol=lf +*.tex text diff=tex +*.adoc text +*.textile text +*.mustache text eol=lf +*.csv text +*.tab text +*.tsv text +*.txt text +*.sql text + +# Scripts +*.bash text eol=lf +*.fish text eol=lf +*.sh text eol=lf + +# Source files +# ============ +*.pxd text diff=python +*.py text diff=python +*.py3 text diff=python +*.pyc text diff=python +*.pyd text diff=python +*.pyo text diff=python +*.pyw text diff=python +*.pyx text diff=python +*.pyz text diff=python + + +# +# Exclude files from exporting +# + +.gitattributes export-ignore +.gitignore export-ignore \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 7900a62..8aabbc5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,8 @@ script: - sudo apt-get install jpegoptim -y - sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1 - sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1 + - sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 + - sudo chmod +x /usr/local/bin/avif - sudo cp optimize.sh /usr/local/bin/img-optimize - sudo chmod 755 /usr/local/bin/img-optimize - /usr/local/bin/img-optimize --all diff --git a/optimize.sh b/optimize.sh index 69e2a95..3aa1f7e 100755 --- a/optimize.sh +++ b/optimize.sh @@ -7,7 +7,7 @@ # Author: VirtuBox # License: M.I.T # ---------------------------------------------------------------------------- -# Version 1.1 - 2019-09-24 +# Version 1.2 - 2020-10-18 # ---------------------------------------------------------------------------- CSI='\033[' @@ -27,8 +27,9 @@ _help() { echo " --jpg ..... optimize all jpg images" echo " --png ..... optimize all png images" echo " --webp ..... convert all images in webp" + echo " --avif ..... convert all images in avif" echo " --nowebp ..... optimize all png & jpg images" - echo " --all ..... optimize all images (png + jpg + webp)" + echo " --all ..... optimize all images (png + jpg + webp +avif)" echo " -i, --interactive ..... run img-optimize in interactive mode" echo " -q, --quiet ..... run image optimization quietly" echo " --path ..... define images path" @@ -68,10 +69,14 @@ else --webp) WEBP_OPTIMIZATION="y" ;; + --avif) + AVIF_OPTIMIZATION="y" + ;; --all) PNG_OPTIMIZATION="y" JPG_OPTIMIZATION="y" WEBP_OPTIMIZATION="y" + AVIF_OPTIMIZATION="y" ;; -i | --interactive) INTERACTIVE_MODE="1" @@ -143,6 +148,16 @@ if [ "$INTERACTIVE_MODE" = "1" ]; then echo "" echo "" fi + if [ -z "$AVIF_OPTIMIZATION" ]; then + echo "" + echo "Do you want to convert all jpg & png images to WebP in $IMG_PATH ? (y/n)" + while [[ $AVIF_OPTIMIZATION != "y" && $AVIF_OPTIMIZATION != "n" ]]; do + echo "Select an option [y/n]: " + read -r AVIF_OPTIMIZATION + done + echo "" + echo "" + fi fi ################################## @@ -215,6 +230,38 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' fi +if [ "$AVIF_OPTIMIZATION" = "y" ]; then + [ -z "$(command -v avif)" ] && { + echo "Error: avif isn't installed" + exit 1 + } + # convert png to webp + echo -ne ' png to avif conversion [..]\r' + cd "$IMG_PATH" || exit 1 + if [ -n "$FIND_ARGS" ]; then + find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + else + find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \ + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + fi + echo -ne " png to avif conversion [${CGREEN}OK${CEND}]\\r" + echo -ne '\n' + + # convert jpg to webp + echo -ne ' jpg to avif conversion [..]\r' + cd "$IMG_PATH" || exit 1 + if [ -n "$FIND_ARGS" ]; then + find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + else + find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \ + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + fi + + echo -ne " jpg to avif conversion [${CGREEN}OK${CEND}]\\r" + echo -ne '\n' +fi # We're done ! echo "" From 716c541168861764abf87a4470ed774de609e0d8 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sun, 18 Oct 2020 19:11:16 +0200 Subject: [PATCH 06/13] Update travis --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8aabbc5..101aafe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,9 @@ script: - sudo chmod +x /usr/local/bin/avif - sudo cp optimize.sh /usr/local/bin/img-optimize - sudo chmod 755 /usr/local/bin/img-optimize - - /usr/local/bin/img-optimize --all + - ls -alh images/ + - /usr/local/bin/img-optimize --all -q + - ls -alh images/ - /usr/local/bin/img-optimize -jpg -q - /usr/local/bin/img-optimize --png -q - rm images/*.webp From a77944a525f8586818f2c938431443709b9a16d7 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Mon, 19 Oct 2020 13:59:16 +0200 Subject: [PATCH 07/13] Remove avif file if conversion fail --- optimize.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/optimize.sh b/optimize.sh index 3aa1f7e..153be4e 100755 --- a/optimize.sh +++ b/optimize.sh @@ -148,7 +148,7 @@ if [ "$INTERACTIVE_MODE" = "1" ]; then echo "" echo "" fi - if [ -z "$AVIF_OPTIMIZATION" ]; then + if [ -z "$AVIF_OPTIMIZATION" ]; then echo "" echo "Do you want to convert all jpg & png images to WebP in $IMG_PATH ? (y/n)" while [[ $AVIF_OPTIMIZATION != "y" && $AVIF_OPTIMIZATION != "n" ]]; do @@ -240,10 +240,10 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }" else find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }" fi echo -ne " png to avif conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' @@ -253,10 +253,10 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; } " else find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif'; }" + bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }" fi echo -ne " jpg to avif conversion [${CGREEN}OK${CEND}]\\r" From cbe45eea8e46e7369b352a43d65e663b0d890539 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Mon, 19 Oct 2020 14:07:33 +0200 Subject: [PATCH 08/13] Fix travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 101aafe..275dd96 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ script: - ls -alh images/ - /usr/local/bin/img-optimize --all -q - ls -alh images/ - - /usr/local/bin/img-optimize -jpg -q + - /usr/local/bin/img-optimize --jpg -q - /usr/local/bin/img-optimize --png -q - rm images/*.webp - /usr/local/bin/img-optimize --webp From 5e1c6d1da2148ab514782c5008c48949efb23112 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Tue, 10 Nov 2020 18:49:15 +0100 Subject: [PATCH 09/13] Update img-optimize * avif support --- CHANGELOG.md | 6 ++++++ README.md | 12 ++++++++++-- optimize.sh | 25 +++++++++++++++---------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f929de8..96da192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +## [V2.0] - 2020-11-10 + ### Added - quiet mode - option --no-run-if-empty added to xargs - scripts to compile optipng & libwebp from source +- Avif (AV1 Image Format) support ### Changed +- JPG are optimized as progressive JPG +- WebP and Avif newly created images are deleted in case of failure during conversion + ## [V1.1] - 2019-04-05 ### Added diff --git a/README.md b/README.md index 1368cbc..e8643a4 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ - jpegoptim for jpg optimization - optipng for png optimization - cwebp for WebP conversion +- go-avif for Avif conversion ### From APT repositories @@ -36,6 +37,13 @@ curl -sL git.io/fjddn | sudo -E bash curl -sL git.io/fjdd6 | sudo -E bash ``` +### Go-Avif installation + +```bash +sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 +sudo chmod +x /usr/local/bin/avif +``` + -------------------------------------------------------------------------------- ## Installation @@ -87,7 +95,7 @@ Examples: img-optimize --jpg /var/www/images ``` -## Update the script +### Update the script To update the script, just run : @@ -95,7 +103,7 @@ To update the script, just run : git -C $HOME/.img-optimize pull ``` -## Setup daily cronjob +### Setup daily cronjob You just have to copy the scripts to /etc/cron.daily : diff --git a/optimize.sh b/optimize.sh index 153be4e..166c4bc 100755 --- a/optimize.sh +++ b/optimize.sh @@ -7,7 +7,7 @@ # Author: VirtuBox # License: M.I.T # ---------------------------------------------------------------------------- -# Version 1.2 - 2020-10-18 +# Version 2.0 - 2020-11-10 # ---------------------------------------------------------------------------- CSI='\033[' @@ -28,8 +28,9 @@ _help() { echo " --png ..... optimize all png images" echo " --webp ..... convert all images in webp" echo " --avif ..... convert all images in avif" - echo " --nowebp ..... optimize all png & jpg images" - echo " --all ..... optimize all images (png + jpg + webp +avif)" + echo " --std ..... optimize all png & jpg images" + echo " --next ..... convert all images in webp & avif" + echo " --all ..... optimize all images (png + jpg + webp + avif)" echo " -i, --interactive ..... run img-optimize in interactive mode" echo " -q, --quiet ..... run image optimization quietly" echo " --path ..... define images path" @@ -61,11 +62,15 @@ else --png) PNG_OPTIMIZATION="y" ;; - --nowebp) + --std) JPG_OPTIMIZATION="y" PNG_OPTIMIZATION="y" WEBP_OPTIMIZATION="n" + AVIF_OPTIMIZATION="n" ;; + --next) + AVIF_OPTIMIZATION="y" + WEBP_OPTIMIZATION="y" --webp) WEBP_OPTIMIZATION="y" ;; @@ -173,9 +178,9 @@ if [ "$JPG_OPTIMIZATION" = "y" ]; then echo -ne ' jpg optimization [..]\r' cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then - find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" --preserve --strip-all -m82 + find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -s --all-progressive -m82 else - find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" --preserve --strip-all -m82 + find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -s -m82 --all-progressive fi echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r" @@ -221,10 +226,10 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp'; }" + bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp || rm -f '{}.webp''; }" else find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \ - bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp'; }" + bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp' || rm -f '{}.webp'; }" fi echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" @@ -235,7 +240,7 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then echo "Error: avif isn't installed" exit 1 } - # convert png to webp + # convert png to avif echo -ne ' png to avif conversion [..]\r' cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then @@ -248,7 +253,7 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then echo -ne " png to avif conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' - # convert jpg to webp + # convert jpg to avif echo -ne ' jpg to avif conversion [..]\r' cd "$IMG_PATH" || exit 1 if [ -n "$FIND_ARGS" ]; then From f445537361c520f98feef3234076e69bbe71f094 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Tue, 10 Nov 2020 20:30:31 +0100 Subject: [PATCH 10/13] Fix typo --- optimize.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/optimize.sh b/optimize.sh index 166c4bc..742e4d1 100755 --- a/optimize.sh +++ b/optimize.sh @@ -71,6 +71,7 @@ else --next) AVIF_OPTIMIZATION="y" WEBP_OPTIMIZATION="y" + ;; --webp) WEBP_OPTIMIZATION="y" ;; From ec68a935bfd4a415ea8fdb419a274b621287ab7e Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Wed, 11 Nov 2020 12:41:24 +0100 Subject: [PATCH 11/13] Create main.yml --- .github/workflows/main.yml | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..553146f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,53 @@ +name: CI + +on: + push: + branches: + - develop + - master + pull_request: + branches: + - master + release: + types: [published] + schedule: + - cron: '0 0 * * 0' + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + # Runs a set of commands using the runners shell + - name: Install prerequesites + run: | + sudo apt-get update + sudo apt-get install tree -y + sudo apt-get -qq autoremove --purge + sudo apt-get install jpegoptim -y + sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1 + sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1 + sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 + sudo chmod +x /usr/local/bin/avif + sudo cp optimize.sh /usr/local/bin/img-optimize + sudo chmod 755 /usr/local/bin/img-optimize + + - name: Optimize Images + run: | + ls -alh images/ + /usr/local/bin/img-optimize --all -q + ls -alh images/ + /usr/local/bin/img-optimize --jpg -q + /usr/local/bin/img-optimize --png -q + rm images/*.webp + /usr/local/bin/img-optimize --webp + tree -L 2 . + rm images/*.webp + /usr/local/bin/img-optimize --webp --quiet From e7d9dee89016e754f6f0ad2c7488e5fb42c13376 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Wed, 11 Nov 2020 12:47:08 +0100 Subject: [PATCH 12/13] Update main.yml --- .github/workflows/main.yml | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 553146f..89ca79e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,8 +29,6 @@ jobs: - name: Install prerequesites run: | sudo apt-get update - sudo apt-get install tree -y - sudo apt-get -qq autoremove --purge sudo apt-get install jpegoptim -y sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1 sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1 @@ -42,12 +40,10 @@ jobs: - name: Optimize Images run: | ls -alh images/ - /usr/local/bin/img-optimize --all -q + /usr/local/bin/img-optimize --avif ls -alh images/ - /usr/local/bin/img-optimize --jpg -q - /usr/local/bin/img-optimize --png -q - rm images/*.webp /usr/local/bin/img-optimize --webp - tree -L 2 . - rm images/*.webp - /usr/local/bin/img-optimize --webp --quiet + ls -alh images/ + rm -f images/*.webp images/*.avif + /usr/local/bin/img-optimize --all + ls -alh images/ From e91fee0836fceb4fd16f727c4a1acaa51681ae65 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Wed, 11 Nov 2020 12:56:44 +0100 Subject: [PATCH 13/13] Update Changelog and Readme --- CHANGELOG.md | 1 + README.md | 17 ++-- docs/README.md | 253 ++++++++++++++++++++++++++----------------------- 3 files changed, 145 insertions(+), 126 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96da192..09c1f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - option --no-run-if-empty added to xargs - scripts to compile optipng & libwebp from source - Avif (AV1 Image Format) support +- Avoid optimizing the same image at the same moment [PR #12](https://github.com/VirtuBox/img-optimize/pull/12) ### Changed diff --git a/README.md b/README.md index e8643a4..fa78b7a 100644 --- a/README.md +++ b/README.md @@ -81,18 +81,23 @@ Bash script to optimize your images and convert them in WebP Usage: img-optimize [options] If images path isn't defined, img-optimize will use the current directory Options: - --jpg ..... optimize all jpg images - --png ..... optimize all png images - --webp ..... convert all images in webp - --nowebp ..... optimize all png & jpg images - --all ..... optimize all images (png + jpg + webp) + --jpg ..... optimize all jpg images + --png ..... optimize all png images + --webp ..... convert all images in webp + --avif ..... convert all images in avif + --std ..... optimize all png & jpg images + --next ..... convert all images in webp & avif + --all ..... optimize all images (png + jpg + webp + avif) -i, --interactive ..... run img-optimize in interactive mode -q, --quiet ..... run image optimization quietly + --path ..... define images path Other options : -h, --help, help ... displays this help information + --cmin [+|-] ... File's status was last changed n minutes ago. + act find cmin argument (+n : greater than n, -n : less than n, n : exactly n) Examples: optimize all jpg images in /var/www/images - img-optimize --jpg /var/www/images + img-optimize --jpg --path /var/www/images ``` ### Update the script diff --git a/docs/README.md b/docs/README.md index 4e8dfaa..fa78b7a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,120 +1,133 @@ -# Image optimization bash script - -![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) - -[![Travis](https://img.shields.io/travis/virtubox/img-optimize.svg)](https://travis-ci.org/VirtuBox/img-optimize) ![GitHub](https://img.shields.io/github/license/VirtuBox/img-optimize.svg) ![GitHub release](https://img.shields.io/github/release/VirtuBox/img-optimize.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/VirtuBox/img-optimize.svg) ![Github stars](https://img.shields.io/github/stars/VirtuBox/img-optimize.svg) - -## Prerequisite - -- jpegoptim for jpg optimization -- optipng for png optimization -- cwebp for WebP conversion - -### From APT repositories - -Debian/Ubuntu : - -```bash -sudo apt install jpegoptim optipng webp -y -``` - -Centos 7 : - -```bash -sudo yum install optipng jpegoptim libwebp-tools -y -``` - -### Compile the latest release (optipng & libwebp) - -For Debian/Ubuntu (available in scripts folder) : - -```bash -# optipng -curl -sL git.io/fjddn | sudo -E bash - -# libwebp -curl -sL git.io/fjdd6 | sudo -E bash -``` - --------------------------------------------------------------------------------- - -## Installation - -1) Clone the repository - -```bash -git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize -``` - -2) Install the script - -**Method 1** : Add an alias in .bashrc - -With this method img-optimize can only be used by the current user - -```bash -echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc -source $HOME/.bashrc -``` - -**Method 2** : Add an alias to the script in /usr/local/bin - -With this method img-optimize can be used by all users - -```bash -sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -sudo chmod +x /usr/local/bin/img-optimize -``` - -## Usage - -```bash -Bash script to optimize your images and convert them in WebP -Usage: img-optimize [options] -If images path isn't defined, img-optimize will use the current directory - Options: - --jpg ..... optimize all jpg images - --png ..... optimize all png images - --webp ..... convert all images in webp - --nowebp ..... optimize all png & jpg images - --all ..... optimize all images (png + jpg + webp) - -i, --interactive ..... run img-optimize in interactive mode - -q, --quiet ..... run image optimization quietly - 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 : - -```bash -git -C $HOME/.img-optimize pull -``` - -## Setup daily cronjob - -You just have to copy the scripts to /etc/cron.daily : - -```bash -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](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website) - -- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) +# Image optimization bash script + +![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) + +[![Travis](https://travis-ci.org/VirtuBox/img-optimize.svg?branch=master)](https://travis-ci.org/VirtuBox/img-optimize) ![GitHub](https://img.shields.io/github/license/VirtuBox/img-optimize.svg) ![GitHub release](https://img.shields.io/github/release/VirtuBox/img-optimize.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/VirtuBox/img-optimize.svg) ![Github stars](https://img.shields.io/github/stars/VirtuBox/img-optimize.svg) + +## Prerequisite + +- jpegoptim for jpg optimization +- optipng for png optimization +- cwebp for WebP conversion +- go-avif for Avif conversion + +### From APT repositories + +Debian/Ubuntu : + +```bash +sudo apt install jpegoptim optipng webp -y +``` + +Centos 7 : + +```bash +sudo yum install optipng jpegoptim libwebp-tools -y +``` + +### Compile the latest release (optipng & libwebp) + +For Debian/Ubuntu (available in scripts folder) : + +```bash +# optipng +curl -sL git.io/fjddn | sudo -E bash + +# libwebp +curl -sL git.io/fjdd6 | sudo -E bash +``` + +### Go-Avif installation + +```bash +sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 +sudo chmod +x /usr/local/bin/avif +``` + +-------------------------------------------------------------------------------- + +## Installation + +1) Clone the repository + +```bash +git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize +``` + +2) Install the script + +**Method 1** : Add an alias in .bashrc + +With this method img-optimize can only be used by the current user + +```bash +echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc +source $HOME/.bashrc +``` + +**Method 2** : Add an alias to the script in /usr/local/bin + +With this method img-optimize can be used by all users + +```bash +sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize +sudo chmod +x /usr/local/bin/img-optimize +``` + +## Usage + +```bash +Bash script to optimize your images and convert them in WebP +Usage: img-optimize [options] +If images path isn't defined, img-optimize will use the current directory + Options: + --jpg ..... optimize all jpg images + --png ..... optimize all png images + --webp ..... convert all images in webp + --avif ..... convert all images in avif + --std ..... optimize all png & jpg images + --next ..... convert all images in webp & avif + --all ..... optimize all images (png + jpg + webp + avif) + -i, --interactive ..... run img-optimize in interactive mode + -q, --quiet ..... run image optimization quietly + --path ..... define images path + Other options : + -h, --help, help ... displays this help information + --cmin [+|-] ... File's status was last changed n minutes ago. + act find cmin argument (+n : greater than n, -n : less than n, n : exactly n) +Examples: + optimize all jpg images in /var/www/images + img-optimize --jpg --path /var/www/images +``` + +### Update the script + +To update the script, just run : + +```bash +git -C $HOME/.img-optimize pull +``` + +### Setup daily cronjob + +You just have to copy the scripts to /etc/cron.daily : + +```bash +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](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website) + +- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french)