41 lines
1.1 KiB
Bash
41 lines
1.1 KiB
Bash
#!/bin/bash
|
||||
|
|
|
|||
|
|
# Directory to start from (default is current directory if none given)
|
|||
|
|
DIR="${1:-.}"
|
|||
|
|
|
|||
|
|
# Optional prefix filter (second argument)
|
|||
|
|
PREFIX="${2:-}"
|
|||
|
|
|
|||
|
|
# JPEG/WEBP compression quality (1–100, lower = more compression)
|
|||
|
|
QUALITY=85
|
|||
|
|
|
|||
|
|
# Target max size
|
|||
|
|
MAXSIZE="1920x1040"
|
|||
|
|
|
|||
|
|
# Recursively find images and compress them
|
|||
|
|
find "$DIR" -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' \) | while read -r FILE; do
|
|||
|
|
BASENAME=$(basename "$FILE")
|
|||
|
|
EXT="${FILE##*.}"
|
|||
|
|
EXT_LOWER=$(echo "$EXT" | tr '[:upper:]' '[:lower:]')
|
|||
|
|
|
|||
|
|
# Skip if prefix is set and filename doesn’t start with it
|
|||
|
|
if [ -n "$PREFIX" ] && [[ "$BASENAME" != "$PREFIX"* ]]; then
|
|||
|
|
continue
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
echo "Processing: $FILE"
|
|||
|
|
|
|||
|
|
TMPFILE="$(mktemp --suffix=".$EXT_LOWER")"
|
|||
|
|
|
|||
|
|
case "$EXT_LOWER" in
|
|||
|
|
jpg|jpeg)
|
|||
|
|
convert "$FILE" -resize "$MAXSIZE"\> -strip -interlace Plane -quality $QUALITY "$TMPFILE"
|
|||
|
|
;;
|
|||
|
|
png|webp)
|
|||
|
|
convert "$FILE" -resize "$MAXSIZE"\> -strip -quality $QUALITY "$TMPFILE"
|
|||
|
|
;;
|
|||
|
|
esac
|
|||
|
|
|
|||
|
|
mv "$TMPFILE" "$FILE"
|
|||
|
|
done
|
|||
|
|
|