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

Failed to retrieve image media type

Error message

Failed to retrieve image media type

What it means

The input was a valid Imagick instance, but Imagick::getImageMimeType() threw. The typical cause is an Imagick object that holds no image at all - created with new Imagick() and never loaded, or one whose image list was cleared - so there is no media type to read. The native exception is chained as previous for diagnosis.

Source

Thrown at src/Drivers/Imagick/Decoders/NativeObjectDecoder.php:52

     * {@inheritdoc}
     *
     * @see DecoderInterface::decode()
     *
     * @throws InvalidArgumentException
     * @throws StateException
     * @throws DriverException
     * @throws ImageDecoderException
     */
    public function decode(mixed $input): ImageInterface
    {
        if (!$input instanceof Imagick) {
            throw new InvalidArgumentException('Image source must be an instance of Imagick');
        }

        try {
            $originalMimeType = $input->getImageMimeType();
        } catch (ImagickException $e) {
            throw new ImageDecoderException('Failed to retrieve image media type', previous: $e);
        }

        // For some JPEG formats, the "coalesceImages()" call leads to an image
        // completely filled with background color. The logic behind this is
        // incomprehensible for me; could be an imagick bug.
        try {
            if ($input->getImageFormat() !== 'JPEG') {
                $input = $input->coalesceImages();
            }
        } catch (ImagickException $e) {
            throw new DriverException('Failed to coalesce image', previous: $e);
        }

        // turn images with colorspace 'GRAY' into 'SRGB' to avoid working on
        // grayscale colorspace images as this results images loosing color
        // information when placed into this image.
        try {
            if ($input->getImageColorspace() === Imagick::COLORSPACE_GRAY) {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Ensure the Imagick actually contains at least one image before passing: $imagick->getNumberImages() > 0 (Imagick is Countable)
  2. Read $e->getPrevious() for the native reason
  3. If frames were intentionally removed, keep one frame or pass the encoded bytes instead of the emptied object

Example fix

// before
$imagick = new Imagick(); // never read an image
$image = $manager->read($imagick);

// after
$imagick = new Imagick();
$imagick->readImageBlob($bytes);
$image = $manager->read($imagick); // or simply $manager->read($bytes)
Defensive patterns

Strategy: validation

Validate before calling

if ($imagick->getNumberImages() < 1) {
    throw new RuntimeException('Imagick object holds no image');
}
$image = $manager->read($imagick);

Type guard

function isLoadedImagick(mixed $value): bool
{
    return $value instanceof \Imagick && $value->getNumberImages() > 0;
}

Try / catch

try {
    $image = $manager->read($imagick);
} catch (\Intervention\Image\Exceptions\ImageDecoderException $e) {
    $reason = $e->getPrevious()?->getMessage() ?? 'unknown';
    throw new RuntimeException('Media type lookup failed: ' . $reason, 0, $e);
}

Prevention

When it happens

Trigger: ImageManager::read(new Imagick()) with an empty object; an Imagick whose frames were removed earlier (clear(), removeImage() in an animation-processing loop) and then passed to the decoder.

Common situations: Animation code that strips frames before passing the object on; factory helpers that construct Imagick lazily and accidentally hand over an unloaded instance.

Related errors


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