{"slug": "compress-images-in-a-pre-commit-hook-and-in-ci", "title": "Compress images in a pre-commit hook and in CI", "summary": "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.", "body_md": "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\".\n\nA few months later the repo is heavy, the docs page is slow, and every clone downloads the same oversized images.\n\nThe 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.\n\n```\nnpx pixelvault-cli optimize ./docs/images -r --in-place\n```\n\nIt needs Node 20.9+ and runs locally. It never writes a file bigger than the original.\n\n`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.\n`optimize` resizes, converts and strips metadata, and prints the saving for each file. A few properties make it safe in automation:\n\n`--keep-metadata` if you need it.\nUseful variations:\n\n```\n# Preview the saving without touching anything\nnpx pixelvault-cli optimize ./docs/images -r --dry-run\n\n# Convert a folder to WebP, capped at 1600px wide, into a separate directory\nnpx pixelvault-cli optimize ./shots -r --format webp --max-width 1600 --out-dir ./optimized\n\n# Machine-readable report (always a JSON array)\nnpx pixelvault-cli optimize ./docs/images -r --json\n```\n\nThe 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.\n\nSave 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:\n\n``` bash\n#!/bin/sh\n# Compress staged images before they reach the repo.\nstaged=$(git diff --cached --name-only --diff-filter=AM | grep -Ei '\\.(png|jpe?g|webp|avif)$')\n[ -z \"$staged\" ] && exit 0\n\nprintf '%s\\n' \"$staged\" | tr '\\n' '\\0' | xargs -0 npx --yes pixelvault-cli optimize --in-place >&2\nprintf '%s\\n' \"$staged\" | tr '\\n' '\\0' | xargs -0 git add\n```\n\n`--in-place` is fine here because the files are about to be committed anyway.\n\nHooks 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:\n\n```\n# .github/workflows/images.yml\nname: Check images\non: pull_request\njobs:\n  optimize:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/setup-node@v4\n        with:\n          node-version: 22\n      - name: Compress images\n        run: npx --yes pixelvault-cli optimize ./docs ./public -r --in-place\n      - name: Fail if any image was not optimized\n        run: |\n          git diff --exit-code --stat || {\n            echo \"Some images were not optimized. Run: npx pixelvault-cli optimize <dir> -r --in-place\"\n            exit 1\n          }\n```\n\nAdjust 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`.\n\nCoding agents take a lot of screenshots, so they are a likely source of oversized images. Two ways to nudge them:\n\n`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.\nFor 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\".\n\nIf 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:\n\n```\nnpx pixelvault-cli optimize hero.png --host\n```\n\nThat step needs a PixelVault account and API key. Compressing locally never does.\n\nOriginal 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/)", "url": "https://wpnews.pro/news/compress-images-in-a-pre-commit-hook-and-in-ci", "canonical_source": "https://dev.to/pixelvault/compress-images-in-a-pre-commit-hook-and-in-ci-5209", "published_at": "2026-12-06 09:06:18+00:00", "updated_at": "2026-12-06 09:15:13.138945+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["pixelvault-cli", "GitHub Actions", "Git", "Node.js", "Claude"], "also_reported_by": [], "takeaway": "Running an idempotent image optimizer in a pre-commit hook and then gating CI on `git diff --exit-code` catches oversized images that binary diffs hide, since a clean repo produces no changes.", "recommendation": {"advice": "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.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/compress-images-in-a-pre-commit-hook-and-in-ci", "markdown": "https://wpnews.pro/news/compress-images-in-a-pre-commit-hook-and-in-ci.md", "text": "https://wpnews.pro/news/compress-images-in-a-pre-commit-hook-and-in-ci.txt", "jsonld": "https://wpnews.pro/news/compress-images-in-a-pre-commit-hook-and-in-ci.jsonld"}}