Files
chrnx-dev-wp/scripts/build-responsive-images.sh
T

80 lines
3.2 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
chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX")
trap 'rm -rf -- "$chrnx_image_tmp"' EXIT HUP INT TERM
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_build_portrait() {
chrnx_image_basename=$1
chrnx_image_source="$chrnx_image_root/assets/images/$chrnx_image_basename.png"
if [ ! -f "$chrnx_image_source" ]; then
echo "Source portrait 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.png"
sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null
cwebp -quiet -preset picture -q 78 -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 58 --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_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_portrait "alter-ego-$chrnx_image_topic"
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_output/alter-ego-$chrnx_image_topic.json"
done
chrnx_build_portrait "the-no-names-musician-cutout"
printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/the-no-names-musician-cutout.json"