#!/usr/bin/env bash # Optimize a folder of images for the web: # # ./optimize-images.sh post/my-post # # For every .jpg/.jpeg/.png in the folder: # - converts it to .webp (scaled to fit inside 1200x800, quality 80) # - deletes the original (recoverable via git history) # .gif and existing .webp files are left untouched. set -euo pipefail if [ $# -ne 1 ]; then echo "usage: $0 e.g. $0 post/my-post" >&2 exit 1 fi dir="${1%/}" [ -d "$dir" ] || { echo "error: not a directory: $dir" >&2; exit 1; } shopt -s nullglob nocaseglob for file in "$dir"/*.jpg "$dir"/*.jpeg "$dir"/*.png; do magick "$file" -auto-orient -resize '1200x800>' -quality 80 "${file%.*}.webp" rm "$file" echo "$file -> ${file%.*}.webp" done