release v1.1

This commit is contained in:
VirtuBox 2019-04-05 03:51:50 +02:00
parent af9f3459ad
commit ae9dc38abd
4 changed files with 199 additions and 121 deletions

25
CHANGELOG.md Normal file
View File

@ -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

View File

@ -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) ![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 ## Prerequisite
* optipng
* WebP - jpegoptim for jpg optimization
- optipng for png optimization
- cwebp for WebP conversion
Debian/Ubuntu : Debian/Ubuntu :
@ -20,52 +22,69 @@ Centos 7 :
sudo yum install optipng jpegoptim libwebp-tools -y sudo yum install optipng jpegoptim libwebp-tools -y
``` ```
--- --------------------------------------------------------------------------------
### Installation ## Installation
1) Download the script 1) Clone the repository
```bash ```bash
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize 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 ```bash
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $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 ```bash
Usage: img-optimize [options] <images path> Usage: img-optimize [options] <images path>
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: Options:
--jpg <images path> ..... optimize all jpg images --jpg <images path> ..... optimize all jpg images
--png <images path> ..... optimize all png images --png <images path> ..... optimize all png images
--webp <images path> ..... convert all images in webp --webp <images path> ..... convert all images in webp
--nowebp <images path> ..... optimize all png & jpg images --nowebp <images path> ..... optimize all png & jpg images
--all <images path> ..... optimize all images (png + jpg + webp) --all <images path> ..... optimize all images (png + jpg + webp)
-i, --interactive ... run img-optimize in interactive mode
Other options : Other options :
-h, --help, help ... displays this help information -h, --help, help ... displays this help information
Examples: Example:
optimize all jpg images in /var/www/images optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images img-optimize --jpg /var/www/images
``` ```
### Update the script ## Update the script
To update the script, just run : To update the script, just run :
```bash ```bash
git -C $HOME/.img-optimize pull git -C $HOME/.img-optimize pull
source .bashrc
``` ```
### Setup daily cronjob 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
You copy the scripts to /etc/cron.daily : 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. 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. 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)
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french)

View File

