Intervention/image · error · DriverException

Failed to create new image while cloning

Error message

Failed to create new image while cloning

What it means

imagecreatetruecolor() returned false while Cloner::cloneEmpty() tried to allocate the new canvas. GD refuses allocation when PHP exceeds memory_limit (a truecolor canvas needs roughly width*height*4 bytes plus overhead) or when dimensions exceed GD's internal limits (well beyond 2^31 pixels or memory available). Every GD operation that duplicates a core (resize, crop, effects, encode) goes through the Cloner, so this surfaces as a DriverException from many modifiers.

Source

Thrown at src/Drivers/Gd/Cloner.php:58

     * @throws InvalidArgumentException
     * @throws DriverException
     */
    public static function cloneEmpty(
        GdImage $gd,
        ?SizeInterface $size = null,
        Color $background = new Color(255, 255, 255, 0),
    ): GdImage {
        // define size
        $size = $size ?: new Size(imagesx($gd), imagesy($gd));

        if ($size->width() < 1 || $size->height() < 1) {
            throw new InvalidArgumentException('Invalid image size');
        }

        // create new gd image with same size or new given size
        $clone = imagecreatetruecolor($size->width(), $size->height());
        if ($clone === false) {
            throw new DriverException('Failed to create new image while cloning');
        }

        // copy resolution to clone
        $resolution = imageresolution($gd);
        if (is_array($resolution) && array_key_exists(0, $resolution) && array_key_exists(1, $resolution)) {
            imageresolution($clone, $resolution[0], $resolution[1]);
        }

        // fill with background
        $processor = new ColorProcessor();

        imagefill($clone, 0, 0, $processor->export($background));
        imagealphablending($clone, true);
        imagesavealpha($clone, true);

        // set background image as transparent if alpha channel value if color is below .5
        // comes into effect when the end format only supports binary transparency (like GIF)
        if ($background->alpha()->value() < .5) {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Raise memory_limit (ini_set('memory_limit', '512M') or PHP_INI_ENV) sized for your largest image: width*height*4*~1.6 bytes
  2. Downscale before heavy operations: read with a limit, or use scale()/resize in stages rather than one huge canvas
  3. Free prior images in batch loops (unset($image) and force GC) so allocations don't stack
  4. Stream/queue very large files to an external tool (ImageMagick, vips) instead of GD

Example fix

// before
$image = $manager->read('huge-panorama.png');
$image->resize(20000, 20000); // Fatal allocation

// after
ini_set('memory_limit', '1G');
$image = $manager->read('huge-panorama.png');
$image->scale(width: 4000); // bounded target
Defensive patterns

Strategy: try-catch

Validate before calling

$bytesNeeded = $width * $height * 4 * 1.6; // truecolor + overhead heuristic
$limit = parse_size(ini_get('memory_limit'));
$allocationSafe = $bytesNeeded < ($limit - memory_get_usage(true));

Try / catch

use Intervention\Image\Exceptions\DriverException;

try {
    $image->resize($w, $h);
} catch (DriverException $e) {
    // allocation failed: free memory, shrink target, or offload to a queue/CLI worker
    unset($batchImages);
    gc_collect_cycles();
    $image->scale(width: 2000);
}

Prevention

When it happens

Trigger: Processing very large images (e.g. 10000x10000+) under a low memory_limit; batch pipelines leaking memory until allocation fails; requesting huge target sizes in resize/cover.

Common situations: Default memory_limit=128M shared hosting processing modern camera/photo files; workers that accumulate image objects; Docker PHP defaults (128M) meeting 50MP photos.

Related errors


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