Compare commits

..

No commits in common. "master" and "develop" have entirely different histories.

4 changed files with 19 additions and 29 deletions

View File

@ -1,10 +1,8 @@
# Image optimization bash script # Image optimization bash script
![img-optimize](https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/img-optimize.png) ![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png)
The changes from the [original repo](https://github.com/VirtuBox/img-optimize) is that the EXIF data is **preserved**. [![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)
All the credits to [VirtuBox](https://github.com/VirtuBox)
## Prerequisite ## Prerequisite
@ -33,10 +31,10 @@ For Debian/Ubuntu (available in scripts folder) :
```bash ```bash
# optipng # optipng
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-optipng.sh | sudo -E bash curl -sL git.io/fjddn | sudo -E bash
# libwebp # libwebp
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-webp.sh | sudo -E bash curl -sL git.io/fjdd6 | sudo -E bash
``` ```
### Go-Avif installation ### Go-Avif installation
@ -53,7 +51,7 @@ sudo chmod +x /usr/local/bin/avif
1) Clone the repository 1) Clone the repository
```bash ```bash
git clone https://gitea.avc.cx/borekon/img-optimize.git $HOME/.img-optimize git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
``` ```
2) Install the script 2) Install the script

View File

@ -10,7 +10,7 @@ sites="/var/www/yoursite.tld/images \
for site in $sites; do for site in $sites; do
# optimize jpg images created in the last 24 hours # optimize jpg images created in the last 24 hours
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-none -m82 >> /var/log/jpg-png-cron.log 2>&1 find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 >> /var/log/jpg-png-cron.log 2>&1
# optimize png images created in the last 24 hours # optimize png images created in the last 24 hours
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 >> /var/log/jpg-png-cron.log 2>&1 find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1
done done

View File

@ -16,7 +16,7 @@ CGREEN="${CSI}1;32m"
FIND_ARGS="" FIND_ARGS=""
PNG_ARGS="" PNG_ARGS=""
JPG_ARGS="" JPG_ARGS=""
WEBP_ARGS="-metadata all" WEBP_ARGS=""
IMG_PATH="$PWD" IMG_PATH="$PWD"
_help() { _help() {
@ -168,7 +168,7 @@ if [ "$INTERACTIVE_MODE" = "1" ]; then
fi fi
if [ -z "$AVIF_OPTIMIZATION" ]; then if [ -z "$AVIF_OPTIMIZATION" ]; then
echo "" echo ""
echo "Do you want to convert all jpg & png images to AVIF in $IMG_PATH ? (y/n)" 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 while [[ $AVIF_OPTIMIZATION != "y" && $AVIF_OPTIMIZATION != "n" ]]; do
echo "Select an option [y/n]: " echo "Select an option [y/n]: "
read -r AVIF_OPTIMIZATION read -r AVIF_OPTIMIZATION
@ -186,16 +186,14 @@ fi
if [ "$JPG_OPTIMIZATION" = "y" ]; then if [ "$JPG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v jpegoptim)" ] && { [ -z "$(command -v jpegoptim)" ] && {
echo "Error: jpegoptim isn't installed" echo "Error: jpegoptim isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 exit 1
} }
echo -ne ' jpg optimization [..]\r' echo -ne ' jpg optimization [..]\r'
cd "$IMG_PATH" || exit 1 cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -P --strip-none --all-progressive -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 else
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -P --strip-none -m82 --all-progressive find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -s -m82 --all-progressive
fi fi
echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r" echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r"
@ -204,18 +202,16 @@ fi
if [ "$PNG_OPTIMIZATION" = "y" ]; then if [ "$PNG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v optipng)" ] && { [ -z "$(command -v optipng)" ] && {
echo "Error: optipng isn't installed" echo "Error: optipng isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 exit 1
} }
# optimize png # optimize png
echo -ne ' png optimization [..]\r' echo -ne ' png optimization [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
else else
find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
fi fi
echo -ne " png optimization [${CGREEN}OK${CEND}]\\r" echo -ne " png optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
@ -223,13 +219,11 @@ fi
if [ "$WEBP_OPTIMIZATION" = "y" ]; then if [ "$WEBP_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v cwebp)" ] && { [ -z "$(command -v cwebp)" ] && {
echo "Error: cwebp isn't installed" echo "Error: cwebp isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 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" || (rm "/tmp/$lock" && exit 1) cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }" bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }"
@ -242,7 +236,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" || (rm "/tmp/$lock" && exit 1) cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ 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 || rm -f '{}.webp''; }" bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp || rm -f '{}.webp''; }"
@ -257,13 +251,11 @@ fi
if [ "$AVIF_OPTIMIZATION" = "y" ]; then if [ "$AVIF_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v avif)" ] && { [ -z "$(command -v avif)" ] && {
echo "Error: avif isn't installed" echo "Error: avif isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 exit 1
} }
# convert png to avif # convert png to avif
echo -ne ' png to avif conversion [..]\r' echo -ne ' png to avif conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }" bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }"
@ -276,7 +268,7 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then
# convert jpg to avif # convert jpg to avif
echo -ne ' jpg to avif conversion [..]\r' echo -ne ' jpg to avif conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd "$IMG_PATH" || exit 1
if [ -n "$FIND_ARGS" ]; then if [ -n "$FIND_ARGS" ]; then
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; } " bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; } "

BIN
optipng

Binary file not shown.