Intervention/image · error · InvalidArgumentException

Invalid image size. Must be int<1, max>

Error message

Invalid image size. Must be int<1, max>

What it means

Driver::createImage() rejects zero or negative width/height before touching ImageMagick. Reached through ImageManager::create($width, $height) (or APIs that build blank canvases). Typical cause: dimensions computed at runtime - e.g. a percentage of another image's size - rounding down to 0.

Source

Thrown at src/Drivers/Imagick/Driver.php:63

        if (!extension_loaded('imagick') || !class_exists('Imagick')) {
            throw new MissingDependencyException(
                'Imagick PHP extension must be installed to use this driver',
            );
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see DriverInterface::createImage()
     *
     * @throws InvalidArgumentException
     * @throws DriverException
     */
    public function createImage(int $width, int $height): ImageInterface
    {
        if ($width < 1 || $height < 1) {
            throw new InvalidArgumentException('Invalid image size. Must be int<1, max>');
        }

        try {
            $background = new ImagickPixel('rgba(255, 255, 255, 0)');

            $imagick = new Imagick();
            $imagick->newImage($width, $height, $background, 'png');
            $this->applyDefaultSettings($imagick);
        } catch (ImagickException | ImagickPixelException $e) {
            throw new DriverException('Failed to create new image', previous: $e);
        }

        return new Image($this, new Core($imagick));
    }

    /**
     * {@inheritdoc}
     *

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Clamp before calling: max(1, (int) $width) / max(1, (int) $height)
  2. Fix the upstream computation - check the aspect-ratio source for zero dimensions before deriving sizes
  3. Validate/normalize user-supplied sizes (filter_var with FILTER_VALIDATE_INT, options min_range 1)

Example fix

// before
$width = (int) round($source->width() * $ratio); // 0 for tiny sources
$image = $manager->create($width, $height);

// after
$width = max(1, (int) round($source->width() * $ratio));
$image = $manager->create($width, $height);
Defensive patterns

Strategy: validation

Validate before calling

$width = max(1, (int) $width);
$height = max(1, (int) $height);
$image = $manager->create($width, $height);

Prevention

When it happens

Trigger: $manager->create(0, 100); computed sizes like (int) round($source->width() * 0.05) collapsing to 0 for tiny sources; unvalidated user input where (int) 'abc' becomes 0.

Common situations: Thumbnail/resize-to-fit math operating on already-small images; form fields for canvas size missing validation; float-to-int casts of fractions below 1.

Related errors


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