rosell-dk/image-mime-type-guesser
Composer 安装命令:
composer require rosell-dk/image-mime-type-guesser
包简介
Guess mime type of images
关键字:
README 文档
README
Detect / guess mime type of an image
Do you need to determine if a file is an image?
And perhaps you also want to know the mime type of the image?
– You come to the right library.
Ok, actually the library cannot offer mime type detection for images which works on all platforms, but it can try a whole stack of methods and optionally fall back to guess from the file extension.
The stack of detect methods are currently (and in that order):
- This signature sniffer Does not require any extensions. Recognizes popular image formats only
finfoRequires fileinfo extension to be enabled. (PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL fileinfo >= 0.1.0)exif_imagetypeRequires that PHP is compiled with exif (PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)mime_content_typeRequires fileinfo. (PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
Note that all these methods except the signature sniffer relies on the mime type mapping on the server (the mime.types file in Apache). If the server doesn't know about a certain mime type, it will not be detected. This does however not mean that the methods relies on the file extension. A png file renamed to "png.jpeg" will be correctly identified as image/png.
Besides the detection methods, the library also comes with a method for mapping file extension to mime type. It is rather limited, though.
Installation
Install with composer:
composer require rosell-dk/image-mime-type-guesser
Usage
To detect the mime type of a file, use ImageMimeTypeGuesser::detect($filePath). It returns the mime-type, if the file is recognized as an image. false is returned if it is not recognized as an image. null is returned if the mime type could not be determined (ie due to none of the methods being available).
Example:
use ImageMimeTypeGuesser\ImageMimeTypeGuesser; $result = ImageMimeTypeGuesser::detect($filePath); if (is_null($result)) { // the mime type could not be determined } elseif ($result === false) { // it is NOT an image (not a mime type that the server knows about anyway) // This happens when: // a) The mime type is identified as something that is not an image (ie text) // b) The mime type isn't identified (ie if the image type is not known by the server) } else { // it is an image, and we know its mime type! $mimeType = $result; }
For convenience, you can use detectIsIn method to test if a detection is in a list of mimetypes.
if (ImageMimeTypeGuesser::detectIsIn($filePath, ['image/jpeg','image/png'])) { // The file is a jpeg or a png }
The detect method does not resort to mapping from file extension. In most cases you do not want to do that. In some cases it can be insecure to do that. For example, if you want to prevent a user from uploading executable files, you probably do not want to allow her to upload executable files with innocent looking file extenions, such as "evil-exe.jpg".
In some cases, though, you simply want a best guess, and in that case, falling back to mapping from file extension makes sense. In that case, you can use the guess method instead of the detect method. Or you can use lenientGuess. Lenient guess is even more slacky and will turn to mapping not only when dectect return null, but even when it returns false.
Warning: Beware that guessing from file extension is unsuited when your aim is to protect the server from harmful uploads.
Notice: Only a limited set of image extensions is recognized by the extension to mimetype mapper - namely the following: { apng, avif, bmp, gif, ico, jpg, jpeg, png, tif, tiff, webp, svg }. If you need some other specifically, feel free to add a PR, or ask me to do it by creating an issue.
Example:
$result = ImageMimeTypeGuesser::guess($filePath); if ($result !== false) { // It appears to be an image // BEWARE: This is only a guess, as we resort to mapping from file extension, // when the file cannot be properly detected. // DO NOT USE THIS GUESS FOR PROTECTING YOUR SERVER $mimeType = $result; } else { // It does not appear to be an image }
The guess functions also have convenience methods for testing against a list of mime types. They are called ImageMimeTypeGuesser::guessIsIn and ImageMimeTypeGuesser::lenientGuessIsIn.
Example:
if (ImageMimeTypeGuesser::guessIsIn($filePath, ['image/jpeg','image/png'])) { // The file appears to be a jpeg or a png }
Alternatives
Other sniffers:
- https://github.com/Intervention/mimesniffer
- https://github.com/zjsxwc/mime-type-sniffer
- https://github.com/Tinram/File-Identifier
Do you like what I do?
Perhaps you want to support my work, so I can continue doing it :)
rosell-dk/image-mime-type-guesser 适用场景与选型建议
rosell-dk/image-mime-type-guesser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.9M 次下载、GitHub Stars 达 10, 最近一次更新时间为 2019 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「mime」 「images」 「mime type」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rosell-dk/image-mime-type-guesser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rosell-dk/image-mime-type-guesser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rosell-dk/image-mime-type-guesser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
Parse a generic mail stream, and convert it to a SwiftMailer Message
A Laravel Nova field for distribute your images as array of object.
Pexels API Client for www.pexels.com
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
统计信息
- 总下载量: 8.9M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 23
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-15