{"record":{"id":"9c0d5887e27849a2","repo":"Intervention/image","slug":"failed-to-create-new-image-while-cloning","errorCode":null,"errorMessage":"Failed to create new image while cloning","messagePattern":"Failed to create new image while cloning","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Cloner.php","lineNumber":58,"sourceCode":"     * @throws InvalidArgumentException\n     * @throws DriverException\n     */\n    public static function cloneEmpty(\n        GdImage $gd,\n        ?SizeInterface $size = null,\n        Color $background = new Color(255, 255, 255, 0),\n    ): GdImage {\n        // define size\n        $size = $size ?: new Size(imagesx($gd), imagesy($gd));\n\n        if ($size->width() < 1 || $size->height() < 1) {\n            throw new InvalidArgumentException('Invalid image size');\n        }\n\n        // create new gd image with same size or new given size\n        $clone = imagecreatetruecolor($size->width(), $size->height());\n        if ($clone === false) {\n            throw new DriverException('Failed to create new image while cloning');\n        }\n\n        // copy resolution to clone\n        $resolution = imageresolution($gd);\n        if (is_array($resolution) && array_key_exists(0, $resolution) && array_key_exists(1, $resolution)) {\n            imageresolution($clone, $resolution[0], $resolution[1]);\n        }\n\n        // fill with background\n        $processor = new ColorProcessor();\n\n        imagefill($clone, 0, 0, $processor->export($background));\n        imagealphablending($clone, true);\n        imagesavealpha($clone, true);\n\n        // set background image as transparent if alpha channel value if color is below .5\n        // comes into effect when the end format only supports binary transparency (like GIF)\n        if ($background->alpha()->value() < .5) {","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Cloner.php#L40-L76","documentation":"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.","triggerScenarios":"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.","commonSituations":"Default memory_limit=128M shared hosting processing modern camera/photo files; workers that accumulate image objects; Docker PHP defaults (128M) meeting 50MP photos.","solutions":["Raise memory_limit (ini_set('memory_limit', '512M') or PHP_INI_ENV) sized for your largest image: width*height*4*~1.6 bytes","Downscale before heavy operations: read with a limit, or use scale()/resize in stages rather than one huge canvas","Free prior images in batch loops (unset($image) and force GC) so allocations don't stack","Stream/queue very large files to an external tool (ImageMagick, vips) instead of GD"],"exampleFix":"// before\n$image = $manager->read('huge-panorama.png');\n$image->resize(20000, 20000); // Fatal allocation\n\n// after\nini_set('memory_limit', '1G');\n$image = $manager->read('huge-panorama.png');\n$image->scale(width: 4000); // bounded target","handlingStrategy":"try-catch","validationCode":"$bytesNeeded = $width * $height * 4 * 1.6; // truecolor + overhead heuristic\n$limit = parse_size(ini_get('memory_limit'));\n$allocationSafe = $bytesNeeded < ($limit - memory_get_usage(true));","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\DriverException;\n\ntry {\n    $image->resize($w, $h);\n} catch (DriverException $e) {\n    // allocation failed: free memory, shrink target, or offload to a queue/CLI worker\n    unset($batchImages);\n    gc_collect_cycles();\n    $image->scale(width: 2000);\n}","preventionTips":["Size memory_limit to your largest expected canvas (width*height*4 plus margin)","Process very large images in queued CLI workers, not web requests","unset() finished image objects in batch loops and call gc_collect_cycles()"],"tags":["gd","memory","allocation","imagecreatetruecolor","large-images"],"backgroundTag":"memory-limit-exhausted","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}