Intervention/image · error · InvalidArgumentException

Invalid target size {width}x{height}

Error message

Invalid target size {width}x{height}

What it means

Size::resize(width, height) delegates to the Resizer helper, whose constructor requires any non-null target dimension to be >= 1 (Resizer.php:22-32). When the Resizer rejects the input, Size::resize catches that InvalidArgumentException and rethrows it as 'Invalid target size {width}x{height}' with the original exception chained as previous, so the message always names the exact target that failed.

Source

Thrown at src/Size.php:304

     */
    public function orientation(): Orientation
    {
        return Orientation::fromSize($this);
    }

    /**
     * {@inheritdoc}
     *
     * @see SizeInterface::resize()
     *
     * @throws InvalidArgumentException
     */
    public function resize(?int $width = null, ?int $height = null): SizeInterface
    {
        try {
            return $this->resizer($width, $height)->resize($this);
        } catch (InvalidArgumentException $e) {
            throw new InvalidArgumentException(
                'Invalid target size ' . $width . 'x' . $height,
                previous: $e,
            );
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see SizeInterface::resizeDown()
     *
     * @throws InvalidArgumentException
     */
    public function resizeDown(?int $width = null, ?int $height = null): SizeInterface
    {
        try {
            return $this->resizer($width, $height)->resizeDown($this);
        } catch (InvalidArgumentException $e) {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Pass null instead of 0 for dimensions you want to be calculated automatically, and ensure passed ints are >= 1
  2. Clamp computed targets: $width = max(1, (int) $width)
  3. Validate resize parameters at the API boundary as positive integers

Example fix

// before
$size->resize(0, 300); // 0 is not a valid target

// after
$size->resize(null, 300); // height-only resize, width auto
Defensive patterns

Strategy: validation

Validate before calling

// Normalize resize targets: null = auto, ints must be >= 1
$width = isset($input['width']) && $input['width'] > 0 ? (int) $input['width'] : null;
$height = isset($input['height']) && $input['height'] > 0 ? (int) $input['height'] : null;

$size->resize($width, $height);

Type guard

function isValidResizeTarget(null|int $value): bool
{
    return $value === null || $value >= 1;
}

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException;

try {
    $size->resize($width, $height);
} catch (InvalidArgumentException $e) {
    // message: 'Invalid target size {width}x{height}' — inspect $e->getPrevious() for the root cause
    $size->resize(null, max(1, (int) $height));
}

Prevention

When it happens

Trigger: Calling $size->resize(-1, -1), $size->resize(0, 300), or $size->resize(null, 0) — any target dimension that is a non-null int < 1. Equivalently via the image API, $image->resize(0, 300) reaches the same Resizer validation. Note that null/null is caught earlier by ResizeModifier's own check.

Common situations: Resize targets taken from user input where 0 means 'not provided'; arithmetic like width - padding that can drop below 1; percentage-based sizing that rounds a small image down to 0; API defaults of 0 used as 'unlimited'.

Related errors


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