compressJPG
compressJPG optimizes.jpg /.jpeg files with a two-step pipeline:
Read this when Use this page when tracing shell automation, compression, upload/download helpers, linting, or preprocessing around compressJPG.
Overview
compressJPG optimizes .jpg / .jpeg files with a two-step pipeline:
jpegtranfor progressive/optimized re-encoding with metadata strippingmogrify -quality 60for additional size reduction
It computes size ratio (original / new) and only replaces the original if ratio is greater than 1.2 (roughly >20% savings).
Input Behavior
- If file arguments are provided, it processes those files.
- If no arguments are provided, it auto-discovers JPEG files in the current directory (non-recursive).
- If neither yields files, it exits with an error.
- Each file's actual type is checked via
file --mime-type: files containing PNG data (mis-labeled files, common in SingleFile-generated web snapshots) are delegated tocompressPNGwithout changing the extension; files that are neither JPEG nor PNG data are skipped with an error message.
Processing Steps
For each file:
- Detect MIME type with
file; delegate PNG data tocompressPNG, skip non-JPEG/non-PNG data - Copy original to a temp file
- Run
jpegtran -copy none -progressive -optimize - Run
mogrify -quality 60on the temp result - Compare byte sizes with
wc -c - Replace original only if compression gain is sufficient
Temporary files are cleaned up at the end of each iteration.
Dependencies
jpegtranmogrifybcfile(MIME-type detection)compressPNG(sibling script, used when a.jpgfile turns out to contain PNG data)- standard POSIX tooling (
find,wc,mktemp,cp,mv,rm)
Notes
- Uses
set -efor fail-fast behavior. - Color helper functions are inlined for portability (instead of sourcing
build/bash.sh). - Uses a conservative replacement threshold to avoid unnecessary churn on low-gain recompressions.
See Also
- compress-gif - GIF optimization companion
- compress-png - PNG optimization companion
- upload - Upload pipeline that may rely on pre-compressed assets