Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 17 additions & 70 deletions .github/workflows/enforce-image-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:

permissions:
contents: read
pull-requests: write

jobs:
check-images:
Expand All @@ -23,7 +22,6 @@ jobs:
sudo apt-get install -y ffmpeg

- name: Detect oversized images (>1280px)
id: scan
shell: bash
run: |
set -euo pipefail
Expand All @@ -36,7 +34,7 @@ jobs:
'*.png' '*.jpg' '*.jpeg' '*.PNG' '*.JPG' '*.JPEG' || true
)

oversized=""
bad=0
for f in "${files[@]}"; do
[[ -f "$f" ]] || continue

Expand All @@ -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<<EOF"
echo -e "$oversized"
echo "EOF"
} >> "$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 = "<!-- image-size-check -->";
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
50 changes: 50 additions & 0 deletions _imgscript/resize-image.sh
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions scripts/img-resize/resize-recur.sh

This file was deleted.

34 changes: 0 additions & 34 deletions scripts/resize-image.sh

This file was deleted.

Binary file removed tmp/waves_crashing_waves_on_rocks_copy.jpg
Binary file not shown.
Binary file removed tmp/waves_crashing_waves_on_the_ocean.jpg
Binary file not shown.