A product photo that trips a 10 MB upload limit is almost never a big file. It is a big decoded image. The two numbers get conflated because the form only shows one of them.
I learned this the boring way: the same pair of shoes, shot on the same phone, produced a 12.4 MB PNG and a 240 KB JPEG. The PNG failed the upload. The JPEG sailed through and still came out at 2048px on the long side.
This post is about the arithmetic between those two numbers, and why I now do it in the browser before the request leaves.
Two numbers, one decision
File.size tells you what the network will carry. width * height * 4 tells you what the decoder will allocate. A 6,000 x 4,000 PNG expands to roughly 96 MB of RGBA in memory, whatever its compressed size says.
A 10 MB ceiling is a reasonable proxy for "this image is probably too large to process quickly", but it is a bad measurement. A noisy 4,000px JPEG can sit at 3 MB and still be the slowest item in a batch. A flat-lay on white can be 8 MB and decode in milliseconds.
So the useful pre-check has two clauses, not one:
const LIMIT_BYTES = 10 * 1024 * 1024; const LIMIT_LONG_EDGE = 2048; async function inspect(file) { if (file.size > LIMIT_BYTES) { return { ok: false, reason: 'bytes', size: file.size }; } const bitmap = await createImageBitmap(file); const longEdge = Math.max(bitmap.width, bitmap.height); bitmap.close(); if (longEdge < 64) { return { ok: false, reason: 'tiny', width: bitmap.width }; } return { ok: true, longEdge }; } createImageBitmap decodes off the main thread, so the check itself does not jank the page. When the file is a HEIC straight off an iPhone, this call is also where you find out that Safari can decode it and Chrome cannot — better discovered before the upload than after the 30-second wait.
The 2048 arithmetic
If the output is capped at 2048px on the long side, then anything larger is being decoded so it can be thrown away. That is pure waste, and on a 50-image batch it is the difference between a two-minute run and a ten-minute one.
The resize is worth doing client-side when the source is more than about 1.5x the cap:
function targetSize(w, h, cap = 2048) { const longEdge = Math.max(w, h); if (longEdge <= cap) return { w, h, scale: 1 }; const scale = cap / longEdge; return { w: Math.round(w * scale), h: Math.round(h * scale), scale, }; } Two details that bite:
- Round once, at the end. Rounding width and height separately and then deriving the other dimension gives you a 1px skew on odd aspect ratios. Compute the scale, then round both sides.
- Never upscale. A 900px image scaled to 2048px gains nothing: the background removal still has 900px of edge detail to work with, and the file gets four times larger for zero information.
Format triage
The extension is a hint, the MIME type is a claim, and the decoder is the only authority. In practice three formats cover a product catalogue:
- WebP for anything that will live on the web. Same quality, roughly 25–35% of the JPEG bytes on photographic content.
- PNG only when alpha matters, which is exactly the case for a cut-out. A transparent PNG of a bottle is cheap; a transparent PNG of a knitted blanket is not.
- JPEG when a marketplace insists on
.jpgand rejects everything else. Pure white#FFFFFFsurvives JPEG at high quality, but the corners drift to 254 or 253 — enough for some automated checkers to call it "off-white".
That last point is the one nobody warns you about: "white background" and "pure white" are not the same requirement, and JPEG chroma subsampling is happy to split the difference.
The ceiling is not a security control
A 10 MB limit keeps the queue honest. It does not stop a 500 MB decompression bomb, a PDF renamed to .png, or a polyglot file that is valid in two formats at once.
Those need a different layer:
- Sniff the magic bytes, do not trust
Content-Type. - Decode with a hard pixel budget, not just a byte budget.
- Re-encode the output rather than passing the input through.
The re-encode is the cheap part. If the output is always a fresh #FFFFFF JPEG or a fresh transparent PNG at a known size, then whatever was hidden in the source's metadata — EXIF GPS, XMP history, a steganographic payload — does not survive the trip.
A 50-image ZIP changes the failure mode
Single uploads fail one at a time. A ZIP fails as a set, and the failure is usually one entry.
The rule I settled on: validate every entry before processing any of them, and report per-entry, not per-batch.
photos.zip — 47 of 50 entries accepted ✗ img_0031.png 14.2 MB (limit 10 MB) ✗ img_0044.png unsupported format: TIFF ✗ img_0049.jpg 112 x 80 px This is why batch limits differ from single-file limits in most products: a 50-image Standard batch and a 20-image Quality batch are different queue shapes, and mixing them in one ZIP is how you get a job that sits at 80% for twenty minutes.
Fail with a number in it
The most expensive version of this error message is "Upload failed". The cheapest to fix is:
12.4 MB is over the 10 MB limit. Resize to 2048px on the long side and this image drops to about 380 KB.
Everything in that sentence is computable at the moment of failure. A client that knows both numbers can resize and retry without a support ticket, and a support inbox that never receives the ticket is the actual feature.
That pipeline — decode, measure, resize, re-encode — is what runs behind CleanWhiteBG for the 50-image Standard and 20-image Quality batches: JPG, PNG and WebP in, pure #FFFFFF JPG or transparent PNG out, capped at 2048px on the long side, with the source files deleted within two hours.
