# Validate image uploads before you allocate a preview

> Source: <https://dev.to/sumytbe/validate-image-uploads-before-you-allocate-a-preview-1n1g>
> Published: 2026-11-16 02:18:34+00:00

A browser image editor can accept a small file that decodes into a large bitmap. File size is an upload constraint. Pixel count is a memory and processing constraint. If you build an editor that accepts several reference images, enforce both before making full previews, and keep an independent limit on the work you start at once.

This article was drafted with generative AI. The policy values and incident scenarios below are illustrative examples for an image-editor implementation. The cover is an AI-generated workflow illustration.

A hypothetical 8,000 × 6,000 image has 48 million pixels. A single 8-bit RGBA buffer for that image uses 192 million bytes, about 183 MiB, before you count the original compressed file, decoder buffers, canvas copies or GPU resources. The file may be small because large flat areas compress well. A maximum upload size alone tells you nothing reliable about the peak memory required to decode it.

Give the interface two different rejection reasons. An oversized upload can be compressed or exported differently. An image with too many pixels needs smaller dimensions even when its compressed file is already below the byte limit. Use an error message with the applicable limit so the user can choose the right fix.

A browser can reject an oversized `File` before reading it. Its type and name are hints, not proof of the decoded format. Validate format and dimensions with a bounded, format-aware metadata reader before allocating a full bitmap, or perform that check in a controlled server process. Parsing a header is separate from proving the file will decode. JPEG markers may be separated by metadata, WebP has more than one representation, and animated files need a frame policy. A parser should reject unsupported or malformed variants cleanly instead of silently treating them as a still image.

The following TypeScript function is the policy step after a metadata reader has supplied a supported still image's dimensions. It intentionally does not decode the upload. Its sample limits use decimal bytes and pixels.

```
export function checkStillImage(
  bytes: number,
  width: number,
  height: number,
): "ok" | "invalid" | "file-too-large" | "too-many-pixels" {
  const maxBytes = 24_000_000;
  const maxPixels = 16_000_000;
  const validSize = [bytes, width, height].every(
    (value) => Number.isSafeInteger(value) && value > 0,
  );
  if (!validSize) return "invalid";
  if (bytes > maxBytes) return "file-too-large";
  if (width > Math.floor(maxPixels / height)) return "too-many-pixels";
  return "ok";
}
```

The division avoids multiplying two untrusted dimensions. A real system should also bound each dimension for the limits of its decoder and display path. Apply the authoritative checks again on the server before expensive work, because a request can bypass the browser entirely. Set separate time and memory ceilings on the decoding process.

If the editor accepts several references, five safe files are still five allocations. Start only as many preview decodes as the target device can reasonably support. Generate a thumbnail for the visible preview, retain the original separately, and dispose of resources when the user replaces a reference. Revoke object URLs when they are no longer used and close an `ImageBitmap` when the caller owns it and no longer needs it. Even if an API can request a resized bitmap, do not assume that its underlying decoder never materializes the larger source.

Set a separate rule for the number of references the selected model accepts. A file can pass the safety checks and still be incompatible with the chosen edit. If the user switches models, preserve their files and show which references the next generation would use before they commit. The upload, model compatibility and quoted cost are three different pieces of information.

Test exact boundaries and one value beyond each. Add a small compressed file with huge reported dimensions, malformed metadata, an unsupported animation and a series of replaced references. Assert that rejected files never reach the full decode path and that a failed decode releases the concurrency slot. After generation, show the original and candidate independently so checking the input does not get confused with accepting the output.
