Intervention/image · error · InvalidArgumentException
Width of must be greater than or equal to 0
Error message
Width of {class} must be greater than or equal to 0 What it means
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.
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
Example fix
// before $size = new Size($x2 - $x1, $y2 - $y1); // negative when x2 < x1 // after $size = new Size(abs($x2 - $x1), abs($y2 - $y1));
Defensive patterns
Strategy: validation
Validate before calling
// Validate dimensions before building geometry $width = max(0, (int) $input['width']); $height = max(0, (int) $input['height']); $size = new \Intervention\Image\Size($width, $height);
Type guard
function isValidDimension(mixed $value): bool
{
return is_int($value) && $value >= 0;
} Try / catch
use Intervention\Image\Exceptions\InvalidArgumentException;
try {
$size = new Size($width, $height);
} catch (InvalidArgumentException $e) {
// message: 'Width of ... must be greater than or equal to 0'
$size = new Size(abs($width), abs($height));
} Prevention
- 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')
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Height of must be greater than or equal to 0
- At least one argument must be provided: width, height, or…
- Invalid target size x
- Invalid sharpening level. Must be int<0, max>
- Invalid trim tolerance. Must be int<0, max>
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/62a90da0a4f34af9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Size.php:37
/**
* @implements IteratorAggregate<int>
* @implements ArrayAccess<int|string, int>
*/
class Size implements SizeInterface, ArrayAccess, IteratorAggregate
{
/**
* Create new size instance.
*
* @throws InvalidArgumentException
*/
public function __construct(
protected int $width,
protected int $height,
protected PointInterface $pivot = new Point(),
) {
if ($width < 0) {
throw new InvalidArgumentException(
'Width of ' . $this::class . ' must be greater than or equal to 0',
);
}
if ($height < 0) {
throw new InvalidArgumentException(
'Height of ' . $this::class . ' must be greater than or equal to 0',
);
}
}
/**
* Create size statically.
*
* @throws InvalidArgumentException
*/
public static function create(int $width, int $height, PointInterface $pivot = new Point()): self
{View on GitHub (pinned to 5598b9e397)