@ -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) ![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 ## Prerequisite
* optipng
* WebP - jpegoptim for jpg optimization
- optipng for png optimization
- cwebp for WebP conversion
Debian/Ubuntu : Debian/Ubuntu :
@ -20,52 +22,69 @@ Centos 7 :
sudo yum install optipng jpegoptim libwebp-tools -y sudo yum install optipng jpegoptim libwebp-tools -y
``` ```
--- --------------------------------------------------------------------------------
### Installation ## Installation
1) Download the script 1) Clone the repository
```bash ```bash
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize 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 ```bash
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $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 ```bash
Usage: img-optimize [options] <image path> sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize
Options:
--jpg <image path> ..... optimize all jpg images
--png <image path> ..... optimize all png images
--webp <image path> ..... convert all images in webp
--nowebp <image path> ..... optimize all png & jpg images
--all <image 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 ## Usage
```bash
Usage: img-optimize [options] <images path>
If images path is empty, 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)
-i, --interactive ... run img-optimize in interactive mode
Other options :
-h, --help, help ... displays this help information
Example:
optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images
```
## Update the script
To update the script, just run : To update the script, just run :
```bash ```bash
git -C $HOME/.img-optimize pull git -C $HOME/.img-optimize pull
source .bashrc
``` ```
### Setup daily cronjob 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
You copy the scripts to /etc/cron.daily : 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. 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. 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)
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french)

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# img-optimize- Image optimization bash script # img-optimize- Image optimization bash script
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -7,15 +7,13 @@
# Author: VirtuBox # Author: VirtuBox
# License: M.I.T # License: M.I.T
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Version 1.0 - 2019-03-20 # Version 1.1 - 2019-04-05
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#!/bin/bash
CSI='\033[' CSI='\033['
CEND="${CSI}0m" CEND="${CSI}0m"
CGREEN="${CSI}1;32m" CGREEN="${CSI}1;32m"
_help() { _help() {
echo "Bash script to optimize your images and convert them in WebP " echo "Bash script to optimize your images and convert them in WebP "
echo "Usage: img-optimize [options] <images path>" echo "Usage: img-optimize [options] <images path>"
@ -26,6 +24,7 @@ _help() {
echo " --webp <images path> ..... convert all images in webp" echo " --webp <images path> ..... convert all images in webp"
echo " --nowebp <images path> ..... optimize all png & jpg images" echo " --nowebp <images path> ..... optimize all png & jpg images"
echo " --all <images path> ..... optimize all images (png + jpg + webp)" echo " --all <images path> ..... optimize all images (png + jpg + webp)"
echo " -i, --interactive ... run img-optimize in interactive mode"
echo " Other options :" echo " Other options :"
echo " -h, --help, help ... displays this help information" echo " -h, --help, help ... displays this help information"
echo "Examples:" echo "Examples:"
@ -51,8 +50,6 @@ else
if [ "$2" ]; then if [ "$2" ]; then
IMG_PATH=$2 IMG_PATH=$2
shift shift
else
IMG_PATH="$PWD"
fi fi
;; ;;
--png) --png)
@ -60,8 +57,6 @@ else
if [ "$2" ]; then if [ "$2" ]; then
IMG_PATH=$2 IMG_PATH=$2
shift shift
else
IMG_PATH="$PWD"
fi fi
;; ;;
--nowebp) --nowebp)
@ -71,8 +66,6 @@ else
if [ "$2" ]; then if [ "$2" ]; then
IMG_PATH=$2 IMG_PATH=$2
shift shift
else
IMG_PATH="$PWD"
fi fi
;; ;;
--webp) --webp)
@ -80,8 +73,6 @@ else
if [ "$2" ]; then if [ "$2" ]; then
IMG_PATH=$2 IMG_PATH=$2
shift shift
else
IMG_PATH="$PWD"
fi fi
;; ;;
--all) --all)
@ -91,10 +82,11 @@ else
if [ "$2" ]; then if [ "$2" ]; then
IMG_PATH=$2 IMG_PATH=$2
shift shift
else
IMG_PATH="$PWD"
fi fi
;; ;;
-i | --interactive)
INTERACTIVE_MODE="1"
;;
-h | --help | help) -h | --help | help)
_help _help
exit 1 exit 1
@ -113,30 +105,38 @@ echo ""
echo "Welcome to optimize.sh image optimization script." echo "Welcome to optimize.sh image optimization script."
echo "" 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 if [ -z "$JPG_OPTIMIZATION" ]; then
echo "" 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 while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " JPG_OPTIMIZATION read -p "Select an option [y/n]: " JPG_OPTIMIZATION
done done
fi fi
if [ -z "$PNG_OPTIMIZATION" ]; then if [ -z "$PNG_OPTIMIZATION" ]; then
echo "" 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 while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " PNG_OPTIMIZATION read -p "Select an option [y/n]: " PNG_OPTIMIZATION
done done
fi fi
if [ -z "$WEBP_OPTIMIZATION" ]; then if [ -z "$WEBP_OPTIMIZATION" ]; then
echo "" 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 while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " WEBP_OPTIMIZATION read -p "Select an option [y/n]: " WEBP_OPTIMIZATION
done done
echo "" echo ""
echo "" echo ""
fi fi
else
if [ -z "$IMG_PATH" ]; then
IMG_PATH="$PWD"
fi
fi fi
################################## ##################################
@ -145,26 +145,38 @@ fi
# optimize jpg # optimize jpg
if [ "$JPG_OPTIMIZATION" = "y" ]; then if [ "$JPG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v jpegoptim)" ] && {
echo "Error: jpegoptim isn't installed"
exit 1
}
echo -ne ' jpg optimization [..]\r' 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 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 " jpg optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
if [ "$PNG_OPTIMIZATION" = "y" ]; then if [ "$PNG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v optipng)" ] && {
echo "Error: optipng isn't installed"
exit 1
}
# optimize png # optimize png
echo -ne ' png optimization [..]\r' 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 find . -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all
echo -ne " png optimization [${CGREEN}OK${CEND}]\\r" echo -ne " png optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
if [ "$WEBP_OPTIMIZATION" = "y" ]; then if [ "$WEBP_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v cwebp)" ] && {
echo "Error: cwebp isn't installed"
exit 1
}
# convert png to webp # convert png to webp
echo -ne ' png to webp conversion [..]\r' 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 {} \ 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; }'
@ -173,7 +185,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
# convert jpg to webp # convert jpg to webp
echo -ne ' jpg to webp conversion [..]\r' 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 {} \ 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; }'
@ -181,7 +193,6 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
echo -ne '\n' echo -ne '\n'
fi fi
# We're done ! # We're done !
echo "" echo ""
echo -e " ${CGREEN}Image optimization performed successfully !${CEND}" echo -e " ${CGREEN}Image optimization performed successfully !${CEND}"