{"record":{"id":"b6d3ad7835fad357","repo":"Intervention/image","slug":"invalid-trim-tolerance-must-be-int-0-max","errorCode":null,"errorMessage":"Invalid trim tolerance. Must be int<0, max>","messagePattern":"Invalid trim tolerance\\. Must be int<0, max>","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Modifiers/TrimModifier.php","lineNumber":20,"sourceCode":"\ndeclare(strict_types=1);\n\nnamespace Intervention\\Image\\Modifiers;\n\nuse Intervention\\Image\\Drivers\\SpecializableModifier;\nuse Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\nclass TrimModifier extends SpecializableModifier\n{\n    /**\n     * Create new modifier object.\n     *\n     * @throws InvalidArgumentException\n     */\n    public function __construct(public int $tolerance = 0)\n    {\n        if ($this->tolerance < 0) {\n            throw new InvalidArgumentException('Invalid trim tolerance. Must be int<0, max>');\n        }\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":24,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Modifiers/TrimModifier.php#L2-L24","documentation":"TrimModifier backs $image->trim($tolerance) and removes a uniform border around the image; tolerance defines how much neighboring colors may deviate from the border color and still get trimmed. It must be a non-negative int (int<0, max> in PHPStan range notation); a negative tolerance has no meaning, so the constructor throws InvalidArgumentException.","triggerScenarios":"Calling $image->trim(-1) or $image->trim(tolerance: $config['tolerance']) where the config value is negative. Also new TrimModifier(-5) directly.","commonSituations":"Config files with mistyped negative tolerances; user-facing 'trim sensitivity' inputs where higher sensitivity was mapped to negative numbers; tolerance values passed through arithmetic that can go below zero.","solutions":["Clamp the tolerance before calling: $image->trim(max(0, $tolerance))","Validate config/user input for trim tolerance as int >= 0 and reject or normalize negatives","If a negative input means 'do not trim', skip the trim() call when the value is negative"],"exampleFix":"// before\n$image->trim($settings['trim_tolerance']);\n\n// after\n$image->trim(max(0, (int) $settings['trim_tolerance']));","handlingStrategy":"validation","validationCode":"// Clamp trim tolerance before calling\n$tolerance = max(0, (int) $settings['trim_tolerance']);\n$image->trim($tolerance);","typeGuard":"function isValidTrimTolerance(mixed $tolerance): bool\n{\n    return is_int($tolerance) && $tolerance >= 0;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $image->trim($tolerance);\n} catch (InvalidArgumentException $e) {\n    // message: 'Invalid trim tolerance. Must be int<0, max>'\n    $image->trim(0);\n}","preventionTips":["Define trim tolerance as an unsigned int (min: 0) in configs and API schemas","Clamp user input with max(0, (int) $value)","Remember tolerance 0 is valid and means exact color match"],"tags":["trim","constructor","argument-validation","intervention-image"],"backgroundTag":"out-of-range-argument","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}