{"record":{"id":"834afdf5cbfea236","repo":"Intervention/image","slug":"invalid-image-size-must-be-int-1-max","errorCode":null,"errorMessage":"Invalid image size. Must be int<1, max>","messagePattern":"Invalid image size\\. Must be int<1, max>","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Driver.php","lineNumber":60,"sourceCode":"        if (!extension_loaded('gd') || !function_exists('gd_info')) {\n            throw new MissingDependencyException(\n                'GD PHP extension must be installed to use this driver',\n            );\n        }\n    }\n\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    }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Driver.php#L42-L78","documentation":"Driver::createImage() validates that both dimensions are at least 1 pixel; zero or negative width or height throws InvalidArgumentException before GD is even touched. It is a pure input-contract violation, not an environment problem.","triggerScenarios":"$manager->create(0, 300) or createImage(-10, 100); dimensions computed from user input that default to 0; (int) cast of null or '' producing 0; aspect-ratio or crop arithmetic rounding down to 0.","commonSituations":"Thumbnail sizes taken from query parameters (?w=0), null coalescing that falls through to 0, crop-region math on very thin images producing 0 height, form fields left empty.","solutions":["Validate and clamp dimensions to >= 1 before calling create()/createImage()","Default to a sane fallback size when the input is missing or unparseable","Reject non-numeric or zero-sized requests earlier at the API boundary","Log the offending values so the faulty computation is traceable"],"exampleFix":"// before\n$width = (int) ($_GET['w'] ?? 0);\n$image = $manager->create($width, $width); // 0 => throws\n\n// after\n$width = max(1, min(4096, (int) ($_GET['w'] ?: 200)));\n$image = $manager->create($width, $width);","handlingStrategy":"validation","validationCode":"$width = max(1, (int) $width);\n$height = max(1, (int) $height);\n$manager->create($width, $height);","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->create($w, $h);\n} catch (InvalidArgumentException $e) {\n    $image = $manager->create(1, 1); // degenerate fallback, or re-prompt user\n}","preventionTips":["Clamp user-supplied dimensions to a sane range at the boundary","Treat empty/zero dimension input as 'use default', not 0","Unit-test size math (crops, aspect ratios) for edge cases"],"tags":["validation","dimensions","create","gd"],"backgroundTag":"invalid-image-dimensions","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}