Intervention/image · error · InvalidArgumentException

Height of {class} must be greater than or equal to 0

Error message

Height 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 height to be an int >= 0 (0 is allowed); a negative height is rejected with InvalidArgumentException naming the class. This is the height sibling of the width check that runs just before it in the same constructor.

Source

Thrown at src/Size.php:43

{
    /**
     * 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
    {
        return new self($width, $height, $pivot);
    }

    /**
     * {@inheritdoc}
     *

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 y2 < y1

// 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: 'Height 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 height is negative; passing a computed dimension like $bottom - $top that can go negative when the rectangle is inverted.

Common situations: Crop coordinates where the user swaps y1/y2 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/08ff66027b47825e. Report an issue: GitHub.