esi/mimey

PHP包,用于将文件扩展名转换为MIME类型,反之亦然。

v2.1.0 2024-04-29 05:58 UTC

This package is auto-updated.

Last update: 2024-09-22 11:34:51 UTC


README

PHP包,用于将文件扩展名转换为MIME类型,反之亦然。

Build Status Code Coverage Scrutinizer Code Quality PHPStan Tests Psalm Static analysis Type Coverage Psalm Level SymfonyInsight Latest Stable Version Downloads per Month License

此包使用httpdmime.types来生成文件扩展名到MIME类型的映射,反之亦然。点击此处查看它们的svn更改日志:changelog

通过bin/generate.php解析mime.types文件,并将其转换为存储在dist/mime.types.min.json中的优化JSON对象,然后由辅助类MimeTypes封装。

还提供了一个包含所有MIME类型和获取扩展名的方法的生成PHP枚举。也可以用于从扩展名获取枚举值。

用法

$mimes = new MimeTypes;

// Convert extension to MIME type:
$mimes->getMimeType('json'); // application/json

// Convert MIME type to extension:
$mimes->getExtension('application/json'); // json

使用枚举

$json = MimeType::ApplicationJson;
echo $json->getExtension(); // json
echo $json->value; // application/json

$html = MimeType::fromExtension('html');
echo $html->value; // text/html

MimeType::fromExtension('asdf'); // throws an InvalidArgumentException if the extension cannot be found

获取所有

很少,但有些扩展名有多个MIME类型

// Get all MIME types for an extension:
$mimes->getAllMimeTypes('wmz'); // array('application/x-ms-wmz', 'application/x-msmetafile')

然而,有许多MIME类型有多个扩展名

// Get all extensions for a MIME type:
$mimes->getAllExtensions('image/jpeg'); // array('jpeg', 'jpg', 'jpe')

自定义转换

您可以通过更改传递给MimeTypes的映射来添加自定义转换。

有一个Mapping.Builder可以帮助您完成此操作

use Esi\Mimey\Mapping\Builder;

// Create a builder using the built-in conversions as the basis.
$builder = Builder::create();

// Add a conversion. This conversion will take precedence over existing ones.
$builder->add('custom/mime-type', 'myextension');

$mimes = new MimeTypes($builder->getMapping());
$mimes->getMimeType('myextension'); // custom/mime-type
$mimes->getExtension('custom/mime-type'); // myextension

您可以将所需的任何转换添加到构建器中

$builder->add('custom/mime-type', 'myextension');
$builder->add('foo/bar', 'foobar');
$builder->add('foo/bar', 'fbar');
$builder->add('baz/qux', 'qux');
$builder->add('cat/qux', 'qux');
...

优化自定义转换加载

您可以通过在构建步骤中将所有转换保存到编译的PHP文件中来优化自定义转换的加载。

// Add a bunch of custom conversions.
$builder->add(...);
$builder->add(...);
$builder->add(...);
...
// Save the conversions to a cached file.
$builder->save($cacheFilePath);

然后可以加载该文件,以避免重复调用$builder->add(...)的开销

// Load the conversions from a cached file.
$builder = Builder::load($cacheFilePath);
$mimes = new MimeTypes($builder->getMapping());

安装

与PHP >= 8.2兼容。

composer require esi/mimey

鸣谢

此分支使用的许可证与原始存储库@ralouphie(MIT)相同。此存储库是elephox-dev/mimey的分支,而elephox-dev/mimey本身是ralouphie/mimey的分支。感谢他们以及所有贡献者!

提交错误和功能请求

错误和功能请求在GitHub上跟踪

问题是最快报告错误的方式。如果您发现错误或文档错误,请首先检查以下内容

  • 没有关于该错误的已打开问题
  • 问题尚未得到解决(例如,在已关闭的问题中)

贡献

缺少MIME类型?

打开一个issue或甚至自己添加!过程非常简单

  1. 分支此存储库
  2. 将您的MIME类型添加到data/mime.types.custom文件中(确保格式正确!)
  3. 推送您的更改
  4. 提交一个pull request

有关贡献的更多信息,请参阅CONTRIBUTING

作者

Eric Sizemore - admin@secondversion.com - https://www.secondversion.com

许可证

.mimey 在MIT许可下授权 - 详细信息请参阅 LICENSE.md 文件。