# Compress images in a pre-commit hook and in CI

> Source: <https://dev.to/pixelvault/compress-images-in-a-pre-commit-hook-and-in-ci-5209>
> Published: 2026-12-06 09:06:18+00:00

**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 the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

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:

```
# 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
```

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:

``` bash
#!/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
```

`--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:

```
# .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
          }
```

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](https://pixelvault.dev/tools/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](https://pixelvault.dev/blog/compress-images-in-ci-and-pre-commit/)
