{"record":{"id":"366c6ae3b728e97b","repo":"gchq/CyberChef","slug":"error-resizing-image-err","errorCode":null,"errorMessage":"Error resizing image. (${err})","messagePattern":"Error resizing image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ResizeImage.mjs","lineNumber":130,"sourceCode":"                    mode: resizeMap[resizeAlg],\n                });\n            } else {\n                image.resize({\n                    w: width,\n                    h: height,\n                    mode: resizeMap[resizeAlg],\n                });\n            }\n\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 resizing image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the resized 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":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ResizeImage.mjs#L112-L148","documentation":"Thrown by the Resize Image operation when an exception occurs during the Jimp resize/scaleToFit or getBuffer calls inside run(). Any error raised by Jimp (bad dimensions, unsupported algorithm, encoding failure) is caught and re-wrapped as an OperationError with the original error message embedded. GIFs are re-encoded as PNG, which can itself fail.","triggerScenarios":"Calling ResizeImage.run() with a 'Percent' unit that produces NaN/Infinity dimensions (e.g. zero or negative percent), passing an unmapped resize algorithm string so resizeMap[resizeAlg] is undefined, or getBuffer() failing on a corrupted or unsupported image. Large images that exhaust worker memory also surface here.","commonSituations":"Passing width/height of 0 with Percent unit; supplying an image Jimp can decode but not re-encode (e.g. exotic GIF/animated frames); running against very large images in a memory-constrained web worker; a Jimp version change where ResizeStrategy enum names drift.","solutions":["Check the wrapped 'err' string — it carries the underlying Jimp failure and pinpoints resize vs. getBuffer.","Ensure Width/Height are positive numbers; for 'Percent' unit use values 1-100 to avoid fractional/negative pixel dimensions.","If the input is a GIF, convert it to PNG before resizing, since the operation re-encodes GIFs as PNG and animated GIFs can fail.","Reduce the image size or run outside the worker if memory limits are the cause."],"exampleFix":"// before\nResizeImage.run(buffer, [-50, -50, \"Percent\", false, \"Bilinear\"])\n// after\nResizeImage.run(buffer, [50, 50, \"Percent\", false, \"Bilinear\"])","handlingStrategy":"validation","validationCode":"// Validate dimensions before calling run()\nconst [w, h, unit] = args;\nif (unit === \"Percent\" && (w <= 0 || h <= 0 || w > 100 || h > 100)) {\n  throw new Error(\"Percent width/height must be between 1 and 100\");\n}\nif (unit === \"Pixels\" && (w < 1 || h < 1)) {\n  throw new Error(\"Pixel width/height must be >= 1\");\n}\nconst VALID_ALGS = [\"Nearest Neighbour\", \"Bilinear\", \"Bicubic\", \"Hermite\", \"Bezier\"];\nif (!VALID_ALGS.includes(args[4])) {\n  throw new Error(\"Unknown resize algorithm: \" + args[4]);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const out = await resizeImage.run(buf, args);\n} catch (e) {\n  if (e instanceof OperationError && /Error resizing image/.test(e.message)) {\n    // e.message embeds the underlying Jimp error\n  }\n}","preventionTips":["Validate dimensions are positive and (for Percent) within 1-100 before invoking.","Confirm the resize algorithm string is one of the five declared values.","Test with a small PNG before running on large images in a worker."],"tags":["image","jimp","resize","operation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}