Skip to content

Image Formats and Compression

Optimizing image formats and compression is critical for improving load times and achieving strong Core Web Vitals scores. Modern formats like WebP and AVIF offer significant gains in compression efficiency, while techniques like lossy compression and format-specific tools enable balancing quality and file size.


Image Formats: WebP vs. AVIF

WebP

  • Advantages:
  • Combines lossy and lossless compression (via webp and webplossless encodings).
  • Smaller file sizes than JPEG/PNG for most use cases (up to 30–50% reduction).
  • Supports transparency (alpha channel) and animation.
  • Use Cases:
  • Static images (e.g., product photos, banners).
  • WebP is widely supported in modern browsers (Chrome, Firefox, Edge).

AVIF

  • Advantages:
  • Superior compression efficiency compared to WebP, especially for complex images (up to 40% smaller than WebP in some cases).
  • Lossy and lossless options, with better color fidelity than JPEG.
  • Supports alpha transparency and animation.
  • Use Cases:
  • High-quality images requiring minimal file size (e.g., hero images, galleries).
  • Modern browsers (Chrome 88+, Firefox 86+, Safari 14.1+).

Legacy Formats: JPEG and PNG

  • JPEG: Best for photographs (lossy compression, no transparency).
  • PNG: Lossless compression, ideal for logos/icons (but larger file sizes).
  • Recommendation: Use WebP or AVIF for most use cases, falling back to JPEG/PNG for compatibility.

Compression Techniques

Lossy vs. Lossless

  • Lossy: Reduces file size by discarding non-critical data (e.g., JPEG, WebP).
  • Trade-off: Visible quality loss, but significant size reduction.
  • Lossless: Preserves original quality (e.g., PNG, WebPlossless).
  • Trade-off: Larger file sizes, but no quality degradation.

Tools for Compression

  • Squoosh CLI:
    npx squoosh-cli --input input.jpg --output output.webp --format webp
    
  • ImageOptim (macOS): Automates lossy and lossless compression.
  • Next.js Image Component: Automatically selects optimal format and compression.

Implementation Strategies

1. Use Format Detection

<img 
  src="image.avif" 
  srcset="image.webp 1x, image.avif 2x" 
  type="image/webp" 
  alt="Optimized image"
/>
- Prioritize AVIF for modern browsers, fall back to WebP.

2. Responsive Images

  • Use srcset and sizes to serve appropriate resolutions:
    <img 
      src="image.jpg" 
      srcset="image-320.jpg 320w, image-800.jpg 800w" 
      sizes="(max-width: 600px) 320px, 800px" 
      alt="Responsive image"
    />
    

3. Automate with Build Tools

  • Configure Webpack or Vite to convert images to WebP/AVIF during build:
    // webpack.config.js
    module.exports = {
      module: {
        rules: [
          {
            test: /\.(jpe?g|png|gif)$/i,
            use: [
              {
                loader: 'image-webpack-loader',
                options: { 
                  mozjpeg: { 
                    progressive: true, 
                    quality: 65 
                  }, 
                  optipng: { 
                    optimizationLevel: 7 
                  } 
                }
              }
            ]
          }
        ]
      }
    };
    

Diagram: Image Optimization Workflow

[Source Image]  
  ↓  
[Lossy/Lossless Compression]  
  ↓  
[Format Conversion (WebP/AVIF)]  
  ↓  
[Responsive Delivery (srcset/sizes)]  
  ↓  
[Browser Rendering]  

Key takeaways

  • Prioritize WebP or AVIF for modern browsers to reduce file sizes without sacrificing quality.
  • Balance compression settings based on content: use lossy for photos, lossless for icons.
  • Automate format conversion and compression in build pipelines to ensure consistency.
  • Implement responsive images with srcset and sizes to adapt to device capabilities.
  • Test across browsers to ensure compatibility, especially for AVIF which has limited support in older browsers.