// engineering notes2026-11-09

How Squoosh-Style Image Compression Works in Your Browser (WebAssembly + Workers)

Learn why HTML5 Canvas fails at image compression and how FinalImg runs MozJPEG, libwebp, libavif, and OxiPNG via WebAssembly in a Web Worker.

TL;DR. A canvas can draw a picture but it cannot choose the encoder, so canvas.toBlob() hands you a file that depends on the browser you happen to be using. FinalImg's compress page runs MozJPEG, libwebp, libavif and OxiPNG as WebAssembly inside a Web Worker, which means the file you download is the file the encoder wrote, and the size on screen is measured from the bytes that were actually produced.

Every image tool has to answer one question before it does anything: who actually encodes the file? For a long time the honest answer for a browser tool was "the browser". Draw the picture on a canvas, call canvas.toBlob(), and let the engine's built-in encoder write the bytes. It is fast, it needs no dependency, and it is what a lot of "compress image" pages are doing under the hood.

The compress page on this site used to do exactly that. It no longer does. This post is about why, and about the architecture it uses instead — the one Squoosh popularised: real image codecs, compiled to WebAssembly, running in a Web Worker, inside the tab.

The canvas is a renderer, not a compressor

HTMLCanvasElement.toBlob() is a convenience wrapper around whatever encoder the browser ships. That has three consequences that matter on a page whose whole promise is the file you download.

First, the output depends on the browser. Chrome, Safari and Firefox each embed their own encoders (and their own versions of them), so the same pixels and the same quality argument can produce a different file on a different machine — and a different order of bytes for the same picture. A page that prints "smaller by 62%" is making a claim about a number it did not compute with the encoder it thinks it is using.

Second, the controls are shallow. The type and quality arguments are defined for the formats the platform supports, and support is uneven: JPEG honours a quality, WebP usually does, PNG is lossless and ignores it, and writing AVIF from a canvas is not something you can rely on. There is no way to ask for "OxiPNG at effort 4", because that encoder is not there.

Third, the canvas is the picture you drew, not the picture you think you drew. A resize, a color transform, or an alpha channel composited into a white background all happen before the encoder sees the pixels, and none of them are visible in the file's metadata. The result is fine until it is not, and then there is nothing to inspect.

What Squoosh changed

Squoosh, the Chrome Labs image compression app, made a different bet: treat the browser as a host for the same encoders a desktop tool would use. It compiles codecs such as MozJPEG, libwebp, libavif and OxiPNG to WebAssembly, runs them in a Worker, and keeps the file on the visitor's machine. The UI is famous; the architecture is the part worth copying.

FinalImg's compress page uses the same idea through jSquash, the npm packages that repackage the Squoosh codecs for use outside the Squoosh app. The codecs themselves are the upstream projects:

That is not a branding exercise. It means the page can offer a real quality scale for the lossy formats, a real optimization effort for PNG, AVIF as an output at all, and a file whose size is measured from the bytes that were actually written.

The pipeline, step by step

The worker does one pass per request, in this order.

1. Decode. The original file arrives as an ArrayBuffer. It is decoded with createImageBitmap(), with the EXIF orientation applied — a portrait photo taken on a phone comes out upright, not sideways. The bitmap is drawn into an OffscreenCanvas and read back as RGBA pixels. Decoding with the browser, rather than with a codec, is deliberate: the browser already knows how to read the input formats and has the platform's metadata handling.

2. Resize, if asked. The page offers a longest edge of 1920, 1280 or 800 pixels. The downscale uses Lanczos3 in linear light with premultiplied alpha — the resize Squoosh uses. Doing it before encoding matters: shrinking first reduces the number of pixels the encoder has to spend bits on, while shrinking after encoding means you paid for detail and then threw it away.

3. Encode, with the codec the visitor chose.

  • JPEG is written by MozJPEG, which is a JPEG encoder built around transform coding and quantisation — the quality value drives how much of the high-frequency detail is quantised away.
  • WebP is written by libwebp; the format has a lossy and a lossless mode, and the page uses the lossy one for the quality slider. The WebP documentation is the reference for what the format stores.
  • AVIF is written by libavif. AVIF stores an AV1 intra-frame image, which is why it is usually smaller than JPEG or WebP at the same visual quality on photographs — and also why it takes longer to encode. The page fixes libavif's speed setting at its default in exchange for a predictable wait.
  • PNG is written losslessly by OxiPNG from the raw pixels: it is not a quality trade, it is a search over filters and compression strategies for a smaller way to store the same pixels. That is why the control is called effort (1 to 6), not quality: a higher effort spends more time looking, and the result is the same picture.

