Exam, visa and job application forms love hard limits: "photo under 50 KB", "signature between 10 and 20 KB". A canvas can export a JPEG at any quality you like, but there is no toBlob(..., { maxBytes: 50000 }). So you have to search for the right settings. Here is the search I ended up with, and the small details that decide whether the portal accepts the file.

First: "50 KB" means two different numbers

Some sites count 1 KB as 1,000 bytes, some as 1,024. A file has to pass both readings, so:

  • a maximum is converted with 1,000 (the smaller number): "under 100 KB" means at most 100,000 bytes
  • a minimum is converted with 1,024 (the larger number): "at least 20 KB" means at least 20,480 bytes
export const maxKbToBytes = (kb: number) => Math.round(kb * 1000) export const minKbToBytes = (kb: number) => Math.round(kb * 1024) 

That one rule removes a whole class of "my 99 KB file was rejected" complaints.

Search quality first, at full size

The best answer is the largest file that still fits, because that is the best-looking one. So the search starts at full resolution and only touches quality:

  1. Encode at quality 95. If it already fits, you are done.
  2. Otherwise binary-search quality between 10 and 90, in steps of 5. Finer steps barely move the byte count, so they only cost time.
  3. Remember every attempt that fits and keep the biggest.

Each attempt is just a canvas encode:

const canvas = new OffscreenCanvas(w, h) const ctx = canvas.getContext("2d")! ctx.fillStyle = "#ffffff" // JPEG has no alpha: transparent PNGs would turn black ctx.fillRect(0, 0, w, h) ctx.drawImage(bitmap, 0, 0, w, h) const blob = await canvas.convertToBlob({ type: "image/jpeg", quality: q / 100 }) 

The white fill matters more than it looks. Without it, a transparent PNG logo or signature comes out on a black background.

When quality is not enough, shrink by measurement

A 12 MP phone photo cannot reach 20 KB at any quality. The pixels have to shrink. The obvious approach is a fixed step (scale by 0.85, try again), but that is slow: going from 690 KB to 20 KB takes about 11 encodes.

JPEG size roughly tracks pixel count, so you can read the next scale off the last measurement instead:

const next = scale * Math.sqrt(targetBytes / lastBytes) * 0.97 // aim slightly under 

The square root is there because bytes follow area and scale is applied to both sides. This usually lands in one or two encodes. Once a size fits, spend the remaining attempts searching quality again at that size, so the result looks as good as possible. Two guards keep it sane: never shrink below 320 px on the long side, and cap the whole search at 16 encodes.

Forms with a minimum: growing a file

Some portals also reject files that are too small. Going up is a different search: try quality between 80 and 95 first, and if quality 95 is still under the floor, enlarge the pixels (measured the same way, up to a cap). Enlarging adds no detail, but it is the only honest way to add bytes. Padding the file with junk is not.

One trap I measured in Chrome: browser JPEG encoders jump hard near the top. The same image was 40 KB at quality 95, 59 KB at 98 and 143 KB at 100. If you reach for 100 first, you land far past the form's maximum. Go above 95 only as a last resort, one step at a time.

PNG is out

PNG is lossless, so there is no quality dial to search. For a byte target, convert to JPEG (or WebP if the portal accepts it).

Try it

This is what runs behind the free KB size tools on BatchSet, for example compress image to 50 KB and compress image to 20 KB. Everything happens in the browser, so the photo is never uploaded.

If you have hit a portal that counts KB some third way, I would like to hear about it.