DEV Community

I asked canvas for AVIF and it silently handed me a PNG - 10x the size of the WebP

If you ask a browser's canvas for an AVIF, it will hand you a PNG. Not an error. Not null. A perfectly valid PNG, byte-for-byte identical to the one you would have gotten by asking for PNG in the first place - and roughly ten times larger than the WebP you could have had.

I measured this in Chromium 149 (headless) because "just add AVIF to your converter" is advice you see everywhere, and the failure is silent. The code below is the same chain every client-side image tool uses: decode → drawImage → toBlob(type, quality).

const c = document.createElement('canvas');
c.width = img.naturalWidth;
c.height = img.naturalHeight;
c.getContext('2d').drawImage(img, 0, 0);
c.toBlob(blob => {
  console.log(blob.type, blob.size); // image/png 2150964
}, 'image/avif', 0.85);

You asked for image/avif. You got image/png, 2,150,964 bytes, first bytes 89504e470d0a1a0a. That is the PNG magic number. The AVIF magic (ftypavif) never appears.

Requested vs. returned, same canvas, same call

Source: a 1200×800 photo stored as lossless PNG, 1,627,250 bytes. Every row is one toBlob(type, 0.85) on the identical canvas.

Requested blob.type returned Bytes Magic bytes Honoured
image/avif image/png 2,150,964 89504e47… (PNG) no
image/jxl image/png 2,150,964 89504e47… (PNG) no
image/webp image/webp 215,206 52494646… (WebP) yes
image/jpeg image/jpeg 250,498 ffd8ffe0… (JPEG) yes
image/png image/png 2,150,964 89504e47… (PNG) yes

Two things worth noticing. The AVIF request and the JXL request produce the exact same byte count as the PNG request - 2,150,964, three times in a row. And the AVIF-target blob is no smaller than PNG, so the thing people want AVIF for (smaller files) is exactly what you don't get.

Repeat it on a flat graphic (1200×630, 44,915 bytes source) and the pattern holds: image/avif → PNG, 71,663 bytes; image/png → PNG, 71,663 bytes; image/webp at 0.85 → 27,792 bytes.

Quality is ignored too

A quality sweep makes it obvious that the parameter reaches nothing. Four values, same canvas:

Type q=1.00 q=0.85 q=0.50 q=0.10 Distinct sizes
image/avif 2,150,964 2,150,964 2,150,964 2,150,964 1
image/jxl 2,150,964 2,150,964 2,150,964 2,150,964 1
image/webp 1,224,138 215,206 112,616 45,706 4
image/jpeg 1,081,393 250,498 120,207 35,857 4
image/png 2,150,964 2,150,964 2,150,964 2,150,964 1

For AVIF and JXL the output never changes, because the output is never AVIF or JXL. For WebP and JPEG the quality parameter works, and works a lot: WebP goes from 1.2 MB to 45 KB across the same range.

The trap that actually ships bad files

toDataURL behaves the same way, which is where the damage compounds:

canvas.toDataURL('image/avif', 0.85).slice(0, 40)
// "data:image/png;base64,iVBORw0KGgoAAAANSU"

Now consider the very common pattern of building a download name from what you asked for:

const ext = type.split('/')[1]; // "avif"
a.download = `${name}.${ext}`; // photo.avif
a.href = canvas.toDataURL(type, 0.85); // ...containing a PNG

You now ship a file called photo.avif whose contents are PNG. macOS Preview, Windows Photos, and most image viewers sniff the magic bytes and open it anyway, so nobody notices - until something downstream trusts the extension, or a CDN serves it as image/avif and a strict decoder rejects it.

If you're building a converter, take the extension from blob.type, never from the type you requested. This is the same class of bug as building a WebP filename from a JPEG that was re-saved - the size and the format silently disagree with the label, which is exactly what I found when measuring what a JPG-to-WebP conversion really changes.

Decoding works fine - only encoding is missing

The asymmetry matters, because it means the browser can read the format it cannot write. A real 403×302 AVIF sample (21,132 bytes) decodes without complaint and reports its true dimensions. Re-encoding that decoded bitmap:

Target requested Returned Bytes
image/avif image/png 238,385
image/webp image/webp 22,260
image/png image/png 238,385

So an AVIF in, a 238 KB PNG out - or a 22 KB WebP, which is 9.3% of the "AVIF" you asked for.

OffscreenCanvas.convertToBlob() - the path a worker-based tool would use - fails the same way: {type: 'image/avif'} returns image/png at 2,319 bytes for a small test canvas, versus image/webp at 1,010 bytes.

Why Chromium ships decoders for AVIF and JPEG XL but not encoders; encoding AVIF requires the codec to be compiled in, and it isn't. The canvas spec says an unsupported type should fall back - and Chromium's chosen fallback is PNG, silently. Firefox and Safari differ in which formats they encode, so a "convert to AVIF" button that works on one browser produces PNG on another.

That's why client-side image tools that upload nothing usually offer PNG, JPEG and WebP and stop there: those three are the ones you can actually produce in a browser today.

Check your own browser in six lines

Don't trust blob.type alone - it is what the browser reports, which in this case is honest, but a sniff of the magic bytes is what proves it:

const c = document.createElement('canvas');
c.width = c.height = 8;
const blob = await new Promise(r => c.toBlob(r, 'image/avif', 0.85));
const head = new Uint8Array(await blob.slice(0, 12).arrayBuffer());
console.log(blob.type, blob.size, Array.from(head).map(b => b.toString(16).padStart(2, '0')).join(''));
// if it starts 89504e47 you asked for AVIF and got a PNG

Boundaries worth stating: this is one build (HeadlessChrome 149.0.7827.55, Linux), and the headless shell here exposes no WebCodecs at all - ImageDecoder, ImageEncoder, VideoEncoder and VideoDecoder are all undefined, so the ImageDecoder.isTypeSupported() route throws rather than answering. The conclusions above rest on toBlob, toDataURL and OffscreenCanvas.convertToBlob, three APIs that exist everywhere, not on WebCodecs behaviour.

If your tool offers a format it can't encode, the honest fix is a capability probe on page load, and a label on the output that says what it actually is. The numbers above are reproducible in a browser console on any machine - including the part where a PNG comes back labelled as something else, which is also true of what a PNG re-encode really does to your file.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.