diff --git a/.github/workflows/enforce-image-size.yml b/.github/workflows/enforce-image-size.yml index 0dfd662a..d4d832a0 100644 --- a/.github/workflows/enforce-image-size.yml +++ b/.github/workflows/enforce-image-size.yml @@ -6,7 +6,6 @@ on: permissions: contents: read - pull-requests: write jobs: check-images: @@ -23,7 +22,6 @@ jobs: sudo apt-get install -y ffmpeg - name: Detect oversized images (>1280px) - id: scan shell: bash run: | set -euo pipefail @@ -36,7 +34,7 @@ jobs: '*.png' '*.jpg' '*.jpeg' '*.PNG' '*.JPG' '*.JPEG' || true ) - oversized="" + bad=0 for f in "${files[@]}"; do [[ -f "$f" ]] || continue @@ -53,74 +51,23 @@ jobs: h="${dims#*x}" if [[ "$w" -gt "$MAX" || "$h" -gt "$MAX" ]]; then - oversized+="${f} (${w}x${h})\n" + echo "::error file=$f::${f} is ${w}x${h}. Max allowed is ${MAX}px (either dimension)." + bad=1 fi done - if [[ -n "$oversized" ]]; then - { - echo "oversized<> "$GITHUB_OUTPUT" - echo "has_oversized=true" >> "$GITHUB_OUTPUT" - else - echo "has_oversized=false" >> "$GITHUB_OUTPUT" + if [[ "$bad" -eq 1 ]]; then + echo "" + echo "One or more images exceed ${MAX}px." + echo "Please fix this locally in your fork." + echo "" + echo "Note: this script requires 'ffmpeg' to be installed on your system." + echo " macOS (Homebrew): brew install ffmpeg" + echo " Ubuntu/Debian: sudo apt-get install ffmpeg" + echo "" + echo "From the root directory of your workshop (not the repo root!), run:" + echo " ../_imgscript/resize-image.sh" + echo "" + echo "The script will only resize images whose width or height is greater than ${MAX}px." + exit 1 fi - - - name: Comment on PR with fix instructions - if: steps.scan.outputs.has_oversized == 'true' - uses: actions/github-script@v7 - with: - script: | - const marker = ""; - const max = 1280; - const oversized = `${{ steps.scan.outputs.oversized }}`.trim(); - - const body = - `${marker}\n` + - `👋 Thanks for the PR! Some images exceed **${max}px** (width or height).\n\n` + - `**Oversized images:**\n` + - "```\n" + oversized + "\n```\n\n" + - `**How to fix (in your fork):**\n` + - "```bash\n" + - "chmod +x scripts/resize-images.sh\n" + - "./scripts/resize-images.sh\n" + - "git commit -am \"Resize images to ${max}px max\"\n" + - "git push\n" + - "```\n\n" + - "Once pushed, this check will pass automatically. 👍"; - - const { owner, repo } = context.repo; - const issue_number = context.payload.pull_request.number; - - const comments = await github.paginate( - github.rest.issues.listComments, - { owner, repo, issue_number, per_page: 100 } - ); - - const existing = comments.find( - c => c.user?.type === "Bot" && c.body?.includes(marker) - ); - - if (existing) { - await github.rest.issues.updateComment({ - owner, - repo, - comment_id: existing.id, - body - }); - } else { - await github.rest.issues.createComment({ - owner, - repo, - issue_number, - body - }); - } - - - name: Fail if oversized images found - if: steps.scan.outputs.has_oversized == 'true' - run: | - echo "Oversized images detected. See PR comment for instructions." - exit 1 diff --git a/_imgscript/resize-image.sh b/_imgscript/resize-image.sh new file mode 100755 index 00000000..a835825e --- /dev/null +++ b/_imgscript/resize-image.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +set -euo pipefail + +MAX=1280 + +# Recursively find images (exclude .git just to be safe) +find . \ + -type f \ + \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) \ + -not -path "./.git/*" \ + -print0 | +while IFS= read -r -d '' f; do + [[ -f "$f" ]] || continue + + # Read dimensions safely (single-line output: WxH) + dims="$(ffprobe -v error -select_streams v:0 \ + -show_entries stream=width,height \ + -of csv=p=0:s=x "$f" || true)" + + if [[ -z "$dims" ]]; then + echo "Skipping (ffprobe failed): $f" + continue + fi + + w="${dims%x*}" + h="${dims#*x}" + + # Skip if already within bounds (no re-encode) + if [[ "$w" -le "$MAX" && "$h" -le "$MAX" ]]; then + echo "Skipping (already <= ${MAX}px): $f (${w}x${h})" + continue + fi + + ext="${f##*.}" + base="${f%.*}" + ext_lc="$(printf '%s' "$ext" | tr '[:upper:]' '[:lower:]')" + tmp="${base}.tmp.${ext_lc}" + + echo "Resizing: $f (${w}x${h} → max ${MAX}px)" + + ffmpeg -loglevel error -nostdin -i "$f" \ + -vf "scale='min(iw,${MAX})':'min(ih,${MAX})':force_original_aspect_ratio=decrease" \ + -y "$tmp" || { + echo "Failed on: $f" >&2 + rm -f "$tmp" + continue + } + + mv -f "$tmp" "$f" +done diff --git a/scripts/img-resize/resize-recur.sh b/scripts/img-resize/resize-recur.sh deleted file mode 100755 index 18364bc8..00000000 --- a/scripts/img-resize/resize-recur.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -# run this script in the root directopry of a workshop to recursivley resize all larger images to max 1920px -# you need to have ffmpeg installed. Should work on Linux and Mac - -find . -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | -while IFS= read -r -d '' f; do - ext="${f##*.}" - base="${f%.*}" - ext_lc="$(printf '%s' "$ext" | tr '[:upper:]' '[:lower:]')" - tmp="${base}.tmp.${ext_lc}" - - echo "Resizing: $f" - ffmpeg -loglevel error -nostdin -i "$f" \ - -vf "scale='min(iw,1920)':'min(ih,1920)':force_original_aspect_ratio=decrease" \ - -y "$tmp" || { - echo "Failed on: $f" >&2 - continue - } - - mv -f "$tmp" "$f" -done - diff --git a/scripts/resize-image.sh b/scripts/resize-image.sh deleted file mode 100755 index 99c20d70..00000000 --- a/scripts/resize-image.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -MAX=1280 - -find . -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | -while IFS= read -r -d '' f; do - [[ -f "$f" ]] || continue - - # Read dimensions - read -r w h < <( - ffprobe -v error -select_streams v:0 -show_entries stream=width,height \ - -of default=noprint_wrappers=1:nokey=1 "$f" | tr '\n' ' ' - ) - - # Skip if already within bounds (no re-encode) - if [[ "${w:-0}" -le "${MAX}" && "${h:-0}" -le "${MAX}" ]]; then - echo "Skipping (already <= ${MAX}px): $f (${w}x${h})" - continue - fi - - ext="${f##*.}" - base="${f%.*}" - ext_lc="$(printf '%s' "$ext" | tr '[:upper:]' '[:lower:]')" - tmp="${base}.tmp.${ext_lc}" - - echo "Resizing: $f (${w}x${h} -> max ${MAX}px)" - - ffmpeg -loglevel error -nostdin -i "$f" \ - -vf "scale='min(iw,${MAX})':'min(ih,${MAX})':force_original_aspect_ratio=decrease" \ - -y "$tmp" || { echo "Failed on: $f" >&2; rm -f "$tmp"; continue; } - - mv -f "$tmp" "$f" -done diff --git a/tmp/waves_crashing_waves_on_rocks_copy.jpg b/tmp/waves_crashing_waves_on_rocks_copy.jpg deleted file mode 100644 index 74bb9380..00000000 Binary files a/tmp/waves_crashing_waves_on_rocks_copy.jpg and /dev/null differ diff --git a/tmp/waves_crashing_waves_on_the_ocean.jpg b/tmp/waves_crashing_waves_on_the_ocean.jpg deleted file mode 100644 index fadf31e9..00000000 Binary files a/tmp/waves_crashing_waves_on_the_ocean.jpg and /dev/null differ