rosell-dk / image-mime-type-sniffer
嗅探MIME类型(仅限图片)
1.1.1
2022-04-20 14:31 UTC
Requires
- php: >=5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.11
- mikey179/vfsstream: ^1.6
- phpstan/phpstan: ^1.5
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: 3.*
README
通过嗅探文件内容,查找签名来获取常见图像文件的MIME类型。
由于这个库将它的目标限制为仅嗅探图像,因此它轻量且简单。它也相当快速。大多数其他嗅探器都会遍历所有常见签名,然而这个库使用有限状态机方法和迭代相结合来实现速度、紧凑性、简单性和可读性之间的良好平衡。
该库识别最广泛使用的图像格式,例如GIF、JPEG、WEBP、AVIF、JPEG-2000和HEIC。
用法
use \ImageMimeTypeSniffer\ImageMimeTypeSniffer; $mimeType = ImageMimeTypeSniffer::detect($fileName); if (is_null($mimeType)) { // mimetype was not detected, which means the file is probably not an image (unless it is a rare type) } else { // It is an image, and we know the mimeType }
PS:如果文件不可读,将抛出异常。
平台
支持以下操作系统(至少):OS:Ubuntu(22.04、20.04)、Windows(2022、2019)、Mac OS(11、10.15)PHP:5.6 - 8.2(2023年10月还测试了8.3和8.4的开发版本)
每个新版本将测试GitHub托管运行器支持的操作系统和PHP版本的所有组合。但是,我们不包括低于PHP 5.6的版本。
测试包括运行单元测试。这个库中的代码几乎全部由测试覆盖(约97%覆盖率)。
版本1.1.1已经在以下操作系统上进行了测试:Ubuntu(22.04、20.04、18.04)、Windows(2022、2019)、Mac OS(13、12、11、10.15)。代码覆盖率:约97%。由于某种原因,PHP 5.6在Windows上的测试失败
识别的图像类型列表
- application/psd
- image/avif
- image/bmp
- image/gif
- image/heic
- image/jp2
- image/jp20
- image/jpeg
- image/jpm
- image/jpx
- image/png
- image/svg+xml
- image/tiff
- image/webp
- image/x-icon
- video/mj2
待办事项:image/heif
替代方案
我创建了一个库,该库使用这个库以及其他方法(如finfo、exif_imagetype等)来确定图像类型。您可能想使用该库,以覆盖所有可能性。它可在以下位置找到: image-mime-type-guesser。
还有其他PHP MIME类型嗅探器
- https://github.com/Intervention/mimesniffer
- https://github.com/Tinram/File-Identifier
- https://github.com/shanept/MimeSniffer
- https://github.com/zjsxwc/mime-type-sniffer
- https://github.com/thephpleague/mime-type-detection/tree/main/src
您喜欢我的工作吗?
也许您想支持我的工作,这样我就可以继续这样做:)