Files
chrnx-dev-wp/scripts/build-responsive-images.sh
T
chrnxandClaude Opus 5 f859780eaa feat: add atmosphere backdrops and shipped placeholders
About and Projects had a figure floating on a CSS gradient while Music
and Podcasts had a whole world behind theirs. They get their own
landscape backdrop, which is what made the four read as one series.

Adds placeholder art for a release without a cover and a post without a
featured image, composited over the per-topic accent so a grid of
missing images still varies. Tracks borrow their album cover first.

The generated inventory grows to 76 files. Placeholders encode at lower
quality because they sit at half opacity behind text, which kept the
payload inside its budget instead of raising it again.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 19:41:48 -05:00

266 lines
12 KiB
Bash
Executable File

#!/bin/sh
set -eu
chrnx_image_script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
chrnx_image_root=$(dirname -- "$chrnx_image_script_dir")
chrnx_image_default_output="$chrnx_image_root/assets/images/generated"
chrnx_image_output=${1:-"$chrnx_image_default_output"}
case "$chrnx_image_output" in
/*) ;;
*)
echo "Image output must be an explicit absolute path." >&2
exit 2
;;
esac
if [ "$chrnx_image_output" = "/" ] || [ "$chrnx_image_output" = "$chrnx_image_root" ]; then
echo "Refusing broad image output path: $chrnx_image_output" >&2
exit 2
fi
if [ -L "$chrnx_image_output" ]; then
echo "Refusing symbolic-link image output: $chrnx_image_output" >&2
exit 2
fi
if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then
if [ ! -d "$chrnx_image_output" ]; then
echo "Temporary image output does not exist: $chrnx_image_output" >&2
exit 2
fi
chrnx_image_safe_tmp=$(CDPATH='' cd -P -- "${TMPDIR:-/tmp}" && pwd)
chrnx_image_output_parent=$(dirname -- "$chrnx_image_output")
chrnx_image_output_name=$(basename -- "$chrnx_image_output")
chrnx_image_output_parent=$(CDPATH='' cd -P -- "$chrnx_image_output_parent" && pwd)
case "$chrnx_image_output_name" in
chrnx-*) ;;
*)
echo "Temporary image output basename must start with chrnx-." >&2
exit 2
;;
esac
if [ "$chrnx_image_output_parent" != "$chrnx_image_safe_tmp" ]; then
echo "Temporary image output must be a direct child of $chrnx_image_safe_tmp." >&2
exit 2
fi
chrnx_image_output=$(CDPATH='' cd -P -- "$chrnx_image_output" && pwd)
fi
for chrnx_image_tool in sips cwebp avifenc; do
if ! command -v "$chrnx_image_tool" >/dev/null 2>&1; then
echo "Required image tool is missing: $chrnx_image_tool" >&2
exit 1
fi
done
mkdir -p -- "$chrnx_image_output"
chrnx_image_stage=$(mktemp -d "$chrnx_image_output/.chrnx-stage.XXXXXX")
trap 'rm -rf -- "$chrnx_image_stage"' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX")
trap 'rm -rf -- "$chrnx_image_tmp" "$chrnx_image_stage"' EXIT
chrnx_image_build_output=$chrnx_image_stage
chrnx_image_backup="$chrnx_image_stage/.backup"
chrnx_image_discard="$chrnx_image_stage/.discard"
chrnx_image_promoted_log="$chrnx_image_stage/.promoted"
chrnx_image_promotion_active=0
chrnx_rollback_responsive_images() {
chrnx_image_rollback_failed=0
mkdir -p -- "$chrnx_image_discard"
if [ -f "$chrnx_image_promoted_log" ]; then
while IFS= read -r chrnx_image_name; do
chrnx_image_staged="$chrnx_image_stage/$chrnx_image_name"
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ ! -e "$chrnx_image_staged" ] && [ ! -L "$chrnx_image_staged" ] && { [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; }; then
if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then
echo "Responsive image rollback could not remove: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
fi
fi
done < "$chrnx_image_promoted_log"
fi
if [ -d "$chrnx_image_backup" ]; then
for chrnx_image_backed_up in "$chrnx_image_backup"/*; do
if [ ! -e "$chrnx_image_backed_up" ] && [ ! -L "$chrnx_image_backed_up" ]; then
continue
fi
chrnx_image_name=$(basename -- "$chrnx_image_backed_up")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then
if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then
echo "Responsive image rollback could not clear: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
continue
fi
fi
if ! mv -f -- "$chrnx_image_backed_up" "$chrnx_image_published"; then
echo "Responsive image rollback could not restore: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
fi
done
fi
return "$chrnx_image_rollback_failed"
}
chrnx_cleanup_responsive_images() {
chrnx_image_status=$?
chrnx_image_rollback_failed=0
trap - EXIT HUP INT TERM
set +e
if [ "$chrnx_image_promotion_active" -eq 1 ]; then
if ! chrnx_rollback_responsive_images; then
chrnx_image_rollback_failed=1
if [ "$chrnx_image_status" -eq 0 ]; then
chrnx_image_status=1
fi
fi
fi
if ! rm -rf -- "$chrnx_image_tmp"; then
echo "Responsive image temporary cleanup failed: $chrnx_image_tmp" >&2
chrnx_image_status=1
fi
if [ "$chrnx_image_rollback_failed" -eq 0 ]; then
if ! rm -rf -- "$chrnx_image_stage"; then
echo "Responsive image staging cleanup failed: $chrnx_image_stage" >&2
chrnx_image_status=1
fi
else
echo "Responsive image rollback backup preserved at: $chrnx_image_stage" >&2
fi
exit "$chrnx_image_status"
}
trap 'chrnx_cleanup_responsive_images' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
chrnx_build_responsive_image() {
chrnx_image_basename=$1
chrnx_image_source_name=$2
chrnx_image_webp_quality=${3:-78}
chrnx_image_avif_quality=${4:-58}
chrnx_image_source="$chrnx_image_root/assets/images/$chrnx_image_source_name"
chrnx_image_source_extension=${chrnx_image_source_name##*.}
if [ ! -f "$chrnx_image_source" ]; then
echo "Source image is missing: $chrnx_image_source" >&2
exit 1
fi
for chrnx_image_width in 384 768; do
chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_source_extension"
sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null
cwebp -quiet -preset picture -q "$chrnx_image_webp_quality" -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor "$chrnx_image_avif_quality" --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
done
}
chrnx_build_responsive_background() {
chrnx_image_basename=$1
chrnx_image_source_name=$2
chrnx_image_source="$chrnx_image_root/assets/images/$chrnx_image_source_name"
if [ ! -f "$chrnx_image_source" ]; then
echo "Source image is missing: $chrnx_image_source" >&2
exit 1
fi
for chrnx_image_width in 384 768 1536; do
chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.png"
sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null
cwebp -quiet -preset picture -q 62 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 52 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
done
}
for chrnx_image_topic in code gaming life literature podcasts research; do
chrnx_build_responsive_image "alter-ego-$chrnx_image_topic" "alter-ego-$chrnx_image_topic.png"
printf '{\n\t"source": "alter-ego-%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_topic" > "$chrnx_image_build_output/alter-ego-$chrnx_image_topic.json"
done
chrnx_build_responsive_image "alter-ego-gaming-half-body" "alter-ego-gaming-half-body.png"
printf '{\n\t"source": "alter-ego-gaming-half-body.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/alter-ego-gaming-half-body.json"
for chrnx_image_placeholder in placeholder-post placeholder-release; do
# Placeholders sit at half opacity behind text; they do not need full quality.
chrnx_build_responsive_image "$chrnx_image_placeholder" "$chrnx_image_placeholder.png" 48 40
printf '{\n\t"source": "%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_placeholder" > "$chrnx_image_build_output/$chrnx_image_placeholder.json"
done
chrnx_build_responsive_image "the-no-names-musician-cutout" "the-no-names-musician-cutout.png"
printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-musician-cutout.json"
chrnx_build_responsive_image "the-no-names-collage" "the-no-names-collage.jpeg" 58
printf '{\n\t"source": "the-no-names-collage.jpeg",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-collage.json"
chrnx_build_responsive_background "podcast-frequency-grunge" "podcast-frequency-grunge.png"
printf '{\n\t"source": "podcast-frequency-grunge.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' > "$chrnx_image_build_output/podcast-frequency-grunge.json"
for chrnx_image_background in about-grunge projects-grunge; do
chrnx_build_responsive_background "$chrnx_image_background" "$chrnx_image_background.png"
printf '{\n\t"source": "%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' "$chrnx_image_background" > "$chrnx_image_build_output/$chrnx_image_background.json"
done
chrnx_validate_staged_family() {
chrnx_image_basename=$1
shift
for chrnx_image_width in "$@"; do
for chrnx_image_extension in avif webp; do
chrnx_image_staged="$chrnx_image_stage/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_extension"
if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ] || [ ! -s "$chrnx_image_staged" ]; then
echo "Responsive image staging is incomplete: $chrnx_image_staged" >&2
exit 1
fi
chrnx_image_actual_width=$(sips -g pixelWidth "$chrnx_image_staged" 2>/dev/null | awk '/pixelWidth:/ { print $2 }')
if [ "$chrnx_image_actual_width" != "$chrnx_image_width" ]; then
echo "Responsive image staging width mismatch: $chrnx_image_staged" >&2
exit 1
fi
done
done
chrnx_image_manifest="$chrnx_image_stage/$chrnx_image_basename.json"
if [ ! -f "$chrnx_image_manifest" ] || [ -L "$chrnx_image_manifest" ] || [ ! -s "$chrnx_image_manifest" ]; then
echo "Responsive image staging is incomplete: $chrnx_image_manifest" >&2
exit 1
fi
}
for chrnx_image_topic in code gaming life literature podcasts research; do
chrnx_validate_staged_family "alter-ego-$chrnx_image_topic" 384 768
done
chrnx_validate_staged_family "alter-ego-gaming-half-body" 384 768
chrnx_validate_staged_family "the-no-names-musician-cutout" 384 768
chrnx_validate_staged_family "placeholder-post" 384 768
chrnx_validate_staged_family "placeholder-release" 384 768
chrnx_validate_staged_family "the-no-names-collage" 384 768
chrnx_validate_staged_family "podcast-frequency-grunge" 384 768 1536
chrnx_validate_staged_family "about-grunge" 384 768 1536
chrnx_validate_staged_family "projects-grunge" 384 768 1536
set -- "$chrnx_image_stage"/*
if [ "$#" -ne 76 ]; then
echo "Responsive image staging expected 76 files, got $#." >&2
exit 1
fi
for chrnx_image_staged in "$@"; do
if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ]; then
echo "Responsive image staging contains an unsafe entry: $chrnx_image_staged" >&2
exit 1
fi
chrnx_image_name=$(basename -- "$chrnx_image_staged")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -d "$chrnx_image_published" ]; then
echo "Refusing directory-shaped responsive image output: $chrnx_image_published" >&2
exit 1
fi
done
mkdir -p -- "$chrnx_image_backup"
chrnx_image_promotion_active=1
for chrnx_image_staged in "$@"; do
chrnx_image_name=$(basename -- "$chrnx_image_staged")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then
mv -f -- "$chrnx_image_published" "$chrnx_image_backup/$chrnx_image_name"
fi
printf '%s\n' "$chrnx_image_name" >> "$chrnx_image_promoted_log"
mv -f -- "$chrnx_image_staged" "$chrnx_image_published"
done
chrnx_image_promotion_active=0