{"record":{"id":"91262711e9175bac","repo":"Intervention/image","slug":"failed-to-retrieve-image-media-type","errorCode":null,"errorMessage":"Failed to retrieve image media type","messagePattern":"Failed to retrieve image media type","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ImageDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/NativeObjectDecoder.php","lineNumber":52,"sourceCode":"     * {@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        }\n\n        // turn images with colorspace 'GRAY' into 'SRGB' to avoid working on\n        // grayscale colorspace images as this results images loosing color\n        // information when placed into this image.\n        try {\n            if ($input->getImageColorspace() === Imagick::COLORSPACE_GRAY) {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"Animation code that strips frames before passing the object on; factory helpers that construct Imagick lazily and accidentally hand over an unloaded instance.","solutions":["Ensure the Imagick actually contains at least one image before passing: $imagick->getNumberImages() > 0 (Imagick is Countable)","Read $e->getPrevious() for the native reason","If frames were intentionally removed, keep one frame or pass the encoded bytes instead of the emptied object"],"exampleFix":"// before\n$imagick = new Imagick(); // never read an image\n$image = $manager->read($imagick);\n\n// after\n$imagick = new Imagick();\n$imagick->readImageBlob($bytes);\n$image = $manager->read($imagick); // or simply $manager->read($bytes)","handlingStrategy":"validation","validationCode":"if ($imagick->getNumberImages() < 1) {\n    throw new RuntimeException('Imagick object holds no image');\n}\n$image = $manager->read($imagick);","typeGuard":"function isLoadedImagick(mixed $value): bool\n{\n    return $value instanceof \\Imagick && $value->getNumberImages() > 0;\n}","tryCatchPattern":"try {\n    $image = $manager->read($imagick);\n} catch (\\Intervention\\Image\\Exceptions\\ImageDecoderException $e) {\n    $reason = $e->getPrevious()?->getMessage() ?? 'unknown';\n    throw new RuntimeException('Media type lookup failed: ' . $reason, 0, $e);\n}","preventionTips":["After clear()/removeImage() loops, verify getNumberImages() > 0 before reusing the object","Pass encoded bytes instead of emptied Imagick objects between pipeline stages"],"tags":["imagick","metadata","mime-type","empty-image"],"backgroundTag":"image-metadata-unavailable","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}