{"record":{"id":"ecdae3f73bc52fa1","repo":"gchq/CyberChef","slug":"error-containing-image-err","errorCode":null,"errorMessage":"Error containing image. (${err})","messagePattern":"Error containing image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ContainImage.mjs","lineNumber":150,"sourceCode":"                    height,\n                    color: 0x000000ff,\n                });\n                image = newImage.blit({\n                    src: image,\n                    x: 0,\n                    y: 0,\n                });\n            }\n\n            let imageBuffer;\n            if (originalMime === \"image/gif\") {\n                imageBuffer = await image.getBuffer(JimpMime.png);\n            } else {\n                imageBuffer = await image.getBuffer(originalMime);\n            }\n            return imageBuffer.buffer;\n        } catch (err) {\n            throw new OperationError(`Error containing image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the contained image using HTML for web apps\n     * @param {ArrayBuffer} data\n     * @returns {html}\n     */\n    present(data) {\n        if (!data.byteLength) return \"\";\n        const dataArray = new Uint8Array(data);\n\n        const type = isImage(dataArray);\n        if (!type) {\n            throw new OperationError(\"Invalid file type.\");\n        }\n\n        return `<img src=\"data:${type};base64,${toBase64(dataArray)}\">`;","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ContainImage.mjs#L132-L168","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure Width and Height are integers >= 1 (and reasonably sized for memory).","Use one of the supported resizing algorithm options (Nearest Neighbour, Bilinear, Bicubic, Hermite, Bezier).","For very large images, reduce target dimensions or downscale in stages."],"exampleFix":"// before\nconst args = [0, 0, \"Center\", \"Middle\", \"Bilinear\", true]; // w=0,h=0\n// after\nconst args = [100, 100, \"Center\", \"Middle\", \"Bilinear\", true];","handlingStrategy":"validation","validationCode":"function validDims(width, height) {\n  return Number.isInteger(width) && Number.isInteger(height) && width >= 1 && height >= 1;\n}\nif (!validDims(width, height)) {\n  throw new Error(`Invalid contain dimensions: width=${width}, height=${height}`);\n}\nconst ALGS = [\"Nearest Neighbour\", \"Bilinear\", \"Bicubic\", \"Hermite\", \"Bezier\"];\nif (!ALGS.includes(alg)) {\n  throw new Error(`Unsupported resize algorithm: ${alg}`);\n}","typeGuard":"function isContainArgs(width, height, alg) {\n  return Number.isInteger(width) && width >= 1 &&\n    Number.isInteger(height) && height >= 1 &&\n    [\"Nearest Neighbour\", \"Bilinear\", \"Bicubic\", \"Hermite\", \"Bezier\"].includes(alg);\n}","tryCatchPattern":"try {\n  result = await containImage.run(input, args);\n} catch (err) {\n  if (/Error containing image/.test(err.message)) {\n    // Most often width/height <= 0 or an unsupported algorithm. Re-check args.\n  }\n  throw err;\n}","preventionTips":["Keep Width and Height as integers >= 1 and memory-reasonable.","Use only the supported resizing-algorithm option strings.","Cap target dimensions for very large sources to avoid memory exhaustion."],"tags":["cyberchef","image","jimp","resize","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}