gchq/CyberChef · error · OperationError

Error cropping image. (${err})

Error message

Error cropping image. (${err})

What it means

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.

Source

Thrown at src/core/operations/CropImage.mjs:143

                });
            } else {
                image.crop({
                    x: xPos,
                    y: yPos,
                    w: width,
                    h: height,
                });
            }

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

    /**
     * Displays the cropped 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 xPos, yPos, width, height form a rectangle fully inside the image (x+w <= imageWidth, y+h <= imageHeight, all >= 0).
  2. For autocrop, use a tolerance that leaves a non-degenerate region.
  3. If unsure of dimensions, run autocrop instead of a manual rectangle, or detect size first.

Example fix

// before: rectangle exceeds image bounds
const args = [0, 0, 10000, 10000, false, 0, true, true, false];
// after: rectangle within the image
const args = [10, 10, 200, 200, false, 0, true, true, false];
Defensive patterns

Strategy: validation

Validate before calling

// Requires known image dimensions; obtain from Jimp.read or Detect Image Size upstream.
function validCropRect(x, y, w, h, imgW, imgH) {
  return [x, y, w, h].every((v) => Number.isInteger(v) && v >= 0) &&
    w >= 1 && h >= 1 && x + w <= imgW && y + h <= imgH;
}
if (!autocrop && !validCropRect(xPos, yPos, width, height, imgWidth, imgHeight)) {
  throw new Error(`Crop rectangle (${xPos},${yPos},${width},${height}) outside image (${imgWidth}x${imgHeight})`);
}

Type guard

function isCropRect(x, y, w, h, imgW, imgH) {
  return Number.isInteger(x) && Number.isInteger(y) &&
    Number.isInteger(w) && Number.isInteger(h) &&
    x >= 0 && y >= 0 && w >= 1 && h >= 1 &&
    x + w <= imgW && y + h <= imgH;
}

Try / catch

try {
  result = await cropImage.run(input, args);
} catch (err) {
  if (/Error cropping image/.test(err.message)) {
    // Likely an out-of-bounds rectangle or degenerate autocrop. Re-check rect vs image size.
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: Crop rectangle larger than the image; origin past the bottom-right edge; autocrop on a uniform-color image with an aggressive tolerance.

Related errors


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