Loading...
Loading...
This skill should be used when the user asks to "optimize images", "compress images", "reduce image file size", "make images smaller", "optimize PNGs", "optimize JPEGs", "speed up website images", "reduce bundle size images", or needs help with image compression for web projects. Provides workflows and scripts for batch image optimization using sharp.
npx skill4agent add b-open-io/gemskills optimize-imagesbun add -d sharp
# or
npm install -D sharp# Total size and count
du -sh public/images/
find public/images -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) | wc -l
# Size by format
find public/images -name "*.png" -exec du -ch {} + | tail -1
find public/images \( -name "*.jpg" -o -name "*.jpeg" \) -exec du -ch {} + | tail -1
# Top 20 largest files
find public/images -type f \( -name "*.png" -o -name "*.jpg" \) -exec ls -la {} \; | \
awk '{print $5, $9}' | sort -rn | head -20 | \
awk '{printf "%6.1fMB %s\n", $1/1048576, $2}'bun run scripts/optimize-images.ts --file=public/images/largest-image.png --dry-runbun run scripts/optimize-images.ts# Compare before/after
du -sh public/images/
# Visually inspect optimized images
# Run production build
bun run buildsharp(filePath)
.png({
quality: 80, // 1-100, lower = smaller
compressionLevel: 9, // 0-9, higher = more compression
adaptiveFiltering: true,
palette: true, // Use palette for smaller files
})
.toBuffer();sharp(filePath)
.jpeg({
quality: 80, // 1-100, lower = smaller
mozjpeg: true, // Use mozjpeg encoder
})
.toBuffer();scripts/optimize-images.ts# Dry run (see what would happen)
bun run scripts/optimize-images.ts --dry-run
# Test single file
bun run scripts/optimize-images.ts --file=path/to/image.png
# Full optimization
bun run scripts/optimize-images.ts| Image Type | Typical Savings |
|---|---|
| Screenshots (PNG) | 40-60% |
| Photos (JPEG) | 20-40% |
| Watercolors (PNG) | 30-50% |
| Icons (PNG) | 10-30% |
next/imagenext/image.husky/pre-commit# Warn if large images are being committed
find public/images -name "*.png" -size +500k -exec echo "Warning: Large image: {}" \;# Fail if images exceed threshold
MAX_SIZE=79 # MB
CURRENT=$(du -sm public/images | cut -f1)
if [ "$CURRENT" -gt "$MAX_SIZE" ]; then
echo "Error: Images exceed ${MAX_SIZE}MB (currently ${CURRENT}MB)"
exit 1
fiqualityqualitypalette: true.png({ quality: 85, palette: false })brew install vipsnpm rebuild sharpreferences/optimization-guide.mdreferences/sharp-api.mdscripts/optimize-images.tsbun add -d sharpdu -sh public/images/bun run scripts/optimize-images.ts --dry-runbun run scripts/optimize-images.ts