{"record":{"id":"38d90db815fc8afc","repo":"gchq/CyberChef","slug":"error-cropping-image-err","errorCode":null,"errorMessage":"Error cropping image. (${err})","messagePattern":"Error cropping image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/CropImage.mjs","lineNumber":143,"sourceCode":"                });\n            } else {\n                image.crop({\n                    x: xPos,\n                    y: yPos,\n                    w: width,\n                    h: height,\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 cropping image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the cropped 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":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/CropImage.mjs#L125-L161","documentation":"After a successful load, Crop Image runs either image.autocrop() (tolerance-based) or image.crop() (manual x/y/w/h), then image.getBuffer() to export. Any failure in crop/autocrop/getBuffer is caught and re-thrown as 'Error cropping image.' The dominant preventable cause is a crop rectangle outside the image bounds.","triggerScenarios":"x or y negative or beyond image dimensions; width/height of 0 or extending past the edges so the rectangle is invalid; autocrop tolerance producing an empty/degenerate result; getBuffer encode failure.","commonSituations":"Crop rectangle larger than the image; origin past the bottom-right edge; autocrop on a uniform-color image with an aggressive tolerance.","solutions":["Ensure xPos, yPos, width, height form a rectangle fully inside the image (x+w <= imageWidth, y+h <= imageHeight, all >= 0).","For autocrop, use a tolerance that leaves a non-degenerate region.","If unsure of dimensions, run autocrop instead of a manual rectangle, or detect size first."],"exampleFix":"// before: rectangle exceeds image bounds\nconst args = [0, 0, 10000, 10000, false, 0, true, true, false];\n// after: rectangle within the image\nconst args = [10, 10, 200, 200, false, 0, true, true, false];","handlingStrategy":"validation","validationCode":"// Requires known image dimensions; obtain from Jimp.read or Detect Image Size upstream.\nfunction validCropRect(x, y, w, h, imgW, imgH) {\n  return [x, y, w, h].every((v) => Number.isInteger(v) && v >= 0) &&\n    w >= 1 && h >= 1 && x + w <= imgW && y + h <= imgH;\n}\nif (!autocrop && !validCropRect(xPos, yPos, width, height, imgWidth, imgHeight)) {\n  throw new Error(`Crop rectangle (${xPos},${yPos},${width},${height}) outside image (${imgWidth}x${imgHeight})`);\n}","typeGuard":"function isCropRect(x, y, w, h, imgW, imgH) {\n  return Number.isInteger(x) && Number.isInteger(y) &&\n    Number.isInteger(w) && Number.isInteger(h) &&\n    x >= 0 && y >= 0 && w >= 1 && h >= 1 &&\n    x + w <= imgW && y + h <= imgH;\n}","tryCatchPattern":"try {\n  result = await cropImage.run(input, args);\n} catch (err) {\n  if (/Error cropping image/.test(err.message)) {\n    // Likely an out-of-bounds rectangle or degenerate autocrop. Re-check rect vs image size.\n  }\n  throw err;\n}","preventionTips":["Ensure the manual crop rectangle lies fully inside the image (x+w <= width, y+h <= height).","For autocrop, use a tolerance that leaves a non-empty region; avoid uniform-color inputs.","When image dimensions are unknown, detect them first (or use autocrop) before setting a manual rectangle."],"tags":["cyberchef","image","jimp","crop","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}