Digital experiences · · 5 min read
Sharp: prepare responsive images for an AI-built website
Create truthful WebP variants, preserve orientation and measure the files you ship before adding them to a business or 3D website.
By Sociologix Editorial

Give generated pages an intentional image pipeline
This AI-assisted editorial guide tests a small Sharp image-preparation script for business websites. Sources were checked November 17, 2026. The local experiment used Node 24.19.0, Sharp 0.35.5 and libvips 8.18.7, with a public documentation PNG and synthetic edge-case files. Five checks passed; no page-load benchmark was run.
An AI-built page can reference a large source image even when its card is narrow. Preparing explicit variants gives the browser useful choices and gives your team files it can inspect. This is one asset step in a website workflow; 3D rendering and video still need their own evaluation.
The measured source was Vercel’s public AI Gateway documentation screenshot, not Sociologix traffic or client work. Its original dimensions were 2456 × 1288 and its size was 307,704 bytes. [6]
1. Reproduce the setup with a copy of your source
Use Node 20.9.0 or newer and a disposable project. The pinned install below matches the version used here; supported platforms can use Sharp’s prebuilt binaries. The experiment used an existing local installation rather than changing the website’s dependencies. [1]
Save the next script as prepare-images.mjs. Put a permitted, single-frame JPEG, PNG or WebP at input.png, or adjust the input filename in the run command. Choose a new output folder; the script refuses an existing one.
npm install sharp@0.35.52. Write variants and record their actual dimensions
The script chooses widths up to 600 and 1200 pixels. A source narrower than both produces one candidate at its own width. Auto-orientation reads EXIF Orientation before resizing; withoutEnlargement prevents upscaling. The output manifest records returned dimensions rather than assuming a filename proves the width. [2][3]
WebP quality 80 is an encoder setting, not a percentage of visual identity. The output is lossy, and Sharp normally removes metadata and converts to sRGB unless preservation options are requested. Inspect brand colors and fine detail before using a derivative. [4]
The 20-million-pixel header limit is a local processing guard. It does not turn this build-time script into a complete hostile-upload service. The sample accepts only the three listed formats and rejects multi-frame inputs. [8]
import sharp from 'sharp';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
const [input, outputDir] = process.argv.slice(2);
if (!input || !outputDir) {
throw new Error('Usage: node prepare-images.mjs input.png NEW-output-folder');
}
const options = { limitInputPixels: 20_000_000 };
const metadata = await sharp(input, options).metadata();
if (!['jpeg', 'png', 'webp'].includes(metadata.format) || (metadata.pages ?? 1) > 1) {
throw new Error('Use a single-frame JPEG, PNG or WebP');
}
const sourceWidth = metadata.autoOrient?.width ?? metadata.width;
if (!Number.isInteger(sourceWidth) || sourceWidth < 1) {
throw new Error('Missing image width');
}
const widths = [...new Set([600, 1200].map(w => Math.min(w, sourceWidth)))];
await mkdir(outputDir); // Require a new folder; do not overwrite an earlier run.
const images = [];
for (const width of widths) {
const file = 'image-' + width + '.webp';
const info = await sharp(input, options)
.autoOrient()
.resize({ width, withoutEnlargement: true })
.webp({ quality: 80 })
.toFile(join(outputDir, file));
images.push({ file, width: info.width, height: info.height, bytes: info.size });
}
await writeFile(join(outputDir, 'images.json'), JSON.stringify(images, null, 2));
console.log(JSON.stringify(images, null, 2));
3. Run it and inspect the measured outputs
The parent directory must exist and prepared-images must be new. A successful run writes WebP files plus images.json. If a conversion fails midway, treat that output folder as incomplete; a manifest is written only after the conversion loop finishes.
node prepare-images.mjs input.png prepared-images- Original public screenshot: 2456 × 1288 PNG, 307,704 bytes.
- Mobile candidate: 600 × 315 WebP, 13,114 bytes.
- Larger candidate: 1200 × 629 WebP, 38,836 bytes — about 87.4% fewer bytes than the source. Both resizing and lossy encoding contributed.
- Five checks covered decodable outputs and an unchanged original, a 240 × 120 source without upscaling or duplicate candidates, EXIF rotation to 40 × 80 with its tag removed, refusal to overwrite an existing folder, and invalid-image rejection.
4. Connect the files to the real layout
Copy the approved variants into your site’s image directory. This HTML uses the measured screenshot dimensions. The width descriptors in srcset must describe actual candidate widths; sizes describes the expected rendered slot. Adapt sizes to your layout rather than copying it into a padded card unchanged. [5]
A browser chooses among candidates using more than viewport width, including display density. Check the selected resource in the Network panel on your target devices. Do not promise that every phone will select the 600-pixel file. [5]
<img
src="/images/image-1200.webp"
srcset="/images/image-600.webp 600w, /images/image-1200.webp 1200w"
sizes="(max-width: 1200px) 100vw, 1200px"
width="1200" height="629"
alt="Example AI Gateway usage dashboard"
style="width:100%;max-width:1200px;height:auto"
>Review quality before making it part of an AI workflow
The 1200-pixel derivative was visually inspected for the chart layout and labels. That inspection does not establish a universal quality threshold. Compare your own photos, logos and screenshots at their intended display sizes; small screenshot text may need a larger variant or a separate detail view.
For an AI website builder, make this a repeatable asset handoff: retain the original, generate variants in a fresh folder, inspect the manifest and approve the visible result. Keep meaningful alternative text and ensure image dimensions match what you publish.
If an image grows after conversion or its text becomes hard to read, keep a better format or adjust the dimensions and encoding. Measure a real page before attributing a loading-speed improvement to this step.
Sources & further reading
- Sharp — Installation and runtime requirements
- Sharp — Image operations and auto-orientation
- Sharp — Resize and withoutEnlargement
- Sharp — WebP output and metadata behavior
- MDN — Responsive img attributes
- Vercel — Official AI Gateway screenshot source page
- lovell/sharp — Official project and Apache-2.0 license
- Sharp — Input options and pixel-limit assumptions
Build a website that earns its visual detail
Sociologix can help combine AI features, responsive imagery and 3D experiences with practical asset and release checks. Bring a page concept and the devices your customers use so we can scope the right experience.
Talk to Sociologix