FileKit
All posts
·8 min read

Image Optimization for Web Performance — A Developer's Guide

How to optimize images for faster web pages: format selection, sizing, compression, lazy loading, responsive images, and measuring impact on Core Web Vitals.

Why Image Optimization Matters for Your Website

Images are the heaviest assets on most web pages. The average web page today loads over 1 MB of images. Unoptimized images slow page loading, hurt search engine rankings (Google uses page speed as a ranking signal), increase hosting bandwidth costs, and drive visitors away — studies consistently show that visitors abandon pages that take more than 3 seconds to load.

Image optimization is the process of reducing image file sizes without visibly degrading quality. Done right, you can cut image sizes by 60-80% while maintaining visual quality that is indistinguishable from the original at normal viewing distances.

The Three Pillars of Image Optimization

1. Choose the Right Format

The format you choose has the biggest impact on file size:

  • JPEG / JPG: Best for photographs and images with smooth gradients. Lossy compression, no transparency. The universal fallback.
  • PNG: Best for screenshots, logos, icons, and images with text or sharp edges. Lossless compression, supports transparency. Larger files than JPEG for photographs.
  • WebP: The modern default for the web. 25-35% smaller than JPEG at equivalent quality, supports transparency and animation. Supported by all modern browsers. Use WebP as your primary format with JPEG fallback.
  • AVIF: The cutting edge — 50% smaller than JPEG. Browser support is growing but not yet universal. Use if your audience is primarily on Chrome, Firefox, or Safari 16+.
  • SVG: For icons, logos, and simple illustrations. Vector format that scales infinitely, typically just a few KB.

For a deeper comparison, see our WebP vs PNG vs JPG guide.

2. Resize to the Displayed Size

This is the single most impactful optimization and the most commonly missed. If your web page displays an image at 800 × 600 pixels, there is no reason to serve a 4000 × 3000 pixel original. The browser downloads the full 4000-pixel image, then discards 96% of the pixels to display it at 800 pixels.

The rule: serve images at 2x the displayed size for Retina/HiDPI screens. If the CSS width is 400px, serve an 800px-wide image. Not 4000px.

  • Hero images: Typically 1200-1600px wide (displayed at 600-800px on a standard layout)
  • Thumbnails: 300-600px wide depending on display size
  • Blog content images: 1200-1400px wide for full-width, 800px for inline images

3. Compress Aggressively

After choosing the right format and size, apply compression. The goal is to find the quality level where file size drops significantly but visual quality remains acceptable:

  • JPEG: Quality 75-85% is the sweet spot for web. Below 70%, artifacts become visible on close inspection. Above 90%, file size increases sharply with diminishing visual improvement.
  • WebP: Quality 75-80% is equivalent to JPEG 85%.
  • PNG: Use maximum compression (it is lossless, so quality is always perfect). Tools like pngquant apply lossy PNG compression that reduces palette size while maintaining visual quality.

Use FileKit's image compressor to compress images directly in your browser without uploading to any server.

Responsive Images: Serving the Right Size to Every Device

Modern HTML provides the srcset attribute for serving different image sizes to different screen widths:

<img
  src="photo-800.webp"
  srcset="photo-400.webp 400w,
          photo-800.webp 800w,
          photo-1200.webp 1200w"
  sizes="(max-width: 600px) 100vw,
         (max-width: 1200px) 50vw,
         800px"
  alt="Description of the image"
  width="800"
  height="600"
  loading="lazy"
/>

The browser selects the smallest image that fits the current viewport and screen density. A phone on a slow connection downloads the 400px version; a desktop with a 4K monitor downloads the 1200px version.

Lazy Loading: Only Load What the User Sees

The loading="lazy" attribute tells the browser to defer loading images until they are about to scroll into the viewport. For a page with 20 images, the browser initially loads only the 2-3 visible ones. The rest load as the user scrolls down.

Important exceptions: the hero image and any above-the-fold images should use loading="eager" (the default) with fetchpriority="high" to ensure they load as fast as possible.

Cumulative Layout Shift: The Hidden Cost of Missing Dimensions

When a browser encounters an <img> tag without width and height attributes, it does not know how much space to reserve. As the image loads, the content below it jumps down — this is called Cumulative Layout Shift (CLS), and Google penalizes pages with high CLS scores.

The fix is simple: always include width and height attributes on every image element. CSS can override the display size, but the aspect ratio from the attributes lets the browser reserve the correct space before the image loads.

Modern Image Delivery: The <picture> Element

For maximum optimization, use the <picture> element to serve AVIF to browsers that support it, WebP as the primary format, and JPEG as the universal fallback:

<picture>
  <source srcset="photo.avif" type="image/avif" />
  <source srcset="photo.webp" type="image/webp" />
  <img src="photo.jpg" alt="Description" width="800" height="600" loading="lazy" />
</picture>

Optimization Checklist

  • All images are in WebP (with JPEG fallback) or AVIF where supported
  • Images are resized to 2x the displayed size, not the original camera resolution
  • JPEG quality is 75-85%, not 95-100%
  • Every <img> has explicit width and height
  • Below-the-fold images use loading="lazy"
  • Hero images use fetchpriority="high"
  • Responsive images use srcset and sizes
  • SVG is used for icons and logos instead of raster images
  • No images are served from the original camera resolution (3000-6000px)

Tools for Image Optimization

  • FileKit Image Compressor — Browser-based, no upload, supports JPEG/PNG/WebP
  • FileKit Image Converter — Convert between formats (PNG → WebP, JPEG → WebP)
  • Squoosh — Google's browser-based tool with side-by-side quality comparison
  • Sharp (Node.js) — Programmatic image processing for build pipelines
  • ImageMagick — Command-line batch processing

Measuring the Impact

After optimizing, measure the improvement using:

  • Google PageSpeed Insights — Scores your page and specifically flags oversized images
  • Chrome DevTools Network tab — Shows each image's transfer size and load time
  • WebPageTest — Waterfall view showing image load sequence and impact on total page weight
  • Lighthouse — Estimates savings from properly-sized and next-gen-format images

A well-optimized page should have total image weight under 500 KB for a typical content page, and under 1 MB for image-heavy galleries.