toeverything/AFFiNE · error · Error

Image could not be decoded

Error message

Image could not be decoded

What it means

decodeAndReduceImage loads the file into an Image and awaits img.decode(); if decoding fails (corrupt or unsupported image data) the catch revokes the object URL and throws this Error before any reduction is attempted.

Source

Thrown at packages/frontend/core/src/utils/reduce-image.ts:13

import reduce from 'image-blob-reduce';

// validate and reduce image size and return as file
export const validateAndReduceImage = async (file: File): Promise<File> => {
  // Declare a new async function that wraps the decode logic
  const decodeAndReduceImage = async (): Promise<Blob> => {
    const img = new Image();
    const url = URL.createObjectURL(file);
    img.src = url;

    await img.decode().catch(() => {
      URL.revokeObjectURL(url);
      throw new Error('Image could not be decoded');
    });

    img.onload = img.onerror = () => {
      URL.revokeObjectURL(url);
    };

    const sizeInMB = file.size / (1024 * 1024);
    if (sizeInMB > 10 || img.width > 4000 || img.height > 4000) {
      // Compress the file to less than 10MB

      try {
        const compressedImg = await reduce().toBlob(file, {
          max: 4000,
          unsharpAmount: 80,
          unsharpRadius: 0.6,
          unsharpThreshold: 2,
        });
        return compressedImg;

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Check the image file is a supported, non-corrupt format.
  2. Convert the image to PNG/JPEG and retry.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown in validateAndReduceImage when img.decode() rejects, i.e. the browser cannot decode the selected file as an image before size reduction.

Common situations: A user uploads a file that is not a valid or supported image (corrupted file or unsupported format). Choose a standard image format such as PNG or JPEG and retry.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/8bc2703c7fbd2218. Report an issue: GitHub.