1
0
Fork 0
forked from lukas/nixos-config
nixos-config/scripts/resize-image.sh
2026-07-28 20:10:42 +02:00

46 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 || $# -gt 3 ]]; then
echo "Usage: $0 INPUT [BASENAME] [OUTPUT_ROOT]" >&2
echo "Example: $0 IMG_0944.HEIC ecoteam /var/www/data/external/pictures" >&2
exit 2
fi
input_path=$1
image_name=${2:-$(basename "${input_path%.*}")}
output_root=${3:-/var/www/data/external/pictures}
if [[ ! -f "$input_path" ]]; then
echo "Input file not found: $input_path" >&2
exit 1
fi
if ! command -v magick >/dev/null 2>&1; then
echo "ImageMagick is required: command 'magick' not found" >&2
exit 1
fi
mkdir -p "$output_root/phone" "$output_root/tablet" "$output_root/desktop"
resize_image() {
local width=$1
local suffix=$2
local directory=$3
magick "$input_path" \
-auto-orient \
-strip \
-resize "${width}x>" \
-quality 82 \
"$output_root/$directory/${image_name}_${suffix}.webp"
}
resize_image 640 s phone
resize_image 1280 m tablet
resize_image 1920 l desktop
printf 'Created:\n'
printf ' %s\n' \
"$output_root/phone/${image_name}_s.webp" \
"$output_root/tablet/${image_name}_m.webp" \
"$output_root/desktop/${image_name}_l.webp"