{"record":{"id":"a4be0464fc4086b8","repo":"gchq/CyberChef","slug":"error-adding-text-to-image-err","errorCode":null,"errorMessage":"Error adding text to image. (${err})","messagePattern":"Error adding text to image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AddTextToImage.mjs","lineNumber":284,"sourceCode":"                case \"Top\":\n                    yPos = 0;\n                    break;\n                case \"Middle\":\n                    yPos = image.height / 2 - textImage.height / 2;\n                    break;\n                case \"Bottom\":\n                    yPos = image.height - textImage.height;\n                    break;\n            }\n\n            // Blit the rendered text image onto the original source image\n            image.blit({\n                src: textImage,\n                x: xPos,\n                y: yPos,\n            });\n        } catch (err) {\n            throw new OperationError(`Error adding text to image. (${err})`);\n        }\n\n        try {\n            let imageBuffer;\n            if (image.mime === \"image/gif\") {\n                imageBuffer = await image.getBuffer(JimpMime.png);\n            } else {\n                imageBuffer = await image.getBuffer(image.mime);\n            }\n            return imageBuffer.buffer;\n        } catch (err) {\n            throw new OperationError(`Error exporting image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the blurred image using HTML for web apps\n     *","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AddTextToImage.mjs#L266-L302","documentation":"AddTextToImage measures the text, prints it onto a temporary Jimp image, computes x/y placement, then blits that text image onto the source with image.blit(...). Any failure in measure/print/blit — out-of-bounds coordinates, a zero-size text image, or a Jimp internal error — is caught and rethrown as 'Error adding text to image.'","triggerScenarios":"x/y position arguments place the blit outside image bounds in a way Jimp rejects; the text image has zero width/height (empty text or measurement failure); an incompatible Jimp bitmap format between text image and source.","commonSituations":"Absolute x/y larger than image dimensions combined with a large font; empty text string producing a degenerate text image; hAlign/vAlign plus a negative offset pushing coordinates out of range.","solutions":["Provide non-empty text and sane x/y within the source image bounds.","If using absolute positioning, ensure 0 ≤ x ≤ width and 0 ≤ y ≤ height after alignment.","Confirm the fontFace value matches one of the four bundled fonts."],"exampleFix":"// before: xPos=9999 on a 200x200 image, empty text → blit fails\n// after: keep coordinates in-bounds and supply non-empty text","handlingStrategy":"try-catch","validationCode":"function clampPosition(img, x, y, w, h) {\n  if (x < 0 || y < 0 || x + w > img.width || y + h > img.height) {\n    throw new Error('Text position out of image bounds');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await addTextToImage(input, ...);\n} catch (e) {\n  if (/Error adding text to image/.test(e.message)) {\n    // fix coordinates/text and retry\n    throw new Error('Text rendering/placement failed — check coordinates, text, and fontFace');\n  }\n  throw e;\n}","preventionTips":["Keep x/y within image bounds after alignment.","Provide non-empty text.","Use one of the four bundled fontFace values."],"tags":["image","jimp","rendering","blit"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}