A form says "max 200 KB". Your photo is 3.8 MB. Most online tools take the photo, upload it, and hand back something smaller. That works, but it's a strange step for a passport scan or a medical letter, and it means trusting a server you know nothing about.

I built OwnFile to do this job in the browser instead. You type the size you need, and it finds the highest JPEG or WebP quality that fits. The file never leaves your device. Here's how that works, because the obvious approach doesn't.

Why there's no formula

The tempting idea is to work out the quality from the target: "200 KB of a 3.8 MB file is about 5%, so use quality 0.05." That gives you a terrible image. The relationship between encoder quality and output size is monotonic (higher quality never gives a smaller file) but wildly non-linear, and it depends on the image. A photo of sky and a photo of gravel at the same quality can differ several times over in size.

So there's nothing to invert. You have to try qualities and measure.

Binary search on quality

Because size only grows with quality, binary search works:

async function compressToTarget(canvas, mime, targetBytes) { const encode = (q) => new Promise((res) => canvas.toBlob(res, mime, q)); // Try the ceiling first: if top quality already fits, don't degrade it. const top = await encode(0.98); if (top.size <= targetBytes) return { blob: top, hit: true }; let lo = 0.05, hi = 0.98, best = null; for (let i = 0; i < 8 && hi - lo > 0.01; i++) { const mid = (lo + hi) / 2; const blob = await encode(mid); if (blob.size <= targetBytes) { best = blob; lo = mid; } // fits: push quality up else hi = mid; // too big: come down } if (!best) best = await encode(0.05); // nothing fits: say so, don't hide it return { blob: best, hit: best.size <= targetBytes }; } 

A few details matter more than the search itself:

  • Decode once, encode many times. The image is decoded and drawn onto a canvas a single time, and every pass re-encodes that same canvas. Decoding a 12-megapixel HEIC on every pass would make this unusably slow; re-encoding a canvas is cheap.
  • Check the top first. If the image already fits at near-maximum quality, return it. Otherwise a tool would happily compress a 150 KB file to "fit" a 200 KB limit at quality 0.5.
  • Say when you missed. If even the lowest quality can't reach the target (a 30 KB budget for a large photo), return the smallest version and tell the user it's still too big. A tool that silently hands back an oversized file fails at the one job it was given. The real fix is usually to reduce the pixel dimensions, which saves far more than quality does.
  • JPEG needs a background. Canvas pixels can be transparent, and JPEG can't store that, so transparent areas come out black unless you fill the canvas with white first.

In testing, a 200 KB target produced a 199.8 KB file at quality 0.72.

Proving it doesn't upload

"Runs in your browser" is easy to claim, so here's how to check any tool, including mine:

  1. Open DevTools → Network, and convert or compress an image. A local tool sends no request carrying your file.
  2. Or load the page, switch off Wi-Fi, and try again. If it still works, the processing is on your machine.

OwnFile also ships a Content Security Policy with connect-src 'self'. That means the browser itself would refuse to send your file to any other server, even if a dependency tried.

What it doesn't do

It's images only. PNG has no quality setting in the browser's encoder, so target size works for JPG and WebP, and PNG gets resizing and format change instead. There's no AVIF output yet, because browsers can't encode it natively.

The same site converts HEIC, WebP, AVIF, SVG and ICO, resizes and crops, and does batches with a ZIP download. It's free, with no account.

Try it: ownfile.app/compress