Prompt
How do I set up on-the-fly image resizing for my site?
Latest observation
On-the-fly image resizing means your site can request images in different sizes as needed, and the server/CDN generates or serves the right version dynamically. The most common ways to do it are:
1) Use an image CDN/service
This is the easiest option.
Examples:
- Cloudinary
- Imgix
- ImageKit
- Fastly Image Optimizer
- Cloudflare Images / Polish / Image Resizing
Typical flow:
- Upload original images.
- Request variants via URL parameters, e.g. width/height/format/quality.
- The service resizes, caches, and delivers optimized images.
Example:
<img src="https://example.imgix.net/photo.jpg?w=800&h=600&fit=crop&auto=format,compress" />
Pros:
- Fast to set up
- Built-in caching and optimization
- Often supports WebP/AVIF, cropping, sharpening, responsive delivery
Cons:
- Monthly cost
- Vendor dependency
2) Use your own server with dynamic resizing
If you want to host it yourself, your app can resize images on request.
Common stacks:
- Node.js:
sharp - Python: Pillow
- PHP: Intervention Image / Imagick
- Go: imaging libraries
- Nginx + image module / backend handler
Example approach
Request format:
/images/photo.jpg?width=800&height=600&format=webp
Backend steps:
- Read the source image
- Validate requested dimensions
- Resize/crop
- Convert format if needed
- Cache the result
- Return the transformed image
Node.js example with Sharp
import express from "express";
import sharp from "sharp";
import fs from "fs";
const app = express();
app.get("/images/:name", async (req, res) => {
const { name } = req.params;
const width = parseInt(req.query.width, 10);
const height = parseInt(req.query.height, 10);
const filePath = `./images/${name}`;
if (!fs.existsSync(filePath)) {
return res.status(404).send("Not found");
}
let image = sharp(filePath);
if (width || height) {
image = image.resize(width || null, height || null);
}
const format = (req.query.format || "jpeg").toString();
if (format === "webp") image = image.webp();
else if (format === "png") image = image.png();
else image = image.jpeg();
res.type(`image/${format}`);
image.pipe(res);
});
app.listen(3000);
Pros:
- Full control
- No external service required
Cons:
- More maintenance
- Need caching and security controls
- Can be CPU-heavy
3) Use responsive images in HTML
Even if you use a resizing service/backend, you should serve the right size to the browser.
Use:
srcsetsizes
Example:
<img
src="/images/photo.jpg?w=800"
srcset="
/images/photo.jpg?w=400 400w,
/images/photo.jpg?w=800 800w,
/images/photo.jpg?w=1200 1200w
"
sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
alt="Example"
/>
This lets the browser pick the best size for the viewport and device pixel ratio.
4) Add caching
This is essential for on-the-fly resizing.
Use:
- CDN caching
- Browser caching headers
- Server-side disk/memory cache
Recommended headers:
Cache-Control: public, max-age=31536000, immutable
If the URL contains all transformation parameters, each variant can be cached separately.
5) Protect against abuse
Dynamic resizing can be expensive.
Add:
- Limits on max width/height
- Allowed formats only
- Allowed source paths
- Rate limiting
- Signed URLs or tokens if needed
Example restrictions:
- Width max 2000 px
- Height max 2000 px
- Only JPEG/PNG/WebP output
- Reject arbitrary remote URLs unless explicitly supported
Recommended setup
For most sites:
- Put originals in object storage or your media folder
- Use a resizing service or
sharpon your backend - Serve images through transform URLs
- Use
srcsetandsizes - Cache aggressively at CDN/browser level
If you tell me your stack
I can give you a ready-to-use setup for:
- WordPress
- Next.js
- React
- Node/Express
- Python/Django/Flask
- Laravel
- Nginx/Apache
- Cloudflare/Cloudinary/Imgix
If you want, I can provide a complete implementation example for your specific stack.