Every image converter on the web works the same way: pick a file, upload it to a server, wait, download the result. It's a reasonable default — until you remember that modern browsers already ship a GPU-accelerated image pipeline, a WASM runtime, and (as of a couple of years ago) native AVIF/WebP codecs. There's no technical reason a JPG-to-WebP or HEIC-to-JPG conversion needs a server at all.

I build dailysolution.org, a set of free browser-only tools, and the image converter/resizer pages there do everything in-tab. No upload, no server round-trip, nothing stored anywhere. Here's how the pieces fit together.

Step 1: never trust the file extension

The first bug I hit was trusting file.name or file.type to know what a user actually uploaded. Phones routinely hand you a .jpg that's secretly a HEIC capture, or a .png that's really a WebP someone renamed. Any format-detection logic built on the extension breaks the moment a real user shows up.

The fix is to read the first few bytes of the file and match them against each format's magic number, the same way the file command on Linux works:

const head = new Uint8Array(await file.slice(0, 32).arrayBuffer()); if (head[0] === 0xff && head[1] === 0xd8) return "jpg"; if (head[0] === 0x89 && ascii(1, 4) === "PNG") return "png"; if (ascii(0, 4) === "RIFF" && ascii(8, 12) === "WEBP") return "webp"; 

HEIC and AVIF are trickier since they're both ISO-BMFF containers (the same wrapper format MP4 uses). You have to look past the container header into the ftyp box and check the brand string:

if (ascii(4, 8) === "ftyp") { const brands = ascii(8, 32); if (/avif|avis/.test(brands)) return "avif"; if (/heic|heix|hevc|hevx|heim|heis|mif1|msf1/.test(brands)) return "heic"; } 

Only once the sniff comes back empty do we fall back to the file extension. This one change quietly fixed a whole category of "why did my conversion come out wrong" reports.

Step 2: decoding, and why HEIC is the odd one out

Once the real format is known, decoding is usually one line:

const bitmap = await createImageBitmap(blob); 

createImageBitmap is implemented natively in every modern browser and handles JPG, PNG, WebP, GIF and (increasingly) AVIF without any extra code. It's also off-main-thread-friendly, so large photos don't freeze the UI.

HEIC is the exception. Outside Safari, browsers simply refuse to decode it — there's no native codec, full stop. The practical fix is a WASM build of libheif, surfaced through the heic2any package:

const { default: heic2any } = await import("heic2any"); const out = await heic2any({ blob: file, toType: "image/png" }); 

It's a dynamic import deliberately: the HEIC decoder is a few hundred KB of WASM, and loading it eagerly would tax every visitor converting a plain JPG. Code-splitting it behind the one code path that needs it keeps the common case fast.

Step 3: encoding to formats the canvas API won't touch

canvas.toBlob() covers JPG, PNG and WebP everywhere, which is most of what people ask for. AVIF is where it falls apart — Chrome and Firefox still don't expose an AVIF encoder through the canvas API, only a decoder. For that one format we pull in @jsquash/avif, which wraps libavif compiled to WASM:

const { encode } = await import("@jsquash/avif");const avifBytes = await encode(imageData); // raw pixels in, .avif bytes out 

Same deliberate-dynamic-import logic as HEIC: the multithreaded AVIF encoder ships its own worker file, and it only loads when someone actually picks AVIF as the output.

Why bother with any of this

Server-side conversion isn't wrong, it's just a trade you don't have to make for most image work anymore. Doing it in the browser means:

  • the photo never leaves the device — relevant the moment the input is a screenshot of a form, an ID, or anything else a user would rather not upload
  • there's no queue, no file-size limit tied to a server timeout, and no cold-start latency
  • it scales to zero — a static page costs nothing to serve regardless of how many conversions run against it

The trade-off is real too: you're shipping a WASM decoder to the client instead of running it once on a server, and you're at the mercy of whatever the browser actually supports. But for the image-conversion case specifically, the browser's own primitives (createImageBitmap, canvas, and two small WASM libraries for the gaps) turn out to be enough to cover the whole format matrix — HEIC and AVIF included — without a single server involved.