# Image Optimization ## Table of Contents - [Modern Formats](#modern-formats) - [Responsive Images](#responsive-images) - [Sharp Script](#sharp-script) - [Framework Components](#framework-components) --- ## Modern Formats | Format | Use Case | Savings | |--------|----------|---------| | AVIF | Best compression, modern browsers | 50-80% vs JPEG | | WebP | Good compression, wide support | 25-35% vs JPEG | | JPEG | Fallback for old browsers | baseline | ### Picture Element with Fallbacks ```html Description ``` --- ## Responsive Images ### srcset with width descriptors ```html Description ``` ### Full responsive picture ```html Hero ``` --- ## Sharp Script Batch convert images to modern formats and sizes: ```javascript // scripts/optimize-images.js const sharp = require('sharp'); const fs = require('fs'); const path = require('path'); const SIZES = [400, 800, 1200]; const INPUT_DIR = './images/original'; const OUTPUT_DIR = './public/images'; async function optimizeImage(inputPath) { const filename = path.basename(inputPath, path.extname(inputPath)); for (const size of SIZES) { const resized = sharp(inputPath).resize(size); // AVIF await resized .avif({ quality: 70 }) .toFile(`${OUTPUT_DIR}/${filename}-${size}.avif`); // WebP await resized .webp({ quality: 80 }) .toFile(`${OUTPUT_DIR}/${filename}-${size}.webp`); // JPEG fallback await resized .jpeg({ quality: 80, progressive: true }) .toFile(`${OUTPUT_DIR}/${filename}-${size}.jpg`); } } // Process all images fs.readdirSync(INPUT_DIR) .filter(f => /\.(jpg|jpeg|png)$/i.test(f)) .forEach(f => optimizeImage(path.join(INPUT_DIR, f))); ``` Run: `node scripts/optimize-images.js` --- ## Framework Components ### Next.js Image ```javascript import Image from 'next/image'; // Automatic optimization Hero // Fill container
Background
``` ### Astro Image ```astro --- import { Image } from 'astro:assets'; import hero from '../assets/hero.png'; --- Hero ``` ### Vite imagetools ```javascript // vite.config.js import { imagetools } from 'vite-imagetools'; export default { plugins: [imagetools()] }; // Usage in code import heroSrcset from './hero.jpg?w=400;800;1200&format=webp&as=srcset'; ```