Intervention/image · error · Intervention\Image\Exceptions\InvalidArgumentException

Argument $native must be of type

Error message

Argument $native must be of type 

What it means

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.

Source

Thrown at src/Drivers/Imagick/Core.php:315

     *
     * @see CoreInterface::native()
     */
    public function native(): mixed
    {
        return $this->imagick;
    }

    /**
     * {@inheritdoc}
     *
     * @see CoreInterface::setNative()
     *
     * @throws InvalidArgumentException
     */
    public function setNative(mixed $native): CoreInterface
    {
        if (!$native instanceof Imagick) {
            throw new InvalidArgumentException('Argument $native must be of type ' . Imagick::class);
        }

        $this->imagick->clear();
        $this->imagick = $native;

        return $this;
    }

    /**
     * {@inheritdoc}
     *
     * @see CoreInterface::frame()
     *
     * @throws InvalidArgumentException
     * @throws DriverException
     */
    public function frame(int $position): FrameInterface
    {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Pass only an instance of Imagick; for the GD driver use the GD core's setNative with GdImage
  2. Build the replacement with native Imagick calls (new Imagick, cloneImage, mergeImageLayers) before calling setNative
  3. Add an instanceof check before the call to fail with your own clearer message

Example fix

// before: mixing drivers
$imagickCore->setNative($gdImage); // GdImage from GD driver

// after: keep driver-native types
if ($replacement instanceof \Imagick) {
    $imagickCore->setNative($replacement);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!$replacement instanceof \Imagick) {
    throw new InvalidArgumentException('Expected Imagick instance, got ' . get_debug_type($replacement));
}
$image->core()->setNative($replacement);

Type guard

function isImagickNative(mixed $value): bool
{
    return $value instanceof \Imagick;
}

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException;

try {
    $core->setNative($native);
} catch (InvalidArgumentException $e) {
    // programmer error: fix the caller to pass an Imagick instance
    throw $e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/50a26a051e2310c0. Report an issue: GitHub.