How to compress an image to exactly 100 KB in the browser (binary search + WebAssembly)
Job portals, exam forms and visa sites love rules like "photo must be under 100 KB". Most people solve that by uploading their passport photo to a random website. I wanted a tool that hits an exact size without the photo
Job portals, exam forms and visa sites love rules like "photo must be under 100 KB". Most people solve that by uploading their passport photo to a random website. I wanted a tool that hits an exact size without the photo ever leaving the browser, so I built one for Acrofile. Here is how it works.
The problem with a quality slider
JPEG quality and file size are related, but not in a straight line, and the curve is different for every photo. A busy street scene at quality 70 might be 400 KB while a plain portrait at the same setting is 60 KB. So "set quality to 60%" is not an answer to "make it under 100 KB".
What we actually want is: the highest quality that still fits under the limit.
Step 1: real encoders in WebAssembly
The browser's built-in canvas.toBlob(type, quality) works, but its JPEG encoder is basic. Instead the tool uses the same codecs as Google's Squoosh, packaged by jSquash:
- MozJPEG for JPG
- libwebp for WebP
- OxiPNG for PNG
They run in a Web Worker so the page never freezes while it encodes:
import { encode as encodeJpeg } from '@jsquash/jpeg';
import { encode as encodeWebp } from '@jsquash/webp';
async function encode(img: ImageData, format: 'jpeg' | 'webp', quality: number) {
if (format === 'jpeg') return encodeJpeg(img, { quality, progressive: true, optimize_coding: true });
return encodeWebp(img, { quality, method: 4 });
}
Step 2: binary search on quality
Size goes up (almost always) as quality goes up, so we can binary search between 5 and 95. That takes about 7 encodes instead of 90:
let lo = 5, hi = 95, best: ArrayBuffer | null = null, bestQ = 0;
while (lo <= hi) {
const q = Math.round((lo + hi) / 2);
const out = await encode(img, format, q);
if (out.byteLength <= target) { best = out; bestQ = q; lo = q + 1; }
else hi = q - 1;
}
best is now the highest-quality version that fits.
Step 3: shrink the picture before ruining it
If the only way to fit is quality 15, the photo turns into blocky mush. Below roughly quality 35 it looks better to make the image a bit smaller and keep the quality up. So if the best quality is under 35, the worker scales the image to 80% and searches again, up to a few times:
if (best && bestQ >= 35) return best;
img = draw(bitmap, Math.round(img.width * 0.8), Math.round(img.height * 0.8));
// ...and run the binary search again
For a form photo, 600 px wide at quality 70 looks far better than 1200 px at quality 20, and both are 100 KB.
Two details that matter
Transparent PNGs turn black as JPEG. JPEG has no alpha channel, so transparent pixels become black by default. Fill the canvas with white before drawing:
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, w, h);
ctx.drawImage(src, 0, 0, w, h);
PNG cannot hit a target size. It is lossless, so there is no quality knob. The honest thing is to say so and suggest JPG or WebP instead of quietly returning a file that is still too big.
Why do it in the browser at all?
The photos people shrink for forms are passport scans, ID cards and signatures. Doing the work on the device means nothing is uploaded, there is no server cost, and there is no upload wait.
If you want to try it, there are ready presets for 20 KB, 50 KB, 100 KB and 200 KB, and the same idea is used for compressing PDFs to a size limit. Questions and improvements are welcome in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.