DEV Community

Resizing a 12-Megapixel Photo in the Browser: What Actually Works

Every web developer has written the server-side resize endpoint at least once. But for simple "resize to 2000x2000 and squeeze under 200 KB" jobs, the browser is genuinely enough these days - no upload, no storage, no privacy-policy headache.

I've been building a client-side image tool (getresizer.com - my project, disclosure below), and here is what I learned about the actual API surface.

The core: createImageBitmap with resize options

Most people resize like this:

const img = new Image();
img.src = URL.createObjectURL(file);
canvas.drawImage(img, 0, 0, 2000, 2000);

It works, but it decodes the full-resolution bitmap into memory first - a 4000x3000 JPEG becomes ~48 MB of RGBA before you touch a pixel.

The better path is createImageBitmap, which resizes during decode:

const bitmap = await createImageBitmap(file, {
  resizeWidth: 2000,
  resizeHeight: 2000,
  resizeQuality: 'high',
});
const canvas = new OffscreenCanvas(2000, 2000);
canvas.getContext('2d').drawImage(bitmap, 0, 0);
const out = await canvas.convertToBlob({ type: 'image/jpeg', quality: 0.82 });

Measured on a mid-range laptop CPU (no GPU acceleration, synthetic 4000x3000 source, 8.2 MB JPEG): ~1.1 s for decode + resize + re-encode into a 1.8 MB output. The naive drawImage downscale took ~1.0 s and produced a slightly larger file (1.9 MB at the same quality).

For one image the difference is small; the full-size decode you skip is what matters on low-RAM devices.

Hitting a file-size target: binary-search the quality

canvas.toBlob(quality) takes no KB target, so you search for it:

async function compressToTarget(canvas, targetBytes) {
  let lo = 0.4, hi = 0.95, best = null;
  while (lo <= hi) {
    const mid = (lo + hi) / 2;
    const blob = await canvas.convertToBlob({ type: 'image/jpeg', quality: mid });
    if (blob.size <= targetBytes) {
      best = blob;
      lo = mid + 0.03;
    } else {
      hi = mid - 0.03;
    }
  }
  return best; // null if the target is unreachable
}

Six to eight iterations, one encode each.

One caveat I learned the hard way: high-detail sources (sensor noise, fine textures, text screenshots) can be practically incompressible - a 1200x800 noise patch stayed over 200 KB even at quality 0.4. Real photos usually land fine, but handle the null return instead of looping forever.

What's still annoying

  • HEIC decode isn't universal (Safari only). You need libheif WASM, ~2 MB download.
  • EXIF orientation: createImageBitmap now respects imageOrientation: 'from-image' by default, but older code paths still flip photos.
  • CMYK JPEGs still bite.
  • Spec tables are the unglamorous part: Amazon wants 1000-10,000 px per side (zoom activates at 1000 px), LinkedIn wants 400x400 minimum.

I built getresizer.com around exactly these edge cases - the 200 KB compressor is literally the binary search above. Full disclosure: that's my site, and I expect it to miss edge cases - tell me which ones yours hit.

What other client-side image-processing edge cases have you run into?

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.