Skip to content

Instantly share code, notes, and snippets.

@e1ectr0cut1e
Last active January 12, 2026 20:36
Show Gist options
  • Select an option

  • Save e1ectr0cut1e/7ce2d841c0fe41a7781ae212073615af to your computer and use it in GitHub Desktop.

Select an option

Save e1ectr0cut1e/7ce2d841c0fe41a7781ae212073615af to your computer and use it in GitHub Desktop.
Convert Ghost CMS images to WebP
#!/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