Intervention/image · error · InvalidArgumentException

Width of {class} 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.

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)

Solutions

  1. Validate dimensions as int >= 0 before constructing Size
  2. Use abs() or reorder coordinates when a delta is legitimately invertible
  3. 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

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


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/62a90da0a4f34af9. Report an issue: GitHub.