Files
Server-scripts/compress_images.sh
T
2026-09-14 12:43:38 +02:00

42 lines
1.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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