Fix $FIND_ARGS

This commit is contained in:
VirtuBox 2019-07-26 12:53:21 +02:00
parent 967857a421
commit 9d02d67ae7
No known key found for this signature in database
GPG Key ID: 22EB296C97BAD476

View File

@ -17,6 +17,7 @@ FIND_ARGS=""
PNG_ARGS="" PNG_ARGS=""
JPG_ARGS="" JPG_ARGS=""
WEBP_ARGS="" WEBP_ARGS=""
IMG_PATH="$PWD"
_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 "
@ -76,19 +77,20 @@ else
INTERACTIVE_MODE="1" INTERACTIVE_MODE="1"
;; ;;
-q | --quiet) -q | --quiet)
PNG_ARGS+=" -quiet" PNG_ARGS=" -quiet"
JPG_ARGS+=" --quiet" JPG_ARGS=" --quiet"
WEBP_ARGS+=" -quiet" WEBP_ARGS=" -quiet"
;; ;;
--cmin) --cmin)
if [ "$2" ]; then if [ -n "$2" ]; then
FIND_ARGS+="-cmin $2" FIND_ARGS="$2"
shift shift
fi fi
;; ;;
--path) --path)
if [ "$2" ]; then if [ -n "$2" ]; then
IMG_PATH="$2" IMG_PATH="$2"
shift
fi fi
;; ;;
-h | --help | help) -h | --help | help)
@ -112,35 +114,35 @@ echo ""
if [ "$INTERACTIVE_MODE" = "1" ]; then if [ "$INTERACTIVE_MODE" = "1" ]; then
if [ -z "$IMG_PATH" ]; then if [ -z "$IMG_PATH" ]; then
echo "What is the path of images you want to optimize ?" echo "What is the path of images you want to optimize ?"
read -p "Images path (eg. /var/www/images): " IMG_PATH echo "Images path (eg. /var/www/images):"
read -r IMG_PATH
fi fi
if [ -z "$JPG_OPTIMIZATION" ]; then if [ -z "$JPG_OPTIMIZATION" ]; then
echo "" echo ""
echo "Do you want to optimize all jpg images in $IMG_PATH ? (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 echo "Select an option [y/n]: "
read -r 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 $IMG_PATH (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 echo "Select an option [y/n]: "
read -r 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 $IMG_PATH ? (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 echo "Select an option [y/n]: "
read -r WEBP_OPTIMIZATION
done done
echo "" echo ""
echo "" echo ""
fi fi
else
if [ -z "$IMG_PATH" ]; then
IMG_PATH="$PWD"
fi
fi fi
################################## ##################################
@ -155,7 +157,11 @@ if [ "$JPG_OPTIMIZATION" = "y" ]; then
} }
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" \) "$FIND_ARGS" -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" --preserve --strip-all -m82 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
else
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" --preserve --strip-all -m82
fi
echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r" echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
@ -169,7 +175,11 @@ if [ "$PNG_OPTIMIZATION" = "y" ]; then
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' "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all if [ -n "$FIND_ARGS" ]; then
find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
else
find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5 -strip all
fi
echo -ne " png optimization [${CGREEN}OK${CEND}]\\r" echo -ne " png optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
@ -181,17 +191,26 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
# 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" "$FIND_ARGS" -print0 | xargs -r -0 -I {} \ if [ -n "$FIND_ARGS" ]; then
bash -c '[ ! -f "{}.webp" ] && { cwebp "${WEBP_ARGS}" -z 9 -mt -quiet "{}" -o "{}.webp"; }' find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt -quiet "{}" -o "{}.webp"; }'
else
find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt -quiet "{}" -o "{}.webp"; }'
fi
echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
# 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" \) ${FIND_ARGS} -print0 | xargs -0 -I {} \ if [ -n "$FIND_ARGS" ]; then
bash -c '[ ! -f "{}.webp" ] && { cwebp "${WEBP_ARGS}" -q 82 -mt "{}" -o "{}.webp"; }' 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"; }'
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"; }'
fi
echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'