{"record":{"id":"fce461d830d697e2","repo":"Intervention/image","slug":"failed-to-read-media-mime-type-from-binary-data","errorCode":null,"errorMessage":"Failed to read media (MIME) type from binary data","messagePattern":"Failed to read media \\(MIME\\) type from binary data","errorType":"exception","errorClass":"ImageDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/AbstractDecoder.php","lineNumber":84,"sourceCode":"     * @throws NotSupportedException\n     */\n    protected function mediaTypeByBinary(string $data): MediaType\n    {\n        if (function_exists('finfo_buffer') && function_exists('finfo_open')) {\n            $mediaType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);\n            if (is_string($mediaType)) {\n                try {\n                    return MediaType::from($mediaType);\n                } catch (ValueError | TypeError) {\n                    throw new NotSupportedException('Unsupported media type (MIME) ' . $mediaType . '.');\n                }\n            }\n        }\n\n        $info = @getimagesizefromstring($data);\n\n        if (!is_array($info)) {\n            throw new ImageDecoderException('Failed to read media (MIME) type from binary data');\n        }\n\n        try {\n            return MediaType::from($info['mime']);\n        } catch (ValueError | TypeError) {\n            throw new NotSupportedException('Unsupported media type (MIME) ' . $info['mime'] . '.');\n        }\n    }\n}\n","sourceCodeStart":66,"sourceCodeEnd":94,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/AbstractDecoder.php#L66-L94","documentation":"Thrown by mediaTypeByBinary when both detection paths fail on binary data: finfo is unavailable or returned nothing usable, and @getimagesizefromstring() did not return an array (i.e. PHP does not recognize the bytes as any known image format). Unlike the 'Unsupported media type' errors, here detection itself failed, which almost always means the data is not an image at all or is truncated/corrupt.","triggerScenarios":"Calling ImageManager::read($string) with random bytes, an HTML/JSON error page captured as a body, a truncated upload (partial multipart read, clipped base64), or an encrypted/encoded payload that was never base64-decoded first. Also hit directly via decodeBinary, which calls mediaTypeByBinary after imagecreatefromstring succeeds.","commonSituations":"cURL/Guzzle response bodies passed to read() without checking Content-Type or success; failed downloads saved to disk then reopened; upstream API returning XML error envelope instead of image bytes; PHP builds compiled without the fileinfo extension where only getimagesizefromstring can judge.","solutions":["Check what the bytes actually are before decoding: $info = @getimagesizefromstring($data); it returns false for non-images","If the data was supposed to be base64, decode it first or pass it through the proper input channel so the Base64ImageDecoder handles it","Re-fetch or repair the source: verify transfer length/checksum, retry the download, or re-export the original file","Assert the upstream HTTP response (status + Content-Type) before treating a body as image data"],"exampleFix":"// before\n$image = $manager->read($response->getBody()->getContents());\n// ImageDecoderException: Failed to read media (MIME) type from binary data\n\n// after\n$body = $response->getBody()->getContents();\nif (@getimagesizefromstring($body) === false) {\n    throw new RuntimeException('Upstream did not return an image: ' . substr($body, 0, 120));\n}\n$image = $manager->read($body);","handlingStrategy":"try-catch","validationCode":"if (@getimagesizefromstring($data) === false && (new \\finfo(FILEINFO_MIME_TYPE))->buffer($data) === 'application/octet-stream') {\n    throw new RuntimeException('Payload is not recognizable image data');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->read($data);\n} catch (\\Intervention\\Image\\Exceptions\\ImageDecoderException $e) {\n    // bytes are not an image; log a prefix of the payload for diagnosis\n    logger()->debug('Non-image payload: ' . substr(bin2hex($data), 0, 32));\n}","preventionTips":["Verify upstream HTTP status and Content-Type before treating a body as image bytes","Check Content-Length or byte count before decoding","Ensure ext-fileinfo is installed so the finfo detection path runs"],"tags":["binary-data","corrupt-file","gd-driver","decoder","getimagesize","detection-failed"],"backgroundTag":"invalid-image-data","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}