# Run the batch optimization script
./scripts/optimize-images.sh
# This will:# - Create a backup of original images# - Compress all JPEGs and PNGs# - Convert large images to WebP format# - Generate a detailed report
Size Limits
Threshold
Action
Status
>500KB
❌ Commit blocked by pre-commit hook
FAIL
300-500KB
⚠️ Warning issued, optimization recommended
WARN
<300KB
✅ Optimized
PASS
Automated Workflows
1. Pre-Commit Hook (Local)
When: Before every commit
What: Validates staged image files
Location:
1
.githooks/pre-commit
Behavior:
Scans all staged images (JPG, PNG, GIF)
Blocks commits with images >500KB
Warns about images 300-500KB
Provides optimization commands
Setup:
1
./scripts/setup-git-hooks.sh
Bypass (NOT recommended):
1
git commit --no-verify
2. GitHub Actions (Pull Requests)
When: On PR with image changes
What: Validates all changed images
Location:
# Check sizels-lh assets/img/animation.gif
# For large GIFs (>500KB):# 1. Use online tool: https://ezgif.com/optimize# 2. Convert to video (better compression):
ffmpeg -i assets/img/animation.gif -movflags faststart -pix_fmt yuv420p -vf"scale=trunc(iw/2)*2:trunc(ih/2)*2" assets/img/animation.mp4
Best Practices
Before Adding Images
Resize to appropriate dimensions
Blog content: 800-1200px wide
Hero images: 1200-1600px wide
Thumbnails: 300-400px wide
Choose the right format
JPEG: Photos, images with gradients
PNG: Screenshots, images with text/transparency
WebP: Modern browsers (use with fallback)
SVG: Icons, logos, simple graphics
GIF: Only for short animations (<500KB)
Optimize before committing
1
2
3
4
5
# Quick check of file sizels-lh assets/img/new-image.jpg
# If >500KB, optimize
./scripts/optimize-images.sh
Using WebP with Fallbacks
1
2
3
4
5
<!-- HTML picture element for WebP with fallback --><picture><sourcesrcset="/assets/img/photo.webp"type="image/webp"><imgsrc="/assets/img/photo.jpg"alt="Description"></picture>
Lazy Loading
1
2
3
4
5
<!-- Add loading="lazy" for below-the-fold images --><imgsrc="/assets/img/photo.jpg"alt="Description"loading="lazy"decoding="async">
Troubleshooting
Pre-commit Hook Not Running
1
2
3
4
5
6
7
# Check git hooks configuration
git config core.hooksPath
# Should output: .githooks# If not set, run setup again
./scripts/setup-git-hooks.sh
# Check script is executablels-la scripts/optimize-images.sh
# Should show: -rwxr-xr-x# If not executablechmod +x scripts/optimize-images.sh
# Run with bash explicitly
bash scripts/optimize-images.sh
Restore from Backup
1
2
3
4
5
6
7
8
# List available backupsls-la _backup/
# Restore specific image from backupcp _backup/images_20250118_143022/assets/img/photo.jpg assets/img/
# Restore all images from specific backupcp-r _backup/images_20250118_143022/assets/img/* assets/img/
Advanced Techniques
Batch Processing Specific Directory
1
2
# Optimize only images in specific subdirectory
find assets/img/2024 -name"*.jpg"-exec mogrify -quality 85 {}\;
Create Responsive Images
1
2
3
4
5
6
7
8
9
10
11
12
13
# Create multiple sizes for responsive designfor img in assets/img/original/*.jpg;do
filename=$(basename"$img" .jpg)# Large (1200px)
convert "$img"-resize 1200x assets/img/${filename}-1200.jpg
# Medium (800px)
convert "$img"-resize 800x assets/img/${filename}-800.jpg
# Small (400px)
convert "$img"-resize 400x assets/img/${filename}-400.jpg
done
Batch WebP Conversion
1
2
3
4
5
# Convert all JPEGs to WebP
find assets/img -name"*.jpg"-exec sh -c'cwebp -q 85 "$1" -o "${1%.jpg}.webp"' _ {}\;# Convert all PNGs to WebP
find assets/img -name"*.png"-exec sh -c'cwebp -q 85 "$1" -o "${1%.png}.webp"' _ {}\;
Performance Monitoring
Check Current Image Sizes
1
2
3
4
5
6
7
8
# Total size of all imagesdu-sh assets/img
# List largest images
find assets/img -type f \(-name"*.jpg"-o-name"*.png"-o-name"*.gif"\)-execls-lh{}\; | awk'{print $5, $9}' | sort-h-r | head-20# Count images over 500KB
find assets/img -type f -size +500k | wc-l
Lighthouse Performance
1
2
3
4
5
6
7
8
# Install Lighthouse CLI
npm install-g lighthouse
# Run audit on production site
lighthouse https://mattblogsit.com --view# Check specific metrics
lighthouse https://mattblogsit.com --only-categories=performance --output=json
CI/CD Integration
GitHub Actions Status Checks
The image optimization workflow runs automatically on PRs:
Detects changed images in
1
assets/img/
Validates sizes against thresholds
Comments on PR with detailed report
Sets commit status:
✅ Success: All images <500KB
❌ Failure: Images >500KB detected
Branch Protection Rules
Consider adding image optimization check as required status: