{"record":{"id":"50a26a051e2310c0","repo":"Intervention/image","slug":"argument-native-must-be-of-type","errorCode":null,"errorMessage":"Argument $native must be of type ","messagePattern":"Argument \\$native must be of type ","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Core.php","lineNumber":315,"sourceCode":"     *\n     * @see CoreInterface::native()\n     */\n    public function native(): mixed\n    {\n        return $this->imagick;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see CoreInterface::setNative()\n     *\n     * @throws InvalidArgumentException\n     */\n    public function setNative(mixed $native): CoreInterface\n    {\n        if (!$native instanceof Imagick) {\n            throw new InvalidArgumentException('Argument $native must be of type ' . Imagick::class);\n        }\n\n        $this->imagick->clear();\n        $this->imagick = $native;\n\n        return $this;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see CoreInterface::frame()\n     *\n     * @throws InvalidArgumentException\n     * @throws DriverException\n     */\n    public function frame(int $position): FrameInterface\n    {","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Core.php#L297-L333","documentation":"Thrown by Core::setNative() when the value passed is not an Imagick instance. The Imagick core can only hold an Imagick object; anything else (GdImage, array, null, ImagickDraw) is rejected immediately with this InvalidArgumentException. Note the message ends with the expected class name, Imagick.","triggerScenarios":"Calling $image->core()->setNative($value) with a native object from the other driver (a GdImage from the GD pipeline), with a plain value, or with null after a failed native extraction. Typical in custom modifiers that replace the core's native object (mirroring Imagick CropModifier/RemoveAnimationModifier which call setNative with a freshly built Imagick).","commonSituations":"Porting a custom modifier from the GD driver to Imagick (or vice versa) without switching the native type; code that does setNative($image->getGdImage()-like calls); swapping cores between images managed by different ImageManager driver instances.","solutions":["Pass only an instance of Imagick; for the GD driver use the GD core's setNative with GdImage","Build the replacement with native Imagick calls (new Imagick, cloneImage, mergeImageLayers) before calling setNative","Add an instanceof check before the call to fail with your own clearer message"],"exampleFix":"// before: mixing drivers\n$imagickCore->setNative($gdImage); // GdImage from GD driver\n\n// after: keep driver-native types\nif ($replacement instanceof \\Imagick) {\n    $imagickCore->setNative($replacement);\n}","handlingStrategy":"type-guard","validationCode":"if (!$replacement instanceof \\Imagick) {\n    throw new InvalidArgumentException('Expected Imagick instance, got ' . get_debug_type($replacement));\n}\n$image->core()->setNative($replacement);","typeGuard":"function isImagickNative(mixed $value): bool\n{\n    return $value instanceof \\Imagick;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $core->setNative($native);\n} catch (InvalidArgumentException $e) {\n    // programmer error: fix the caller to pass an Imagick instance\n    throw $e;\n}","preventionTips":["In custom modifiers, branch on the driver before touching native objects","Type-hint helper parameters as \\Imagick so failures happen at the call site","Never move native objects between GD and Imagick images"],"tags":["imagick","type-error","driver","native-object"],"backgroundTag":"wrong-driver-native-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}