---
name: sharp-responsive-images
description: Batch-optimize raw AI image exports (PNG/JPG) into responsive 640px, 1280px, and 1920px WebP and AVIF assets using Node.js sharp, preserving alpha transparency where present, enforcing Core Web Vitals file budgets, and writing zero-CLS <picture>/<img> markup with exact numeric dimensions.
---

# Sharp Responsive WebP & AVIF Image Pipeline Protocol

Never commit multi-megabyte raw PNG outputs straight from FLUX 3 Image, Seedream 5.5, Ideogram 4.6, or GPT-Image-2.6 into `public/`. Whenever adding raster photos, packshots, or hero artwork to a website, execute this deterministic pipeline:

## 1. Verified Sharp Responsive Batch Script (ESM)
Keep original lossless `.png` masters outside `public/` (e.g., `assets-raw/`), and compile responsive breakpoints with `withoutEnlargement: true` and `.rotate()` (auto-applies EXIF orientation and strips metadata bloat):
```js
import { readdirSync, mkdirSync } from 'node:fs';
import { parse, join } from 'node:path';
import sharp from 'sharp';

const IN_DIR = './assets-raw';
const OUT_DIR = './public/images/media';
const WIDTHS = [640, 1280, 1920];
mkdirSync(OUT_DIR, { recursive: true });

for (const file of readdirSync(IN_DIR).filter((f) => /\.(png|jpe?g|webp)$/i.test(f))) {
  const { name } = parse(file);
  const input = join(IN_DIR, file);
  const meta = await sharp(input).metadata();
  const hasAlpha = Boolean(meta.hasAlpha);

  for (const w of WIDTHS) {
    if (meta.width && w > meta.width && w !== WIDTHS[0]) continue;
    const base = sharp(input).rotate().resize({ width: w, withoutEnlargement: true });
    const webpInfo = await base.clone().webp({ quality: hasAlpha ? 90 : 84, alphaQuality: 95, effort: 5 }).toFile(join(OUT_DIR, `${name}-${w}.webp`));
    const avifInfo = await base.clone().avif({ quality: 58, effort: 4 }).toFile(join(OUT_DIR, `${name}-${w}.avif`));
    console.log(`${name}-${w}: ${webpInfo.width}x${webpInfo.height} | WebP ${(webpInfo.size / 1024).toFixed(1)}KB | AVIF ${(avifInfo.size / 1024).toFixed(1)}KB`);
  }
}
```

## 2. File-Weight Budgets by Slot
- **Above-the-fold Hero (`1280px–1920px`):** Target `< 180 KB` WebP (`< 120 KB` AVIF).
- **PDP Square / Card Thumbnail (`640px–1200px`):** Target `< 90 KB` WebP (`< 65 KB` AVIF).
- **Alpha Cutouts (`Seedream 5.5`, `Ideogram 4.6`, `GPT-Image-2.6`, `Bria RMBG`):** Preserve crisp alpha edges (`alphaQuality: 95` WebP or `png({ compressionLevel: 9, palette: false })`); never convert transparent PNGs to JPEG.
- **Charts / Diagrams with Sharp Text:** Use `webp({ quality: 92 })` (never aggressive lossy compression that introduces halo ringing around thin typography).

## 3. Mandatory Zero-CLS `<picture>` / `<img>` Markup Contract
Always emit exact natural pixel `width` and `height` from `sharp` output metadata plus descriptive `alt` text (`10–35` words):
```html
<picture>
  <source type="image/avif" srcset="/images/media/hero-640.avif 640w, /images/media/hero-1280.avif 1280w" sizes="(max-width: 768px) 100vw, 1200px" />
  <source type="image/webp" srcset="/images/media/hero-640.webp 640w, /images/media/hero-1280.webp 1280w" sizes="(max-width: 768px) 100vw, 1200px" />
  <img src="/images/media/hero-1280.webp" width="1280" height="720" alt="Studio stoneware pour-over kettle on travertine counter under 5600K daylight" loading="eager" fetchpriority="high" decoding="async" class="w-full h-auto" />
</picture>
```
- Use `loading="eager" fetchpriority="high"` **ONLY** for the single above-the-fold LCP hero.
- Use `loading="lazy" decoding="async"` for every below-the-fold image.
