Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
101b24a5c7 | |||
ecebc0dadf | |||
c5fc39d5d1 | |||
f8f6188edc | |||
27dfe70ce7 | |||
8f9c99465a | |||
c3dd0dd535 | |||
5fcf709922 | |||
8364f01c21 | |||
79da33911a | |||
e39529cd99 | |||
![]() |
c78ff855ef | ||
![]() |
ae15dcf6b7 |
12
README.md
12
README.md
|
@ -1,8 +1,10 @@
|
|||
# Image optimization bash script
|
||||
|
||||

|
||||

|
||||
|
||||
[](https://travis-ci.org/VirtuBox/img-optimize)    
|
||||
The changes from the [original repo](https://github.com/VirtuBox/img-optimize) is that the EXIF data is **preserved**.
|
||||
|
||||
All the credits to [VirtuBox](https://github.com/VirtuBox)
|
||||
|
||||
## Prerequisite
|
||||
|
||||
|
@ -31,10 +33,10 @@ For Debian/Ubuntu (available in scripts folder) :
|
|||
|
||||
```bash
|
||||
# optipng
|
||||
curl -sL git.io/fjddn | sudo -E bash
|
||||
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-optipng.sh | sudo -E bash
|
||||
|
||||
# libwebp
|
||||
curl -sL git.io/fjdd6 | sudo -E bash
|
||||
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-webp.sh | sudo -E bash
|
||||
```
|
||||
|
||||
### Go-Avif installation
|
||||
|
@ -51,7 +53,7 @@ sudo chmod +x /usr/local/bin/avif
|
|||
1) Clone the repository
|
||||
|
||||
```bash
|
||||
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
|
||||
git clone https://gitea.avc.cx/borekon/img-optimize.git $HOME/.img-optimize
|
||||
```
|
||||
|
||||
2) Install the script
|
||||
|
|
|
@ -10,7 +10,7 @@ sites="/var/www/yoursite.tld/images \
|
|||
|
||||
for site in $sites; do
|
||||
# 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-all -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-none -m82 >> /var/log/jpg-png-cron.log 2>&1
|
||||
# optimize png images created in the last 24 hours
|
||||
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1
|
||||
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 >> /var/log/jpg-png-cron.log 2>&1
|
||||
done
|
32
optimize.sh
32
optimize.sh
|
@ -16,7 +16,7 @@ CGREEN="${CSI}1;32m"
|
|||
FIND_ARGS=""
|
||||
PNG_ARGS=""
|
||||
JPG_ARGS=""
|
||||
WEBP_ARGS=""
|
||||
WEBP_ARGS="-metadata all"
|
||||
IMG_PATH="$PWD"
|
||||
|
||||
_help() {
|
||||
|
@ -168,7 +168,7 @@ if [ "$INTERACTIVE_MODE" = "1" ]; then
|
|||
fi
|
||||
if [ -z "$AVIF_OPTIMIZATION" ]; then
|
||||
echo ""
|
||||
echo "Do you want to convert all jpg & png images to WebP in $IMG_PATH ? (y/n)"
|
||||
echo "Do you want to convert all jpg & png images to AVIF in $IMG_PATH ? (y/n)"
|
||||
while [[ $AVIF_OPTIMIZATION != "y" && $AVIF_OPTIMIZATION != "n" ]]; do
|
||||
echo "Select an option [y/n]: "
|
||||
read -r AVIF_OPTIMIZATION
|
||||
|
@ -186,14 +186,16 @@ fi
|
|||
if [ "$JPG_OPTIMIZATION" = "y" ]; then
|
||||
[ -z "$(command -v jpegoptim)" ] && {
|
||||
echo "Error: jpegoptim isn't installed"
|
||||
# Free ressource
|
||||
rm "/tmp/$lock"
|
||||
exit 1
|
||||
}
|
||||
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" -p -s --all-progressive -m82
|
||||
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
|
||||
else
|
||||
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -s -m82 --all-progressive
|
||||
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -P --strip-none -m82 --all-progressive
|
||||
fi
|
||||
|
||||
echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r"
|
||||
|
@ -202,16 +204,18 @@ fi
|
|||
if [ "$PNG_OPTIMIZATION" = "y" ]; then
|
||||
[ -z "$(command -v optipng)" ] && {
|
||||
echo "Error: optipng isn't installed"
|
||||
# Free ressource
|
||||
rm "/tmp/$lock"
|
||||
exit 1
|
||||
}
|
||||
# optimize png
|
||||
|
||||
echo -ne ' png optimization [..]\r'
|
||||
cd "$IMG_PATH" || exit 1
|
||||
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
|
||||
if [ -n "$FIND_ARGS" ]; then
|
||||
find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
|
||||
find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5
|
||||
else
|
||||
find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
|
||||
find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5
|
||||
fi
|
||||
echo -ne " png optimization [${CGREEN}OK${CEND}]\\r"
|
||||
echo -ne '\n'
|
||||
|
@ -219,11 +223,13 @@ fi
|
|||
if [ "$WEBP_OPTIMIZATION" = "y" ]; then
|
||||
[ -z "$(command -v cwebp)" ] && {
|
||||
echo "Error: cwebp isn't installed"
|
||||
# Free ressource
|
||||
rm "/tmp/$lock"
|
||||
exit 1
|
||||
}
|
||||
# convert png to webp
|
||||
echo -ne ' png to webp conversion [..]\r'
|
||||
cd "$IMG_PATH" || exit 1
|
||||
cd "$IMG_PATH" || (rm "/tmp/$lock" && 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 $WEBP_ARGS '{}' -o '{}.webp'; }"
|
||||
|
@ -236,7 +242,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
|
|||
|
||||
# convert jpg to webp
|
||||
echo -ne ' jpg to webp conversion [..]\r'
|
||||
cd "$IMG_PATH" || exit 1
|
||||
cd "$IMG_PATH" || (rm "/tmp/$lock" && 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 || rm -f '{}.webp''; }"
|
||||
|
@ -251,11 +257,13 @@ fi
|
|||
if [ "$AVIF_OPTIMIZATION" = "y" ]; then
|
||||
[ -z "$(command -v avif)" ] && {
|
||||
echo "Error: avif isn't installed"
|
||||
# Free ressource
|
||||
rm "/tmp/$lock"
|
||||
exit 1
|
||||
}
|
||||
# convert png to avif
|
||||
echo -ne ' png to avif conversion [..]\r'
|
||||
cd "$IMG_PATH" || exit 1
|
||||
cd "$IMG_PATH" || (rm "/tmp/$lock" && 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' || rm -f '{}.avif'; }"
|
||||
|
@ -268,7 +276,7 @@ if [ "$AVIF_OPTIMIZATION" = "y" ]; then
|
|||
|
||||
# convert jpg to avif
|
||||
echo -ne ' jpg to avif conversion [..]\r'
|
||||
cd "$IMG_PATH" || exit 1
|
||||
cd "$IMG_PATH" || (rm "/tmp/$lock" && 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' || rm -f '{}.avif'; } "
|
||||
|
@ -287,4 +295,4 @@ echo -e " ${CGREEN}Image optimization performed successfully !${CEND}"
|
|||
echo ""
|
||||
|
||||
# Free ressource
|
||||
rm "/tmp/$lock"
|
||||
rm "/tmp/$lock"
|
||||
|
|
Loading…
Reference in New Issue
Block a user