"Compress this image under 200 KB" is one of the most common form-upload requirements on the web—yet guesswork quality sliders almost always overshoot the byte budget or turn text unreadable.
When building Compress Image and Convert Image in FileMinto, we avoided guessing file sizes from lossy quality presets and moved encoding into dedicated Web Workers (0 B uploaded).
1. Sniffing ISO BMFF containers instead of trusting extensions
Phones frequently export .heic or .avif photos whose MIME types are blank or mislabeled. Before choosing a decoder (images.mjs), we read just the first 128 bytes (file.slice(0, 128).arrayBuffer()) and inspect the ISO BMFF ftyp box:
-
avif/avisbrands route through our AVIF inspection path (workers/image.mjs). -
heic/heix/hevc/mif1/msf1brands spin up an isolated HEIC-to-JPEG Web Worker (workers/heic-to-jpg.js) on demand. - PNG-to-ICO packaging routes to a dedicated multi-size icon Worker (
workers/png-to-ico.mjs).
Every job wraps its Worker in an AbortController so cancelling a batch immediately calls worker.terminate() without leaking memory.
2. Bounded binary search over actual encoded bytes
A 0.75 JPEG quality produces wildly different byte sizes on a flat UI screenshot versus a noisy camera photo. In compressTarget (engines/compress-target.mjs), success strictly checks blob.size <= targetBytes:
- If the original format, dimensions, and framing already match
<= targetBytes, keep the original bytes untouched (unchanged: true) to avoid pointless generational loss. - Otherwise, render via
OffscreenCanvasinside the Worker (createImageBitmap+imageSmoothingQuality = 'high') and binary-searchqualityacross[0.1, 0.95]over 8 steps. - If
quality = 0.1still exceedstargetBytes(and resize is allowed), step down dimensions conservatively up to 6 bounded passes (ratio = Math.max(64 / Math.max(w, h), Math.min(0.8, Math.sqrt(targetBytes / size) * 0.9))) until the exact byte cap is met.
3. Batch ZIP downloads in memory
Once a batch completes in AssetStore, createZip packages multi-file outputs locally into one archive for instant download—even large photo batches never touch a backend server.
Try it locally with zero uploads: Compress Image below 200 KB (21s browser demo) · Convert HEIC, AVIF, PNG & WebP (25s browser demo).

