aboutsummaryrefslogtreecommitdiff
path: root/optimize-images.sh
diff options
context:
space:
mode:
Diffstat (limited to 'optimize-images.sh')
-rwxr-xr-xoptimize-images.sh27
1 files changed, 27 insertions, 0 deletions
diff --git a/optimize-images.sh b/optimize-images.sh
new file mode 100755
index 0000000..c95da6e
--- /dev/null
+++ b/optimize-images.sh
@@ -0,0 +1,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