Prompt
How do I generate multiple image sizes automatically for a website?
Latest observation
Nov 3, 2026
OpenAI APIWeb search: off
A good way to generate multiple image sizes automatically is to use responsive images plus a build step or image service that creates the variants for you.
Best approach
1) Create multiple resized versions
Generate several widths of each image, for example:
- 320px
- 640px
- 960px
- 1280px
This lets the browser download the best size for the device.
2) Serve them with srcset
Example:
<img
src="image-640.jpg"
srcset="
image-320.jpg 320w,
image-640.jpg 640w,
image-960.jpg 960w,
image-1280.jpg 1280w
"
sizes="(max-width: 600px) 100vw, 600px"
alt="Example image"
/>
srcsettells the browser which versions exist.sizestells the browser how wide the image will be on the page.
Ways to generate the images automatically
Option A: Use a build tool
If you have a website build process, use tools like:
- Sharp (Node.js)
- ImageMagick
- gulp-responsive
- Webpack/Vite plugins
Example with Sharp in Node.js:
import sharp from "sharp";
const sizes = [320, 640, 960, 1280];
for (const width of sizes) {
await sharp("input.jpg")
.resize({ width })
.jpeg({ quality: 80 })
.toFile(`output/image-${width}.jpg`);
}
Option B: Use an image CDN/service
Services like:
- Cloudinary
- Imgix
- ImageKit
- Netlify Large Media / built-in image transforms depending on platform
These can resize on the fly and often generate modern formats like WebP or AVIF automatically.
Option C: Use static site generators/frameworks
Frameworks often have built-in image optimization:
- Next.js:
next/image - Astro image integrations
- Gatsby image plugins
- Eleventy with plugins
Recommended extras
- Generate WebP and/or AVIF too
- Use lazy loading:
<img loading="lazy" ... /> - Compress images to reduce file size
- Keep aspect ratios consistent
If you want the simplest setup
For a typical site:
- Use Sharp in a build script to make 4–5 widths
- Output JPG + WebP
- Use
srcsetin HTML
If you want, I can give you:
- a Node.js script that auto-generates responsive images, or
- an example for Next.js / WordPress / Python.