{"record":{"id":"f94f8811483bfaf0","repo":"Intervention/image","slug":"quantization-limit-must-be-greater-than-0-f94f88","errorCode":null,"errorMessage":"Quantization limit must be greater than 0","messagePattern":"Quantization limit must be greater than 0","errorType":"validation","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Modifiers/ReduceColorsModifier.php","lineNumber":23,"sourceCode":"namespace Intervention\\Image\\Drivers\\Imagick\\Modifiers;\n\nuse ImagickException;\nuse Intervention\\Image\\Exceptions\\InvalidArgumentException;\nuse Intervention\\Image\\Exceptions\\ModifierException;\nuse Intervention\\Image\\Interfaces\\ImageInterface;\nuse Intervention\\Image\\Interfaces\\SpecializedInterface;\nuse Intervention\\Image\\Modifiers\\ReduceColorsModifier as GenericReduceColorsModifier;\n\nclass ReduceColorsModifier extends GenericReduceColorsModifier implements SpecializedInterface\n{\n    /**\n     * @throws InvalidArgumentException\n     * @throws ModifierException\n     */\n    public function apply(ImageInterface $image): ImageInterface\n    {\n        if ($this->limit <= 0) {\n            throw new InvalidArgumentException('Quantization limit must be greater than 0');\n        }\n\n        // no color reduction if the limit is higher than the colors in the img\n        if ($this->limit > $image->core()->native()->getImageColors()) {\n            return $image;\n        }\n\n        foreach ($image as $frame) {\n            try {\n                $result = $frame->native()->quantizeImage(\n                    $this->limit,\n                    $frame->native()->getImageColorspace(),\n                    0,\n                    false,\n                    false,\n                );\n                if ($result === false) {\n                    throw new ModifierException(","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Modifiers/ReduceColorsModifier.php#L5-L41","documentation":"The Imagick driver's reduceColors() implementation re-checks the quantization limit and throws InvalidArgumentException when it is 0 or negative, because Imagick's quantizeImage() requires a positive color count. Note the generic ReduceColorsModifier constructor already rejects limit < 1 at instantiation with a different message ('Invalid color limit. Must be int<1, max>'), so this driver-level throw is only reachable when the public $limit property is mutated after construction or the specialized modifier is applied by hand. It is a guard against inconsistent state, not normal fluent-API use.","triggerScenarios":"Creating the modifier with a valid limit, then assigning 0/negative to $modifier->limit before apply(); code that constructs specialized modifiers directly and computes the limit from data that can be 0.","commonSituations":"Palette-size parameters derived from user input or image analysis (e.g. 'extract N dominant colors' where N can be 0) written straight into the modifier; refactors that bypass the fluent API and lose constructor validation.","solutions":["Pass the limit through $image->reduceColors($limit) so the constructor validates it once","Clamp computed limits to at least 1: max(1, $limit)","Validate the parameter at the request boundary (integer, min:1) with your framework's validator","If you must build modifiers manually, never mutate $limit after construction"],"exampleFix":"// before\n$modifier->limit = $computedLimit; // 0 from 'dominant color count' analysis\n$modifier->apply($image);\n\n// after\n$image->reduceColors(max(1, $computedLimit));","handlingStrategy":"validation","validationCode":"$limit = (int) $input['colors'];\nif ($limit < 1) {\n    throw new \\InvalidArgumentException('Quantization limit must be >= 1, got ' . $limit);\n}\n$image->reduceColors($limit);","typeGuard":"function isValidQuantizationLimit(int $limit): bool\n{\n    return $limit >= 1;\n}","tryCatchPattern":null,"preventionTips":["Always create the modifier through $image->reduceColors($limit); its constructor enforces int<1, max>","Never overwrite the public $limit property after construction","Validate color-count request parameters with min:1 in your framework's validator"],"tags":["php","intervention-image","reduce-colors","invalidargumentexception","input-validation"],"backgroundTag":"argument-validation-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}