{"record":{"id":"62a90da0a4f34af9","repo":"Intervention/image","slug":"width-of-class-must-be-greater-than-or-equal-to","errorCode":null,"errorMessage":"Width of {class} must be greater than or equal to 0","messagePattern":"Width of (.+?) must be greater than or equal to 0","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Size.php","lineNumber":37,"sourceCode":"\n/**\n * @implements IteratorAggregate<int>\n * @implements ArrayAccess<int|string, int>\n */\nclass Size implements SizeInterface, ArrayAccess, IteratorAggregate\n{\n    /**\n     * Create new size instance.\n     *\n     * @throws InvalidArgumentException\n     */\n    public function __construct(\n        protected int $width,\n        protected int $height,\n        protected PointInterface $pivot = new Point(),\n    ) {\n        if ($width < 0) {\n            throw new InvalidArgumentException(\n                'Width of ' . $this::class . ' must be greater than or equal to 0',\n            );\n        }\n\n        if ($height < 0) {\n            throw new InvalidArgumentException(\n                'Height of ' . $this::class . ' must be greater than or equal to 0',\n            );\n        }\n    }\n\n    /**\n     * Create size statically.\n     *\n     * @throws InvalidArgumentException\n     */\n    public static function create(int $width, int $height, PointInterface $pivot = new Point()): self\n    {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Size.php#L19-L55","documentation":"Size is the core width/height/pivot value object used throughout the library for image and crop dimensions. Its constructor requires width to be an int >= 0 (0 is allowed, e.g. for degenerate sizes); a negative width is rejected with InvalidArgumentException naming the class. The same check exists for height, so exactly which axis is at fault is visible in the message.","triggerScenarios":"Constructing new Size(-100, 50); building crop/resize geometry from user input where width is negative; passing a computed dimension like $right - $left that can go negative when the rectangle is inverted.","commonSituations":"Crop coordinates where the user swaps x1/x2 so the delta becomes negative; image-processing pipelines fed with unvalidated request data; math that assumes an ordering (e.g. larger minus smaller) that the input violates.","solutions":["Validate dimensions as int >= 0 before constructing Size","Use abs() or reorder coordinates when a delta is legitimately invertible","Reject or clamp negative dimensions at the API boundary before they reach geometry code"],"exampleFix":"// before\n$size = new Size($x2 - $x1, $y2 - $y1); // negative when x2 < x1\n\n// after\n$size = new Size(abs($x2 - $x1), abs($y2 - $y1));","handlingStrategy":"validation","validationCode":"// Validate dimensions before building geometry\n$width = max(0, (int) $input['width']);\n$height = max(0, (int) $input['height']);\n\n$size = new \\Intervention\\Image\\Size($width, $height);","typeGuard":"function isValidDimension(mixed $value): bool\n{\n    return is_int($value) && $value >= 0;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $size = new Size($width, $height);\n} catch (InvalidArgumentException $e) {\n    // message: 'Width of ... must be greater than or equal to 0'\n    $size = new Size(abs($width), abs($height));\n}","preventionTips":["Validate width/height as non-negative ints wherever user input enters geometry code","Normalize crop rectangles by ordering coordinates (min/max) before subtracting","The exception message names the failing axis ('Width' vs 'Height')"],"tags":["size","dimensions","constructor","argument-validation","intervention-image"],"backgroundTag":"invalid-image-dimensions","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}