{"record":{"id":"9f4431dfd0e20717","repo":"Intervention/image","slug":"the-specified-position-x-y-is-not-within-th-9f4431","errorCode":null,"errorMessage":"The specified position ({x}, {y}) is not within the image area","messagePattern":"The specified position \\((.+?), (.+?)\\) is not within the image area","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Analyzers/PixelColorAnalyzer.php","lineNumber":28,"sourceCode":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\nuse Intervention\\Image\\Exceptions\\StateException;\nuse Intervention\\Image\\Interfaces\\ColorInterface;\nuse Intervention\\Image\\Interfaces\\ColorProcessorInterface;\nuse Intervention\\Image\\Interfaces\\FrameInterface;\nuse Intervention\\Image\\Interfaces\\ImageInterface;\nuse Intervention\\Image\\Interfaces\\SpecializedInterface;\n\nclass PixelColorAnalyzer extends GenericPixelColorAnalyzer implements SpecializedInterface\n{\n    /**\n     * @throws InvalidArgumentException\n     * @throws AnalyzerException\n     * @throws StateException\n     */\n    public function analyze(ImageInterface $image): mixed\n    {\n        if ($this->x > $image->width() - 1 || $this->y > $image->height() - 1) {\n            throw new InvalidArgumentException(\n                'The specified position (' . $this->x . ', ' . $this->y . ') is not within the image area',\n            );\n        }\n\n        $colorProcessor = $this->driver()->colorProcessor($image);\n\n        return $this->colorAt($colorProcessor, $image->core()->frame($this->frame));\n    }\n\n    /**\n     * @throws AnalyzerException\n     */\n    protected function colorAt(ColorProcessorInterface $processor, FrameInterface $frame): ColorInterface\n    {\n        try {\n            return $processor->import(\n                $frame->native()->getImagePixelColor($this->x, $this->y),\n            );","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Analyzers/PixelColorAnalyzer.php#L10-L46","documentation":"colorAt($x, $y) validates the request before touching Imagick and throws InvalidArgumentException when x exceeds width-1 or y exceeds height-1. Coordinates are zero-based, so the maximum valid coordinate is one less than the dimension; note the guard only covers the upper bound — negative coordinates pass this check and fail later inside Imagick (the 'Failed to read pixel color' error).","triggerScenarios":"Sampling loops written as for ($x = 0; $x <= $image->width(); $x++) colorAt($x, $y); using cached dimensions from before a resize/crop; hard-coded coordinates applied to differently sized images; passing $image->width() as if it were the last column index.","commonSituations":"Edge/border sampling code (border color detection, watermark placement) with off-by-one errors; batch processing of mixed-size images with one fixed coordinate set; coordinates computed from percentages rounded up to the full dimension.","solutions":["Fix the bounds: valid coordinates satisfy 0 <= x < width and 0 <= y < height — use < instead of <= in loops.","Clamp before the call: $x = max(0, min($x, $image->width() - 1)); and the same for y.","Re-read width()/height() after every geometry change instead of caching sizes."],"exampleFix":"// before\nfor ($x = 0; $x <= $image->width(); $x++) {\n    $color = $image->colorAt($x, 0); // x == width is one past the last pixel\n}\n\n// after: the last valid column is width - 1\nfor ($x = 0; $x < $image->width(); $x++) {\n    $color = $image->colorAt($x, 0);\n}","handlingStrategy":"validation","validationCode":"$x = max(0, min($x, $image->width() - 1));\n$y = max(0, min($y, $image->height() - 1));\n$color = $image->colorAt($x, $y);","typeGuard":"function isValidPixel(\\Intervention\\Image\\Interfaces\\ImageInterface $image, int $x, int $y): bool\n{\n    return $x >= 0 && $y >= 0 && $x < $image->width() && $y < $image->height();\n}","tryCatchPattern":null,"preventionTips":["Treat pixel coordinates as zero-based: the last valid column is width - 1.","Recompute dimensions after every resize/crop instead of caching sizes.","Clamp coordinates derived from percentages or user input before sampling."],"tags":["imagick","coordinates","colorat","out-of-bounds","off-by-one"],"backgroundTag":"pixel-coordinate-out-of-bounds","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}