{"record":{"id":"bae43dc022a8036d","repo":"Intervention/image","slug":"failed-to-read-media-mime-type-from-data-in-file","errorCode":null,"errorMessage":"Failed to read media (MIME) type from data in file path","messagePattern":"Failed to read media \\(MIME\\) type from data in file path","errorType":"exception","errorClass":"ImageDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/AbstractDecoder.php","lineNumber":52,"sourceCode":"    protected function mediaTypeByFilePath(string $filepath): MediaType\n    {\n        $filepath = self::readableFilePathOrFail($filepath);\n\n        if (function_exists('finfo_file') && function_exists('finfo_open')) {\n            $mediaType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);\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 = @getimagesize($filepath);\n\n        if (!is_array($info)) {\n            throw new ImageDecoderException('Failed to read media (MIME) type from data in file path');\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    /**\n     * Return media (mime) type of the given image data\n     *\n     * @throws ImageDecoderException\n     * @throws NotSupportedException\n     */\n    protected function mediaTypeByBinary(string $data): MediaType\n    {\n        if (function_exists('finfo_buffer') && function_exists('finfo_open')) {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/AbstractDecoder.php#L34-L70","documentation":"This is the fallback path of mediaTypeByFilePath(): either the fileinfo extension is unavailable (finfo_file/finfo_open missing) or it returned a non-string, so the decoder falls back to @getimagesize() — and that also failed to produce its info array. Combined, the file's type could not be determined at all: typically the data is not an image, is truncated/corrupt, or is unreadable despite passing the earlier readable-file check.","triggerScenarios":"Reading a non-image file (HTML/JSON error body cached as .jpg); zero-byte or truncated upload; PHP built without ext-fileinfo (minimal Docker images, --disable-fileinfo builds); getimagesize() failing on formats it does not recognize.","commonSituations":"Download pipelines saving API error pages with image extensions; slim/alpine PHP images lacking fileinfo so getimagesize() alone must judge corrupt files; CDNs serving partial content.","solutions":["Install/enable ext-fileinfo: docker-php-ext-install fileinfo (or apt install php8.x-fileinfo) so the robust finfo path is used","Verify the payload is really an image before read(): getimagesize($path) !== false, or check the magic bytes yourself","If the file is a partial download, re-fetch it completely (compare Content-Length) before processing","Log the first bytes (bin2hex(substr((string) file_get_contents($path), 0, 16))) to identify what actually arrived"],"exampleFix":"// before\n$image = $manager->read($downloadedPath); // ImageDecoderException\n\n// after\nif (@getimagesize($downloadedPath) === false) {\n    throw new RuntimeException('Downloaded file is not a valid image');\n}\n$image = $manager->read($downloadedPath);","handlingStrategy":"validation","validationCode":"$isImage = function (string $path): bool {\n    if (!is_file($path) || filesize($path) === 0) {\n        return false;\n    }\n    if (function_exists('finfo_file')) {\n        return str_starts_with(\n            (string) finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path),\n            'image/'\n        );\n    }\n\n    return @getimagesize($path) !== false;\n};","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\ImageDecoderException;\n\ntry {\n    $image = $manager->read($path);\n} catch (ImageDecoderException $e) {\n    // not decodable as an image: log payload head and reject\n    logger()->warning('Non-image payload: ' . bin2hex((string) substr((string) file_get_contents($path), 0, 16)));\n    throw $e;\n}","preventionTips":["Install ext-fileinfo in all runtimes so finfo detection is available","Verify downloads completed (Content-Length match) before reading them as images","Check getimagesize() on uploaded files as a cheap is-really-an-image gate"],"tags":["gd","decoder","mime-type","corrupt-file","fileinfo","getimagesize"],"backgroundTag":"unreadable-image-file","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}