compressJPG
Path: build/compressJPG | Language: Bash | Lines: ~51
JPEG compression helper that rewrites files only when savings exceed a threshold.
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.
Processing Steps
For each file:
- 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
jpegtranmogrifybc- 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