{"record":{"id":"91c03e6abbb34552","repo":"Intervention/image","slug":"invalid-image-size","errorCode":null,"errorMessage":"Invalid image size","messagePattern":"Invalid image size","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Cloner.php","lineNumber":52,"sourceCode":"     * Create an \"empty\" clone of the given GdImage\n     *\n     * This only retains the basic data without transferring the actual image.\n     * It is optionally possible to change the size of the result and set a\n     * background color.\n     *\n     * @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));","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Cloner.php#L34-L70","documentation":"Cloner::cloneEmpty() builds a fresh truecolor canvas of the requested Size (or the source GdImage's own dimensions when no Size is passed). Before calling imagecreatetruecolor() it rejects any target with width < 1 or height < 1, because GD cannot create a zero/negative canvas. The size can come either from the source image itself (imagesx/imagesy of a broken native) or from the caller-supplied $size argument.","triggerScenarios":"Passing a Size with a 0 or negative side directly: Cloner::cloneEmpty($gd, new Size(0, 100)); higher-level operations (resize/contain/cover paths, text-block sizing) that compute a target size rounding down to 0 for extreme aspect ratios; cloning a corrupted native whose imagesx() returns 0.","commonSituations":"User-supplied width/height not validated before resizing (resize(0, 100)); contain() into a container whose aspect ratio collapses one dimension; float dimensions floor()ed to 0.","solutions":["Clamp requested dimensions before calling image APIs: max(1, (int) round($width))","Validate user input upstream: reject non-positive width/height at the request layer","If it triggers without an explicit size, the source GdImage itself is broken — rebuild it via ImageManager::read()"],"exampleFix":"// before\n$image->resize(0, 100);\n\n// after\n$width = max(1, (int) round($request->input('width')));\n$image->resize($width, 100);","handlingStrategy":"validation","validationCode":"$width = max(1, (int) round($width));\n$height = max(1, (int) round($height));\nif ($width < 1 || $height < 1) {\n    throw new InvalidArgumentException('width/height must be >= 1');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp every user-supplied dimension to >= 1 before resize/cover/contain","Round floats before casting to int so 0.4 does not silently become 0","Validate at the request/DTO layer, not deep in image code"],"tags":["gd","cloner","size","dimensions","validation"],"backgroundTag":"invalid-image-dimensions","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}