aboutsummaryrefslogtreecommitdiff
path: root/optimize-images.sh
blob: c95da6e733727bbb0d163626b3a3a60fdbb84100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/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 <image-folder>   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" -resize '1200x800>' -quality 80 "${file%.*}.webp"
    rm "$file"
    echo "$file -> ${file%.*}.webp"
done