gchq/CyberChef · error · OperationError

Error containing image. (${err})

Error message

Error containing image. (${err})

What it means

After a successful load, Contain Image runs the letterbox step: image.contain() (plus an optional opaque-background blit) and finally image.getBuffer() to export. Any failure in contain(), blit(), or getBuffer() is caught and re-thrown as 'Error containing image.' The most common driver is invalid dimensions, but encode failures also land here.

Source

Thrown at src/core/operations/ContainImage.mjs:150

                    height,
                    color: 0x000000ff,
                });
                image = newImage.blit({
                    src: image,
                    x: 0,
                    y: 0,
                });
            }

            let imageBuffer;
            if (originalMime === "image/gif") {
                imageBuffer = await image.getBuffer(JimpMime.png);
            } else {
                imageBuffer = await image.getBuffer(originalMime);
            }
            return imageBuffer.buffer;
        } catch (err) {
            throw new OperationError(`Error containing image. (${err})`);
        }
    }

    /**
     * Displays the contained image using HTML for web apps
     * @param {ArrayBuffer} data
     * @returns {html}
     */
    present(data) {
        if (!data.byteLength) return "";
        const dataArray = new Uint8Array(data);

        const type = isImage(dataArray);
        if (!type) {
            throw new OperationError("Invalid file type.");
        }

        return `<img src="data:${type};base64,${toBase64(dataArray)}">`;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Ensure Width and Height are integers >= 1 (and reasonably sized for memory).
  2. Use one of the supported resizing algorithm options (Nearest Neighbour, Bilinear, Bicubic, Hermite, Bezier).
  3. For very large images, reduce target dimensions or downscale in stages.

Example fix

// before
const args = [0, 0, "Center", "Middle", "Bilinear", true]; // w=0,h=0
// after
const args = [100, 100, "Center", "Middle", "Bilinear", true];
Defensive patterns

Strategy: validation

Validate before calling

function validDims(width, height) {
  return Number.isInteger(width) && Number.isInteger(height) && width >= 1 && height >= 1;
}
if (!validDims(width, height)) {
  throw new Error(`Invalid contain dimensions: width=${width}, height=${height}`);
}
const ALGS = ["Nearest Neighbour", "Bilinear", "Bicubic", "Hermite", "Bezier"];
if (!ALGS.includes(alg)) {
  throw new Error(`Unsupported resize algorithm: ${alg}`);
}

Type guard

function isContainArgs(width, height, alg) {
  return Number.isInteger(width) && width >= 1 &&
    Number.isInteger(height) && height >= 1 &&
    ["Nearest Neighbour", "Bilinear", "Bicubic", "Hermite", "Bezier"].includes(alg);
}

Try / catch

try {
  result = await containImage.run(input, args);
} catch (err) {
  if (/Error containing image/.test(err.message)) {
    // Most often width/height <= 0 or an unsupported algorithm. Re-check args.
  }
  throw err;
}

Prevention

When it happens

Trigger: Width or height of 0 or negative; extremely large dimensions exhausting memory; an unsupported resize algorithm string mapping to undefined; getBuffer encode failure when re-encoding to the original mime.

Common situations: Recipe/script passing width=0 or height=0 (the arg min is 1 but a bad value can still arrive); huge target sizes; preserving an output mime Jimp can re-encode poorly.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/ecdae3f73bc52fa1. Report an issue: GitHub.