Prompt
How do I set up on-the-fly image resizing for my site?
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
On-the-fly image resizing usually means: when a browser requests an image at a specific size, your server or CDN generates that size automatically, caches it, and serves it fast on future requests.
Common ways to do it
1) Use a CDN/image proxy
Best option for most sites.
Examples:
- Cloudinary
- Imgix
- ImageKit
- Fastly Image Optimizer
- Cloudflare Images / Polish + resizing features
How it works:
- Store one original high-quality image
- Request resized versions via URL parameters
- CDN generates and caches them
Example:
https://cdn.example.com/image.jpg?w=800&h=600&fit=crop
Pros:
- Easy to set up
- Fast global delivery
- Automatic caching and format optimization
- Often supports WebP/AVIF conversion
Cons:
- Paid service
- Vendor dependency
2) Do resizing on your server
If you want full control, your app can resize images dynamically using an image-processing library.
Typical stack:
- Node.js: Sharp
- Python: Pillow
- PHP: Intervention Image / GD / Imagick
- Ruby: MiniMagick
- Go: imaging libraries
Example flow:
- User requests
/images/photo.jpg?width=400 - Server checks cache/storage for a 400px version
- If missing, generate it from the original
- Save/cache it
- Return the image
Important:
- Never resize on every request without caching
- Always validate dimensions to prevent abuse
- Limit maximum width/height
3) Use your web server with a resize module
Possible with:
- Nginx + image filter module
- Apache modules
- Varnish + image processing
- OpenResty/Lua setups
This is less common than a CDN or app-level solution.
Recommended setup
If you’re starting fresh:
- Keep original images in object storage
- S3, R2, GCS, Azure Blob, etc.
- Put a CDN in front
- Use a resize service or image-processing layer
- Cache generated variants aggressively
- Serve modern formats when possible:
- WebP
- AVIF
- Use responsive images in HTML:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
sizes="(max-width: 600px) 400px, 800px"
alt="Example"
>
Good practices
- Resize based on a small set of allowed widths
- Preserve aspect ratio unless cropping is intentional
- Strip metadata if privacy matters
- Compress appropriately
- Cache by image ID + dimensions + format
- Use lazy loading for below-the-fold images:
<img loading="lazy" ...>
If you want a simple DIY approach in Node.js
Using sharp:
import express from "express";
import sharp from "sharp";
import fs from "fs/promises";
const app = express();
app.get("/images/:name", async (req, res) => {
const width = Math.min(parseInt(req.query.width) || 800, 2000);
const inputPath = `./uploads/${req.params.name}`;
const image = sharp(inputPath).resize({ width });
res.type("image/jpeg");
image.jpeg({ quality: 80 }).pipe(res);
});
app.listen(3000);
In production, add:
- caching
- format negotiation
- error handling
- security checks
- storage/CDN integration
Best choice by scenario
- Small/medium site: CDN resizing service
- Custom backend and full control: app-level resizing with cache
- Enterprise/high traffic: CDN + object storage + image pipeline
If you want, I can show you:
- a Cloudflare setup,
- a Node.js/Sharp implementation,
- or an Nginx/CDN architecture for your stack.