{"record":{"id":"307269c5e90677b7","repo":"Intervention/image","slug":"image-source-must-be-of-type-gdimage","errorCode":null,"errorMessage":"Image source must be of type GdImage","messagePattern":"Image source must be of type GdImage","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/NativeObjectDecoder.php","lineNumber":43,"sourceCode":"     */\n    public function supports(mixed $input): bool\n    {\n        return $input instanceof GdImage;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DecoderInterface::decode()\n     *\n     * @throws InvalidArgumentException\n     * @throws DriverException\n     * @throws StateException\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        if (!$input instanceof GdImage) {\n            throw new InvalidArgumentException('Image source must be of type ' . GdImage::class);\n        }\n\n        if (!imageistruecolor($input)) {\n            $result = imagepalettetotruecolor($input);\n            if ($result === false) {\n                throw new DriverException('Failed to convert image to true color');\n            }\n        }\n\n        imagesavealpha($input, true);\n\n        // build image instance\n        return new Image(\n            $this->driver(),\n            new Core([\n                new Frame($input),\n            ]),\n        );","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/NativeObjectDecoder.php#L25-L61","documentation":"Contract violation of NativeObjectDecoder::decode: the input is not a GdImage instance. This GD-driver decoder accepts only native GD resources/objects, so anything else (string, resource, Imagick object, null) triggers an InvalidArgumentException immediately. In the manager's auto-routing supports() filters for GdImage, so this fires mainly on direct decoder calls or miswired custom chains.","triggerScenarios":"Calling the native decoder (or a pipeline built on it) with an Imagick object, a file-path string, a GD resource from older PHP (PHP < 8 resources vs GdImage), or null; passing the result of a function that returns GdImage|false without checking the false branch.","commonSituations":"Migrating pre-PHP-8 code where imagecreate* returned resources; mixed-driver codebases where Imagick and GD objects flow through the same variable; false from a failed imagecreatetruecolor(color-mode) leaking in; unit tests instantiating decoders directly.","solutions":["Guard the value: $input instanceof GdImage before invoking decode; check for false after any imagecreate* call","Let ImageManager::read() do the dispatch — pass binary/paths and let it route to the right decoder","Convert foreign objects first: Imagick images can be re-encoded to PNG bytes and re-read; older resources are automatically GdImage on PHP >= 8","Type the upstream API as GdImage instead of mixed so violations surface at the caller"],"exampleFix":"// before\n$gd = @imagecreatetruecolor(100, 100); // returns GdImage|false\n$decoder = new NativeObjectDecoder();\n$image = $decoder->decode($gd); // $gd may be false\n// InvalidArgumentException: Image source must be of type GdImage\n\n// after\n$gd = imagecreatetruecolor(100, 100);\nif ($gd === false) {\n    throw new RuntimeException('GD failed to allocate the canvas');\n}\n$image = $decoder->decode($gd);","handlingStrategy":"type-guard","validationCode":"if (!$input instanceof \\GdImage) {\n    throw new InvalidArgumentException('Expected GdImage, got ' . get_debug_type($input));\n}","typeGuard":"/**\n * @param mixed $input\n */\nfunction isGdImage(mixed $input): bool\n{\n    return $input instanceof \\GdImage;\n}","tryCatchPattern":null,"preventionTips":["Check the GdImage|false return of every imagecreate* call before use","Prefer ImageManager::read() so decoding is dispatched by type","Never mix Imagick objects and GdImage values in one untyped pipeline"],"tags":["type-error","invalid-argument","gd-image","decoder","php"],"backgroundTag":"invalid-argument-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}