4. Measure the real file. The worker sends back the encoded bytes, their format, and the dimensions of the export. The readout on the page uses those numbers — not the canvas's — so "1200 × 800 px · 84 KB" describes the file you download. The compare slider goes one step further: it decodes the encoded file for the right-hand side of the split, so what you are comparing against the original is the encoder's output, artefacts included, rather than a canvas copy of the source.

Why a Worker

Encoding a 12-megapixel photograph is not instant. JPEG takes a fraction of a second; AVIF can take seconds. On the main thread the page would freeze while that runs — no scroll, no hover, no way to change your mind. Moving the pipeline into a Worker keeps the interface responsive, which is the entire difference between a tool that feels fast and a tool that feels broken.

The source bytes are handed to the Worker as a transferable object: the ArrayBuffer is moved, not copied, so a 20 MB photograph does not pay for a second copy crossing the thread boundary. The Web Workers API is the relevant documentation, and the WASM side is ordinary WebAssembly — compile once, call many times.

The codecs load lazily, one per format. Exporting a JPEG downloads the MozJPEG wasm (about 250 KB) and nothing else; the first AVIF export is the one that downloads libavif's 3.4 MB. The files are served from the site's own origin and compiled once per session. A visitor who never touches the AVIF button never pays for it.

The details that are easy to get wrong

  • JPEG has no alpha channel. A transparent cut-out that is encoded as JPEG without a matte comes back as a black rectangle. The worker paints the source onto a white background before the JPEG encoder sees it.
  • A closed ImageBitmap has no size. bitmap.close() releases GPU memory, but after it the bitmap reports 0 × 0. This is not a theoretical bug: a version of this page read the dimensions after closing the bitmap and sent a 1 × 1 pixel image to the encoder. Reading the size first is now a comment in the code and an end-to-end assertion on the downloaded file.
  • Slider drags are not requests. Dragging the quality slider from 75 to 40 passes through a lot of values. The page debounces the request, the client supersedes older ones, and the worker runs one encode at a time and skips the queued work it has been overtaken on. WebAssembly codecs are not reentrant; running two encodes at once is not a race you win.
  • A stale file must never be downloadable. The download button unlocks only for the export that matches the current file and the current settings. Otherwise a slow AVIF encode that lands after you changed the format would be saved under the new extension.
  • PNG can be bigger than the JPEG you started from. Lossless means keeping every pixel. The panel reports "X% bigger" rather than hiding it behind a zero, because the honest number is more useful than a comforting one.

What this architecture does not give us

No architecture is free, and the limits are worth stating as plainly as the benefits.

No multithreaded codecs. libavif and OxiPNG can use threads, but WebAssembly threads need SharedArrayBuffer, and SharedArrayBuffer needs cross-origin isolation — the COOP and COEP headers described in web.dev's cross-origin isolation guide. This site does not send those headers, so the codecs run in their single-threaded builds. The consequence is a longer wait on large AVIF exports, not a different file.

A Content-Security-Policy has to allow WebAssembly. Compiling wasm at runtime is blocked by a strict script-src unless the policy includes 'wasm-unsafe-eval'; the production policy on this site does (see the MDN reference for script-src). If you tighten the policy later, this page is the first thing to break.

A codec is a download. The encoders are 165 KB to 3.4 MB of wasm. Lazy loading keeps that off the pages that do not compress, and per-format loading keeps it off the formats you do not export, but the first AVIF export is still a network request and a compile.

Not every codec Squoosh ships is here. Squoosh also exposes JPEG XL and more encoder-specific knobs. This page ships four codecs with the controls that matter most, and says so in the tool rather than pretending the list is complete.

Why it is worth it

The file you get is the file the encoder wrote, measured by the same code that produced it; the picture never leaves your device; the result does not depend on which browser encoded it; and there is no server to pay for, rate-limit, or delete afterwards. Those properties are not features you can add to a canvas.toBlob() call — they come from choosing the pipeline that produces them.

If you want to see the difference rather than read about it, the compress image page is the same tool described here.

References

Try it on this site

The Compress Image tool is the page this post describes: the same four codecs, the same quality scale, the same worker, and the picture never leaves your device. The Batch compress image tool runs the same pipeline over up to 20 pictures at once and hands the whole set back as one archive.

Both pages report the size of the file that was actually written, and the compare slider shows the encoder's output rather than a canvas copy of the source. Open one with a photograph you already have — that is the quickest way to see the difference.