DEV Community

PixelVault
PixelVault

Posted on Originally published at pixelvault.dev

Compress images in a pre-commit hook and in CI

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.

Quick answer

npx pixelvault-cli optimize ./docs/images -r --in-place
Enter fullscreen mode Exit fullscreen mode

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

  • Pre-commit: run it on staged images only, then re-stage them.
  • CI: run it, then 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.

The one command

optimize resizes, converts and strips metadata, and prints the saving for each file. A few properties make it safe in automation:

  • It never writes a bigger file. If an image can't be made smaller, it is left alone and reported as already optimal.
  • It is idempotent. Running it again over compressed images changes nothing, so a hook or CI check doesn't nibble at the same file every time.
  • It refuses to lose data. It stops before writing if two inputs would produce the same output file, or an output would overwrite an input. It writes through a temporary file, so an interrupted run can't truncate an image.
  • Metadata is stripped (camera details, GPS location) after rotation is applied, so the photo still displays the right way up. Add --keep-metadata if you need it.
  • Formats: JPG, PNG, WebP and AVIF. GIF, SVG, animated images and iPhone HEIC are skipped.

Useful variations:

# Preview the saving without touching anything
npx pixelvault-cli optimize ./docs/images -r --dry-run

# Convert a folder to WebP, capped at 1600px wide, into a separate directory
npx pixelvault-cli optimize ./shots -r --format webp --max-width 1600 --out-dir ./optimized

# Machine-readable report (always a JSON array)
npx pixelvault-cli optimize ./docs/images -r --json
Enter fullscreen mode Exit fullscreen mode

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.

Compress staged images in a pre-commit hook

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
# Compress staged images before they reach the repo.
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
Enter fullscreen mode Exit fullscreen mode

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

Fail the build when an image is not optimized

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:

# .github/workflows/images.yml
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
          }
Enter fullscreen mode Exit fullscreen mode

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.

Make your AI coding agent do it

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

  • Add one line to your CLAUDE.md or AGENTS.md: "Before committing images, run npx pixelvault-cli optimize <files> --in-place."
  • Install the PixelVault skill (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.

Prefer not to install anything?

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".

When you also want a hosted URL

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)