Upload forms love file size limits. "Photo must be under 100 KB" is a common one, and the usual advice is to drag a quality slider and hope.

You can do better: let the code find the right quality for you. In this post I'll show a small, working approach that runs entirely in the browser, with no server and no upload, and I'll point out the gotchas that tripped me up while building the image tools on Piczilo.

First, what does "100 KB" actually mean?

This is the first trap. A kilobyte can be 1,000 bytes or 1,024 bytes, and different upload forms mean different things.

  • 100 KB (decimal) = 100,000 bytes
  • 100 KiB = 102,400 bytes

If you aim for 102,000 bytes and the form counts 100,000, you get rejected. When in doubt, target the smaller number. Always leave yourself a safety margin.

The core idea: search over JPEG quality

JPEG file size depends mostly on two things: the pixel dimensions and the quality setting. For a fixed image, size goes down as quality goes down. It is not perfectly smooth, but it is monotonic enough to search for the highest quality that still fits under your byte limit.

A binary search is a natural fit: try a quality in the middle, check the size, then narrow the range. A handful of iterations is usually enough.

Here is a simplified version using the Canvas API:

// Turn a File into a canvas, flattening transparency to white async function fileToCanvas(file) { const bitmap = await createImageBitmap(file); const canvas = document.createElement("canvas"); canvas.width = bitmap.width; canvas.height = bitmap.height; const ctx = canvas.getContext("2d"); ctx.fillStyle = "#ffffff"; // JPEG has no alpha channel ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.drawImage(bitmap, 0, 0); return canvas; } function toJpegBlob(canvas, quality) { return new Promise((resolve) => canvas.toBlob(resolve, "image/jpeg", quality) ); } // Find the highest quality whose output is <= maxBytes async function searchQuality(canvas, maxBytes, iterations = 8) { let lo = 0.05; let hi = 0.95; let best = null; for (let i = 0; i < iterations; i++) { const q = (lo + hi) / 2; const blob = await toJpegBlob(canvas, q); if (blob.size <= maxBytes) { best = { blob, quality: q }; lo = q; // fits: try a higher quality } else { hi = q; // too big: go lower } } return best; // null means even the lowest quality was too big } 

If searchQuality returns null, quality alone can't get you there. That is when you shrink the dimensions.

When quality is not enough: resize, then search again

Large photos (think 4000 x 3000 pixels from a phone) often cannot reach 100,000 bytes by quality alone without looking terrible. Reduce the dimensions a bit and try again:

async function compressToBytes(file, maxBytes) { let canvas = await fileToCanvas(file); for (let pass = 0; pass < 6; pass++) { const result = await searchQuality(canvas, maxBytes); if (result) return result.blob; // Too big even at the lowest quality: scale down by 15% const smaller = document.createElement("canvas"); smaller.width = Math.round(canvas.width * 0.85); smaller.height = Math.round(canvas.height * 0.85); smaller.getContext("2d").drawImage(canvas, 0, 0, smaller.width, smaller.height); canvas = smaller; } throw new Error("Could not reach the size limit"); } 

Usage:

const output = await compressToBytes(file, 100_000); const url = URL.createObjectURL(output); // <a href={url} download="photo.jpg">Download</a> 

Notice what the function does when it fails: it throws an error instead of silently returning a file that is over the limit. A result over the limit is worse than an error, because the user will only find out after the upload fails.

A real run

On Piczilo's 100 KB compressor, the page documents one test with a synthetic 1,800 x 1,200 pixel JPEG. The original was 806,849 bytes. The result was 98,869 bytes: same dimensions, about 35.2% JPEG quality, six quality iterations, and no resize passes. That is roughly an 88% reduction.

One honest caveat: that image was generated artwork, not a photo, so those numbers do not predict what you will get on your own images. Detailed photos and screenshots with small text behave very differently.

Gotchas worth knowing

  1. Transparency is lost. JPEG has no alpha channel, so transparent PNG or WebP areas must be filled with something. White is the usual choice, and the code above does that. Without it you often get black backgrounds.
  2. PNG and WebP inputs become JPEG. If your destination demands PNG, this approach does not apply.
  3. Metadata disappears. Drawing to a canvas drops EXIF data (including orientation in some cases). Check that phone photos are not rotated after processing.
  4. Small files can get bigger. A tiny PNG converted to JPEG at high quality can end up larger than the input. Compare sizes before you ship.
  5. Size limits are not the only rule. Many forms also require a minimum width and height, or a specific format. A 90 KB file can still be rejected for the wrong dimensions, so check the destination requirements first.

Privacy is a real feature

Because everything above runs in the browser, the image never leaves the user's device. For passport photos, IDs and documents that is a big deal. You can verify it yourself: open DevTools, switch to the Network tab, run the compression, and confirm that no request carries the image.

Try it

If you just need the result and not the code, the finished tool is here: compress an image to 100 KB. It also has presets for 50 KB, 200 KB and 500 KB. And if you want a non-code walkthrough, there is a step-by-step guide to compressing an image to 100 KB.

Questions or edge cases I missed? Drop them in the comments and I'll update the post.