{"record":{"id":"660d6865b9b080cc","repo":"Intervention/image","slug":"image-source-must-be-an-instance-of-imagick","errorCode":null,"errorMessage":"Image source must be an instance of Imagick","messagePattern":"Image source must be an instance of Imagick","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/NativeObjectDecoder.php","lineNumber":46,"sourceCode":"    public function supports(mixed $input): bool\n    {\n        return $input instanceof Imagick;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DecoderInterface::decode()\n     *\n     * @throws InvalidArgumentException\n     * @throws StateException\n     * @throws DriverException\n     * @throws ImageDecoderException\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        if (!$input instanceof Imagick) {\n            throw new InvalidArgumentException('Image source must be an instance of Imagick');\n        }\n\n        try {\n            $originalMimeType = $input->getImageMimeType();\n        } catch (ImagickException $e) {\n            throw new ImageDecoderException('Failed to retrieve image media type', previous: $e);\n        }\n\n        // For some JPEG formats, the \"coalesceImages()\" call leads to an image\n        // completely filled with background color. The logic behind this is\n        // incomprehensible for me; could be an imagick bug.\n        try {\n            if ($input->getImageFormat() !== 'JPEG') {\n                $input = $input->coalesceImages();\n            }\n        } catch (ImagickException $e) {\n            throw new DriverException('Failed to coalesce image', previous: $e);\n        }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php#L28-L64","documentation":"NativeObjectDecoder::decode() was called with a value that is not an Imagick object. In normal usage ImageManager::read() routes input through the decoder chain via supports() (which checks instanceof Imagick), so hitting this means the specialized decoder was invoked directly or a custom pipeline bypassed the manager's routing.","triggerScenarios":"Calling (new NativeObjectDecoder($driver))->decode($value) manually with a GdImage, ImagickPixel, string or null; custom decoder chains assembled by hand; code ported from the GD driver where the native object type is GdImage.","commonSituations":"Porting driver-specific code from GD to Imagick; wrapper classes that hold a 'native' object of mixed driver origin and pass it to the wrong decoder.","solutions":["Use ImageManager::read($input) instead of calling the decoder directly - it routes Imagick objects to this decoder automatically","If calling decode() directly, guard with instanceof Imagick or the decoder's supports() method first","For GD-native objects (GdImage), construct the manager with the GD driver and read through it"],"exampleFix":"// before\n$decoder = new NativeObjectDecoder($driver);\n$image = $decoder->decode($gdImage); // GdImage passed to Imagick decoder\n\n// after: route through the manager, which dispatches by type via supports()\n$gdManager = new ImageManager(['driver' => 'gd']);\n$image = $gdManager->read($gdImage); // or for Imagick: $manager->read($imagick)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isImagickObject(mixed $value): bool\n{\n    return $value instanceof \\Imagick;\n}","tryCatchPattern":null,"preventionTips":["Route native objects through ImageManager::read() so supports() picks the right decoder","Never call a specialized decoder directly without an instanceof check matching its supports()"],"tags":["imagick","type-error","decoder","invalid-argument"],"backgroundTag":"invalid-argument-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}