CDN Image Optimization: WebP/AVIF and Responsive Images Guide

Images are usually the heaviest resources on a web page. For most content sites, images account for more than half of total page bytes and are a major factor in loading speed and Core Web Vitals. Doing image optimization at the CDN edge is one of the most cost-effective approaches available.

1. Why Optimize Images at the CDN Edge

The traditional approach is manually exporting multiple versions (different formats and sizes) and maintaining complex naming and path rules. The image-CDN idea is "one source image, generated on demand": you store a single high-quality original at the origin, and the edge node performs format conversion, resizing, cropping, and quality adjustment in real time based on URL parameters.

The benefits:

  • No duplicate storage: no need to keep many copies for different breakpoints and browsers;
  • On-demand generation: only requested variants are created, saving storage and origin bandwidth;
  • Automation: newly uploaded images get the full optimization pipeline automatically, with no manual work.

2. Modern Image Formats: WebP and AVIF

2.1 WebP

WebP was developed by Google and supports lossy, lossless, and transparency. Compared with JPEG at equal quality it typically saves 25%-35% of size, with excellent browser support.

2.2 AVIF

AVIF is based on the AV1 video codec. It typically compresses about 20%-30% better than WebP, making it one of the most efficient web formats today. The cost is higher CPU usage for encoding, which is well suited to unified CDN edge processing.

2.3 Format Negotiation

Modern browsers declare supported image formats via the Accept request header (e.g., image/avif, image/webp). An image CDN inspects that header and returns the best format, falling back to JPEG/PNG for legacy browsers. This is exactly where edge conversion shines — the origin never worries about browser differences.

3. Responsive Images and Size Control

3.1 srcset and sizes

Use srcset and sizes in HTML to tell the browser which image to load for each viewport, so phones do not download desktop-size originals:

<img
  src="https://cdn.example.com/img/hero.jpg?width=1200"
  srcset="
    https://cdn.example.com/img/hero.jpg?width=480 480w,
    https://cdn.example.com/img/hero.jpg?width=800 800w,
    https://cdn.example.com/img/hero.jpg?width=1200 1200w"
  sizes="(max-width: 600px) 100vw, 800px"
  alt="Article image">

3.2 Cropping and Focal Point

Image CDNs typically support fit, crop, and "focal point" parameters to control where cropping is centered, avoiding important content being cut off. Avatars, banners, and product images especially benefit.

3.3 Quality and Compression Balance

  • Photos: set quality to 75-85 — visually almost identical but much smaller;
  • Prefer the CDN's smart compression (MozJPEG, lossy AVIF);
  • Combine with image lazy loading so off-screen images load later, easing first-paint pressure.

4. Ways to Deploy an Image CDN

4.1 Managed Image Service

Platforms like Cloudflare Images let you upload originals and predefine "variants" for different sizes and formats, with friendly URLs and access control.

4.2 Bring Your Own Storage

CDNs can also process images from any origin, including S3-compatible object storage like R2, and apply rules automatically via "transformation flows" for chosen paths — no need to change image URLs.

4.3 Lightweight Self-Built Option

If you prefer not to use a managed service, CDN acceleration with edge functions or server-side scripts can implement on-demand cropping. Self-built setups are more flexible with dynamic parameters, but you must handle cache keys and format negotiation yourself, which raises complexity.

4.4 Quick Reference: Common URL Parameters

Image CDN URL parameters are broadly similar across providers. Using Cloudflare as an example:

Parameter Example Purpose
width width=800 Target width, scaled proportionally
height height=600 Target height
fit fit=cover Scaling and cropping mode
quality quality=80 Output quality
format format=webp Output format
gravity gravity=auto Focal point / crop center
onerror onerror=redirect Fallback when the original fails

Put these parameters in the URL and the edge node generates the variant on demand, while the origin keeps a single original. For team work, standardize the common parameters as a site-level convention so URLs stay consistent and quality or size can be adjusted uniformly later.

5. 16IDC View: Image Optimization Priority Checklist

At 16IDC we recommend this order of attack:

  1. Format first, then size: convert to WebP/AVIF — biggest gain with the least change;
  2. Then responsive: use srcset/sizes so mobile never downloads desktop originals;
  3. Then lazy loading: delay off-screen images with placeholders to avoid layout shift;
  4. Finally an image CDN: when image volume and device variety grow, edge optimization replaces manual multi-version maintenance.

Image optimization is closely tied to website performance optimization and Core Web Vitals optimization. Once the biggest pain point — heavy images — is solved at the CDN edge, LCP and the overall loading experience usually improve immediately.

6. Measuring Results and a Case

How to Quantify the Gain

Run Lighthouse before and after, and watch three numbers: total page bytes, LCP, and image load time. On one blog, original first-paint images weighed about 2.4 MB; after converting to WebP and capping the width at 800px they dropped to about 480 KB, and LCP fell from 3.1s to 1.6s. Sites with a large share of mobile traffic usually benefit even more, because phone screens are narrow and desktop originals are mostly wasted.

When to Adopt an Image CDN

Three signals are worth considering: image requests make up a very large share of total page requests; the same image must serve many screen sizes; or content changes often and you do not want to export multiple versions by hand. Conversely, if your site has only a few fixed images and modest traffic, compressing to WebP with build tooling is enough — no image CDN needed.

Reference: Cloudflare Images docs https://developers.cloudflare.com/images/; MDN img and srcset https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img

Source: https://developers.cloudflare.com/images/