{"record":{"id":"c83d0ffc0d17400a","repo":"Intervention/image","slug":"failed-to-create-new-image","errorCode":null,"errorMessage":"Failed to create new image","messagePattern":"Failed to create new image","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Driver.php","lineNumber":66,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see DriverInterface::createImage()\n     *\n     * @throws InvalidArgumentException\n     * @throws DriverException\n     */\n    public function createImage(int $width, int $height): ImageInterface\n    {\n        if ($width < 1 || $height < 1) {\n            throw new InvalidArgumentException('Invalid image size. Must be int<1, max>');\n        }\n\n        // build new transparent GDImage\n        $data = imagecreatetruecolor($width, $height);\n        if (!$data instanceof GDImage) {\n            throw new DriverException('Failed to create new image');\n        }\n\n        imagesavealpha($data, true);\n        $background = imagecolorallocatealpha($data, 255, 255, 255, 127);\n\n        imagealphablending($data, false);\n        imagefill($data, 0, 0, $background);\n        imagecolortransparent($data, $background);\n        imageresolution($data, 72, 72);\n\n        return new Image($this, new Core([new Frame($data)]));\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DriverInterface::createCore()\n     */","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Driver.php#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"Default memory_limit=128M on shared hosting, generating print-sized posters, batch workers accumulating Image instances until cumulative usage tips over the limit.","solutions":["Raise memory_limit (ini_set or pool config) to comfortably cover width*height*4 bytes plus overhead","Reduce the target canvas dimensions or process the output in tiles","unset() finished images so GC can free earlier canvases in long-running workers","Estimate required bytes before creating and reject or downscale oversized requests"],"exampleFix":"// before\n$image = $manager->create(15000, 15000); // ~900 MB buffer => DriverException\n\n// after\nini_set('memory_limit', '2G');\n$image = $manager->create(15000, 15000);","handlingStrategy":"validation","validationCode":"$needed = $width * $height * 4;\n$limitBytes = parseBytes(ini_get('memory_limit')); // helper for '256M' etc.\nif ($needed > 0.8 * $limitBytes - memory_get_usage()) {\n    throw new RuntimeException('Canvas too large for current memory_limit');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->create($w, $h);\n} catch (DriverException $e) {\n    // reduce dimensions, or raise memory_limit and retry once\n}","preventionTips":["Size memory_limit to the largest planned canvas (w*h*4 plus overhead)","unset() finished images in batch workers","Cap accepted dimensions at the API boundary"],"tags":["memory-limit","gd","large-image","canvas"],"backgroundTag":"memory-limit-exhausted","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}