DEV Community

How to compress an image to a target size in KB, right in the browser (no upload)

Last month I needed to shrink a photo to under 50 KB for an online form. Every tool I found wanted me to upload the picture to some server first. For an ID photo and a signature, I didn't love that.

So I looked into doing it entirely in the browser. It turns out to be simpler than I expected, and it's a good example of how the canvas API can do real work.

The idea

JPEG quality is a number between 0 and 1, but file size doesn't scale in a straight line with it. You can't just calculate the right value. What works is a binary search: try a quality, check the size, then move up or down.

async function compressToKB(file, targetKB) {
  const bitmap = await createImageBitmap(file);
  const canvas = document.createElement('canvas');
  canvas.width = bitmap.width;
  canvas.height = bitmap.height;
  canvas.getContext('2d').drawImage(bitmap, 0, 0);
  let low = 0.1;
  let high = 0.95;
  let best = null;
  for (let i = 0; i < 8; i++) {
    const quality = (low + high) / 2;
    const blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', quality));
    if (blob.size / 1024 <= targetKB) {
      best = blob; // fits, so try a higher quality
      low = quality;
    } else {
      high = quality; // too big, so go lower
    }
  }
  return best; // null means the target wasn't reachable at this size
}

Eight rounds is enough to land very close to the target, and the whole thing runs locally. Nothing leaves the device.

When quality alone isn't enough

If best comes back null, lowering quality can't reach your target. The image simply has too many pixels. The fix is to scale the canvas down (say, by 10% each round) and run the search again.

Just be honest with yourself about the trade-off: very small targets like 20 KB will soften a large photo.

Things that tripped me up

  • PNG ignores the quality value. toBlob only uses it for JPEG and WebP, so convert to JPEG if you need a size target.
  • Transparency turns black in JPEG. Fill the canvas with white first if your image has transparent areas.
  • Phone photos can be huge. A 12 MP photo is usually the reason a target can't be hit, so resize first.
  • "Under 50 KB" and "exactly 50 KB" are different. Most forms want a maximum, so the search above stops just below the limit.

If you don't want to code it

I put these ideas into a free tool, RatioCalc's image compressor. It compresses to a target size in KB and shows the before and after size and dimensions. It also has resize and crop calculators, and photo and signature size guides for common Indian exam forms.

Takeaways

  1. Use a binary search on quality instead of guessing.
  2. Fall back to scaling dimensions when quality isn't enough.
  3. Keep everything local when the image is personal.

If you've solved this a different way, I'd like to hear it in the comments.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.