Last active
January 12, 2026 20:36
-
-
Save e1ectr0cut1e/7ce2d841c0fe41a7781ae212073615af to your computer and use it in GitHub Desktop.
Convert Ghost CMS images to WebP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| apk update | |
| apk add coreutils mariadb-client mariadb-connector-c libwebp-tools | |
| MYSQL_HOST=db | |
| MYSQL_USER=root | |
| MYSQL_PASS=example | |
| MYSQL_DB=ghost | |
| GHOST_PATH=/var/lib/ghost | |
| QUALITY=70 | |
| ABSOLUTE_CONTENT_PATH=$(realpath "$GHOST_PATH/content") || exit 1 | |
| printf "select feature_image from posts where feature_image not regexp '^__GHOST_URL__/content/images/.*\.webp$';\n" | \ | |
| mariadb --skip-ssl-verify-server-cert -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DB" | \ | |
| grep '__GHOST_URL__' | sed "s|__GHOST_URL__|$GHOST_PATH|" | \ | |
| while IFS= read -r IMAGE; do | |
| [ -z "$IMAGE" ] && continue | |
| case "$(realpath "$IMAGE")" in | |
| "$ABSOLUTE_CONTENT_PATH"/*) ;; | |
| *) | |
| printf "Path traversal detected! Please investigate! %s\n" "$IMAGE" >&2 | |
| break | |
| ;; | |
| esac | |
| CONVERTED_IMAGE="$(dirname "$IMAGE")/$(basename "${IMAGE%.*}").webp" | |
| SQL_IMAGE=$(printf "%s" "$IMAGE" | sed -e "s|$GHOST_PATH|__GHOST_URL__|" -e "s/'/''/g") | |
| SQL_CONVERTED_IMAGE=$(printf "%s" "$CONVERTED_IMAGE" | sed -e "s|$GHOST_PATH|__GHOST_URL__|" -e "s/'/''/g") | |
| cwebp -quiet -q "$QUALITY" "$IMAGE" -o "$CONVERTED_IMAGE" && | |
| printf "update posts set feature_image = '%s' where feature_image = '%s';\n" "$SQL_CONVERTED_IMAGE" "$SQL_IMAGE" | \ | |
| mariadb --skip-ssl-verify-server-cert -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DB" && | |
| printf "Successfully converted %s to %s\n" "$IMAGE" "$CONVERTED_IMAGE" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment