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() validates that both dimensions are at least 1 pixel; zero or negative width or height throws InvalidArgumentException before GD is even touched. It is a pure input-contract violation, not an environment problem.

Source

Thrown at src/Drivers/Gd/Driver.php:60

        if (!extension_loaded('gd') || !function_exists('gd_info')) {
            throw new MissingDependencyException(
                'GD 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>');
        }

        // build new transparent GDImage
        $data = imagecreatetruecolor($width, $height);
        if (!$data instanceof GDImage) {
            throw new DriverException('Failed to create new image');
        }

        imagesavealpha($data, true);
        $background = imagecolorallocatealpha($data, 255, 255, 255, 127);

        imagealphablending($data, false);
        imagefill($data, 0, 0, $background);
        imagecolortransparent($data, $background);
        imageresolution($data, 72, 72);

        return new Image($this, new Core([new Frame($data)]));
    }

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Validate and clamp dimensions to >= 1 before calling create()/createImage()
  2. Default to a sane fallback size when the input is missing or unparseable
  3. Reject non-numeric or zero-sized requests earlier at the API boundary
  4. Log the offending values so the faulty computation is traceable

Example fix

// before
$width = (int) ($_GET['w'] ?? 0);
$image = $manager->create($width, $width); // 0 => throws

// after
$width = max(1, min(4096, (int) ($_GET['w'] ?: 200)));
$image = $manager->create($width, $width);
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try {
    $image = $manager->create($w, $h);
} catch (InvalidArgumentException $e) {
    $image = $manager->create(1, 1); // degenerate fallback, or re-prompt user
}

Prevention

When it happens

Trigger: $manager->create(0, 300) or createImage(-10, 100); dimensions computed from user input that default to 0; (int) cast of null or '' producing 0; aspect-ratio or crop arithmetic rounding down to 0.

Common situations: Thumbnail sizes taken from query parameters (?w=0), null coalescing that falls through to 0, crop-region math on very thin images producing 0 height, form fields left empty.

Related errors


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