Intervention/image · error · DriverException

Failed to create new image

Error message

Failed to create new image

What it means

imagecreatetruecolor() returned something that is not a GdImage, which in practice means GD failed to allocate the truecolor canvas — almost always memory exhaustion, since GD needs roughly width*height*4 bytes plus overhead. The InvalidArgumentException on size has already passed at this point.

Source

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

    /**
     * {@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)]));
    }

    /**
     * {@inheritdoc}
     *
     * @see DriverInterface::createCore()
     */

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Raise memory_limit (ini_set or pool config) to comfortably cover width*height*4 bytes plus overhead
  2. Reduce the target canvas dimensions or process the output in tiles
  3. unset() finished images so GC can free earlier canvases in long-running workers
  4. Estimate required bytes before creating and reject or downscale oversized requests

Example fix

// before
$image = $manager->create(15000, 15000); // ~900 MB buffer => DriverException

// after
ini_set('memory_limit', '2G');
$image = $manager->create(15000, 15000);
Defensive patterns

Strategy: validation

Validate before calling

$needed = $width * $height * 4;
$limitBytes = parseBytes(ini_get('memory_limit')); // helper for '256M' etc.
if ($needed > 0.8 * $limitBytes - memory_get_usage()) {
    throw new RuntimeException('Canvas too large for current memory_limit');
}

Try / catch

try {
    $image = $manager->create($w, $h);
} catch (DriverException $e) {
    // reduce dimensions, or raise memory_limit and retry once
}

Prevention

When it happens

Trigger: Creating very large canvases, e.g. $manager->create(15000, 15000) (~900 MB of pixel buffer) under a low memory_limit; creating a moderate canvas inside a worker that already holds many decoded images.

Common situations: Default memory_limit=128M on shared hosting, generating print-sized posters, batch workers accumulating Image instances until cumulative usage tips over the limit.

Related errors


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