Prompt
How do I use a CDN to resize images on the fly?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
To resize images on the fly with a CDN, you usually use the CDN’s image transformation feature. The basic idea is:
- Store the original image on your origin server or object storage.
- Request the image through the CDN with resize parameters in the URL.
- The CDN fetches the original, resizes it at the edge, caches the result, and serves the transformed image.
Typical flow
Original image:
https://origin.example.com/images/photo.jpg
CDN transformed URL:
https://cdn.example.com/images/photo.jpg?w=800&h=600&fit=crop
The CDN interprets the query parameters and returns a resized version.
Common resize options
Different CDNs use different syntax, but the usual options are:
worwidth— target widthhorheight— target heightfit— how to handle aspect ratio:cropcontaincoverfill
qorquality— compression qualityformat— auto-convert to WebP/AVIF/JPEG/PNGdpr— device pixel ratio for retina screens
Example:
...?w=400&h=400&fit=crop&format=auto&q=80
Example with a CDN that supports transformations
Cloudflare Image Resizing
Example:
https://example.com/cdn-cgi/image/width=400,height=400,fit=cover,quality=80/images/photo.jpg
Imgix
Example:
https://your-source.imgix.net/images/photo.jpg?w=400&h=400&fit=crop&auto=format,compress
AWS CloudFront + Lambda@Edge / CloudFront Functions
You can:
- attach a function that rewrites URLs
- send requests to an image-processing service
- cache the output at the CDN
Akamai / Fastly
These often support image optimization rules or edge image processing features.
Best practices
- Keep originals untouched and high quality.
- Use caching so the CDN doesn’t resize every request.
- Use responsive images with
srcsetandsizes. - Prefer auto format conversion (
WebP/AVIF) if supported. - Set cache-control headers appropriately.
- Use signed URLs if you want to prevent abuse.
- Make sure the CDN whitelists only your origin to avoid open proxy issues.
HTML example with responsive images
<img
src="https://cdn.example.com/images/photo.jpg?w=800"
srcset="
https://cdn.example.com/images/photo.jpg?w=400 400w,
https://cdn.example.com/images/photo.jpg?w=800 800w,
https://cdn.example.com/images/photo.jpg?w=1200 1200w
"
sizes="(max-width: 600px) 400px, 800px"
alt="Example photo"
/>
If your CDN does not support resizing directly
You can still do it by:
- putting an image resizing service behind the CDN
- using a serverless function or edge function to transform images
- caching the transformed output at the CDN
A simple implementation plan
- Upload originals to S3, R2, or your server.
- Put a CDN in front.
- Enable image transformation.
- Use URL parameters to request sizes.
- Serve responsive variants in your frontend.
- Verify caching and image quality.
If you tell me which CDN you’re using — Cloudflare, CloudFront, Fastly, Akamai, Imgix, etc. — I can show the exact URL format and setup steps.