cd /news/developer-tools/compress-images-in-a-pre-commit-hook… · home › topics › developer-tools › article
[ARTICLE · art-182794] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Compress images in a pre-commit hook and in CI

A developer published a workflow for automatically compressing oversized images before they reach a repository, using the pixelvault-cli tool in a git pre-commit hook and a GitHub Actions CI check. The hook compresses only staged images and re-stages them, while CI runs the idempotent optimizer and fails via `git diff --exit-code` when any image was missed. The tool runs locally on Node 20.9+, requires no account, and never writes a file larger than the original.

recommendation

Add an idempotent image optimizer to your pre-commit hook and fail CI when `git diff --exit-code` shows changes, so unoptimized images never reach the repo.

more in playbook: Running AI in production
by read3 min views4 publishedDec 6, 2026

Somebody (or some coding agent) adds a screenshot to the docs, and the PNG is 4 MB. Nobody notices in review, because the diff of a binary file is just "changed".

A few months later the repo is heavy, the docs page is slow, and every clone downloads the same oversized images.

The fix is boring and mechanical, so automate it: compress images before they are committed, and make CI fail when one slips through. One command does both. It needs no account and uploads nothing.

npx pixelvault-cli optimize ./docs/images -r --in-place

It needs Node 20.9+ and runs locally. It never writes a file bigger than the original.

git diff --exit-code. Any changed file is an image someone forgot, so the build fails.--in-place overwrites files with no undo. Only use it on files under version control. optimize resizes, converts and strips metadata, and prints the saving for each file. A few properties make it safe in automation:

--keep-metadata if you need it. Useful variations:

npx pixelvault-cli optimize ./docs/images -r --dry-run

npx pixelvault-cli optimize ./shots -r --format webp --max-width 1600 --out-dir ./optimized

npx pixelvault-cli optimize ./docs/images -r --json

The default format is keep, so a PNG stays a PNG (lossless) and a JPEG stays a JPEG. Converting to WebP usually saves the most, but it changes the extension, so update any references to the old name.

Save this as .git/hooks/pre-commit (or call it from your hook manager) and make it executable. It compresses only the images you are about to commit, then re-stages them:

#!/bin/sh
staged=$(git diff --cached --name-only --diff-filter=AM | grep -Ei '\.(png|jpe?g|webp|avif)$')
[ -z "$staged" ] && exit 0

printf '%s\n' "$staged" | tr '\n' '\0' | xargs -0 npx --yes pixelvault-cli optimize --in-place >&2
printf '%s\n' "$staged" | tr '\n' '\0' | xargs -0 git add

--in-place is fine here because the files are about to be committed anyway.

Hooks can be skipped, so add a CI check too. Because optimize is idempotent, a repo with compressed images produces no changes. Anything it does change is exactly the set of images someone forgot:

name: Check images
on: pull_request
jobs:
  optimize:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - name: Compress images
        run: npx --yes pixelvault-cli optimize ./docs ./public -r --in-place
      - name: Fail if any image was not optimized
        run: |
          git diff --exit-code --stat || {
            echo "Some images were not optimized. Run: npx pixelvault-cli optimize <dir> -r --in-place"
            exit 1
          }

Adjust the directories to where your images live. The command stops with an error if a path doesn't exist. If you would rather have CI fix the images than fail, commit the result back to the branch instead of running git diff --exit-code.

Coding agents take a lot of screenshots, so they are a likely source of oversized images. Two ways to nudge them:

CLAUDE.md or AGENTS.md: "Before committing images, run npx pixelvault-cli optimize <files> --in-place." npx skills add pixelvault-dev/skill), which includes /pixelvault-optimize. It tells the agent to preview with --dry-run first, prefer a separate output unless the files are committed, and report the saving. For a handful of images, use the free in-browser image compressor. Drop your files in, pick a format, quality and maximum width, and download the results. Nothing is uploaded unless you click "Get a link".

If a compressed image needs a public URL (a README, an email, a docs page that shouldn't carry the binary), add --host to upload the result and print its CDN URL:

npx pixelvault-cli optimize hero.png --host

That step needs a PixelVault account and API key. Compressing locally never does.

Original post, with the full workflow diagram: pixelvault.dev/blog/compress-images-in-ci-and-pre-commit

── more in #developer-tools 4 stories · sorted by recency
── more on @pixelvault-cli 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/compress-images-in-a…] indexed:0 read:3min 2026-12-06 · —