diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d7b71bf --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,25 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), + +## [Unreleased] + +### Changed + +## [V1.1] - 2019-04-05 + +### Added + +- Interactive mode with `img-optimize -i` +- check if optipng, jpegoptim or cwebp executable before optimization +- additional install method + +### Fixed + +- images path replaced by current directory if empty + +## [v1.0] - 2019-03-20 + +- Initial release diff --git a/README.md b/README.md index 0e955d5..656f19d 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ -## Bash script to optimize your images and convert them in WebP +# Image optimization bash script ![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) -### Prerequisite +![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) -* jpegoptim -* optipng -* WebP +## Prerequisite + +- jpegoptim for jpg optimization +- optipng for png optimization +- cwebp for WebP conversion Debian/Ubuntu : @@ -14,58 +16,75 @@ Debian/Ubuntu : sudo apt install jpegoptim optipng webp -y ``` -Centos 7 : +Centos 7 : ```bash sudo yum install optipng jpegoptim libwebp-tools -y ``` ---- +-------------------------------------------------------------------------------- -### Installation +## Installation -1) Download the script +1) Clone the repository ```bash git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize ``` -2) Add an alias in your bashrc +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 ``` -### Usage +**Method 2** : Copy the script in /usr/local/bin + +With this method img-optimize can be used by all users + +```bash +sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize +``` + +## Usage ```bash Usage: img-optimize [options] -If images path is not defined, img-optimize will use the current directory +If images path is empty, 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 Other options : -h, --help, help ... displays this help information -Examples: +Example: optimize all jpg images in /var/www/images img-optimize --jpg /var/www/images ``` -### Update the script +## Update the script To update the script, just run : ```bash git -C $HOME/.img-optimize pull +``` -source .bashrc +If you previously used the 2nd install method, you have to copy the script again to update it + +```bash +sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -f ``` -### Setup daily cronjob +## Setup daily cronjob You copy the scripts to /etc/cron.daily : @@ -79,10 +98,12 @@ 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 +## 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 +## 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) -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) \ No newline at end of file +- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) diff --git a/docs/README.md b/docs/README.md index 9b7e4aa..656f19d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,12 +1,14 @@ -## Bash script to optimize your images and convert them in WebP +# Image optimization bash script ![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) -### Prerequisite +![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) -* jpegoptim -* optipng -* WebP +## Prerequisite + +- jpegoptim for jpg optimization +- optipng for png optimization +- cwebp for WebP conversion Debian/Ubuntu : @@ -14,58 +16,75 @@ Debian/Ubuntu : sudo apt install jpegoptim optipng webp -y ``` -Centos 7 : +Centos 7 : ```bash sudo yum install optipng jpegoptim libwebp-tools -y ``` ---- +-------------------------------------------------------------------------------- -### Installation +## Installation -1) Download the script +1) Clone the repository ```bash git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize ``` -2) Add an alias in your bashrc +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 ``` -### Usage +**Method 2** : Copy the script in /usr/local/bin + +With this method img-optimize can be used by all users ```bash -Usage: img-optimize [options] +sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize +``` + +## Usage + +```bash +Usage: img-optimize [options] +If images path is empty, 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 + --nowebp ..... optimize all png & jpg images + --all ..... optimize all images (png + jpg + webp) + -i, --interactive ... run img-optimize in interactive mode Other options : -h, --help, help ... displays this help information -Examples: +Example: optimize all jpg images in /var/www/images img-optimize --jpg /var/www/images - ``` -### Update the script +## Update the script To update the script, just run : ```bash git -C $HOME/.img-optimize pull +``` -source .bashrc +If you previously used the 2nd install method, you have to copy the script again to update it + +```bash +sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -f ``` -### Setup daily cronjob +## Setup daily cronjob You copy the scripts to /etc/cron.daily : @@ -79,10 +98,12 @@ 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 +## 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 +## 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) -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) \ No newline at end of file +- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) diff --git a/optimize.sh b/optimize.sh index 4749bb5..0921fb1 100755 --- a/optimize.sh +++ b/optimize.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash #---------------------------------------------------------------------------- # img-optimize- Image optimization bash script #---------------------------------------------------------------------------- @@ -7,15 +7,13 @@ # Author: VirtuBox # License: M.I.T # ---------------------------------------------------------------------------- -# Version 1.0 - 2019-03-20 +# Version 1.1 - 2019-04-05 # ---------------------------------------------------------------------------- -#!/bin/bash CSI='\033[' CEND="${CSI}0m" CGREEN="${CSI}1;32m" - _help() { echo "Bash script to optimize your images and convert them in WebP " echo "Usage: img-optimize [options] " @@ -26,6 +24,7 @@ _help() { echo " --webp ..... convert all images in webp" echo " --nowebp ..... optimize all png & jpg images" echo " --all ..... optimize all images (png + jpg + webp)" + echo " -i, --interactive ... run img-optimize in interactive mode" echo " Other options :" echo " -h, --help, help ... displays this help information" echo "Examples:" @@ -39,67 +38,60 @@ _help() { # Parse script arguments ################################## -if [ "${#}" = "0" ]; then +if [ "${#}" = "0" ]; then _help exit 1 else - + while [ "$#" -gt 0 ]; do case "$1" in - --jpg) - JPG_OPTIMIZATION="y" - if [ "$2" ]; then - IMG_PATH=$2 - shift - else - IMG_PATH="$PWD" - fi + --jpg) + JPG_OPTIMIZATION="y" + if [ "$2" ]; then + IMG_PATH=$2 + shift + fi + ;; + --png) + PNG_OPTIMIZATION="y" + if [ "$2" ]; then + IMG_PATH=$2 + shift + fi ;; - --png) - PNG_OPTIMIZATION="y" - if [ "$2" ]; then - IMG_PATH=$2 - shift - else - IMG_PATH="$PWD" - fi + --nowebp) + JPG_OPTIMIZATION="y" + PNG_OPTIMIZATION="y" + WEBP_OPTIMIZATION="n" + if [ "$2" ]; then + IMG_PATH=$2 + shift + fi ;; - --nowebp) - JPG_OPTIMIZATION="y" - PNG_OPTIMIZATION="y" - WEBP_OPTIMIZATION="n" - if [ "$2" ]; then - IMG_PATH=$2 - shift - else - IMG_PATH="$PWD" - fi + --webp) + WEBP_OPTIMIZATION="y" + if [ "$2" ]; then + IMG_PATH=$2 + shift + fi ;; - --webp) - WEBP_OPTIMIZATION="y" - if [ "$2" ]; then - IMG_PATH=$2 - shift - else - IMG_PATH="$PWD" - fi + --all) + PNG_OPTIMIZATION="y" + JPG_OPTIMIZATION="y" + WEBP_OPTIMIZATION="y" + if [ "$2" ]; then + IMG_PATH=$2 + shift + fi ;; - --all) - PNG_OPTIMIZATION="y" - JPG_OPTIMIZATION="y" - WEBP_OPTIMIZATION="y" - if [ "$2" ]; then - IMG_PATH=$2 - shift - else - IMG_PATH="$PWD" - fi + -i | --interactive) + INTERACTIVE_MODE="1" ;; - -h | --help | help) - _help - exit 1 + -h | --help | help) + _help + exit 1 ;; - *) ;; + *) ;; esac shift done @@ -113,30 +105,38 @@ echo "" echo "Welcome to optimize.sh image optimization script." echo "" -if [ -z "$JPG_OPTIMIZATION" ] && [ -z "$PNG_OPTIMIZATION" ] && [ -z "$WEBP_OPTIMIZATION" ]; then +if [ "$INTERACTIVE_MODE" = "1" ]; then + if [ -z "$IMG_PATH" ]; then + echo "What is the path of images you want to optimize ?" + read -p "Images path (eg. /var/www/images): " IMG_PATH + fi if [ -z "$JPG_OPTIMIZATION" ]; then echo "" - echo "Do you want to optimize all jpg images in $1 ? (y/n)" + echo "Do you want to optimize all jpg images in $IMG_PATH ? (y/n)" while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do read -p "Select an option [y/n]: " JPG_OPTIMIZATION done fi if [ -z "$PNG_OPTIMIZATION" ]; then echo "" - echo "Do you want to optimize all png images in $1 (it may take a while) ? (y/n)" + echo "Do you want to optimize all png images in $IMG_PATH (it may take a while) ? (y/n)" while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do read -p "Select an option [y/n]: " PNG_OPTIMIZATION done fi if [ -z "$WEBP_OPTIMIZATION" ]; then echo "" - echo "Do you want to convert all jpg & png images to WebP in $1 ? (y/n)" + echo "Do you want to convert all jpg & png images to WebP in $IMG_PATH ? (y/n)" while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do read -p "Select an option [y/n]: " WEBP_OPTIMIZATION done echo "" echo "" fi +else + if [ -z "$IMG_PATH" ]; then + IMG_PATH="$PWD" + fi fi ################################## @@ -145,43 +145,54 @@ fi # optimize jpg if [ "$JPG_OPTIMIZATION" = "y" ]; then + [ -z "$(command -v jpegoptim)" ] && { + echo "Error: jpegoptim isn't installed" + exit 1 + } echo -ne ' jpg optimization [..]\r' - cd $IMG_PATH || exit 1 + cd "$IMG_PATH" || exit 1 find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 - + echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r" echo -ne '\n' fi if [ "$PNG_OPTIMIZATION" = "y" ]; then + [ -z "$(command -v optipng)" ] && { + echo "Error: optipng isn't installed" + exit 1 + } # optimize png - + echo -ne ' png optimization [..]\r' - cd $IMG_PATH || exit 1 + cd "$IMG_PATH" || exit 1 find . -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all echo -ne " png optimization [${CGREEN}OK${CEND}]\\r" echo -ne '\n' fi if [ "$WEBP_OPTIMIZATION" = "y" ]; then + [ -z "$(command -v cwebp)" ] && { + echo "Error: cwebp isn't installed" + exit 1 + } # convert png to webp echo -ne ' png to webp conversion [..]\r' - cd $IMG_PATH || exit 1 + cd "$IMG_PATH" || exit 1 find . -type f -iname "*.png" -print0 | xargs -0 -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }' - + bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }' + echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' - + # convert jpg to webp echo -ne ' jpg to webp conversion [..]\r' - cd $IMG_PATH || exit 1 + cd "$IMG_PATH" || exit 1 find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \ - bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }' - + bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }' + echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne '\n' fi - # We're done ! echo "" echo -e " ${CGREEN}Image optimization performed successfully !${CEND}"