Skip to content

Instantly share code, notes, and snippets.

@kkroesch
Created May 7, 2025 08:48
Show Gist options
  • Save kkroesch/f9f9b1aecaad68aca2b2e22783054db2 to your computer and use it in GitHub Desktop.
Save kkroesch/f9f9b1aecaad68aca2b2e22783054db2 to your computer and use it in GitHub Desktop.
Thumbnail generation for website gallery
#!/bin/bash
# mkthumbs.sh – generates thumbnails and large versions only when needed
# Usage: ./mkthumbs.sh [subdirectory]
set -euo pipefail
SUBDIR="${1:-}" # optional first argument
SRC_DIR="content/gallery/${SUBDIR}"
DST_THUMB="static/gallery/thumbs/${SUBDIR}"
DST_FULL="static/gallery/full/${SUBDIR}"
mkdir -p "$DST_THUMB" "$DST_FULL"
find "$SRC_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) | while read -r img; do
name=$(basename "$img")
name_noext="${name%.*}"
out_thumb="$DST_THUMB/${name_noext}.jpg"
out_full="$DST_FULL/${name_noext}.jpg"
regenerate_thumb=false
regenerate_full=false
# Check if thumbnail needs to be (re)generated
if [[ ! -f "$out_thumb" || "$img" -nt "$out_thumb" ]]; then
regenerate_thumb=true
fi
# Check if large version needs to be (re)generated
if [[ ! -f "$out_full" || "$img" -nt "$out_full" ]]; then
regenerate_full=true
fi
# Generate thumbnail if needed
if $regenerate_thumb; then
echo "→ Thumb: $SUBDIR/$name"
magick "$img" -auto-orient -thumbnail 300x300^ -gravity center -extent 300x300 -quality 80 "$out_thumb"
fi
# Generate large version if needed
if $regenerate_full; then
echo "→ Full: $SUBDIR/$name"
magick "$img" -auto-orient -resize 1600x1600\> -quality 50 "$out_full"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment