devanych / mime-types
Converts MIME types to file extensions and vice versa
Installs: 56 381
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.3
This package is auto-updated.
Last update: 2024-10-27 21:39:48 UTC
README
This PHP package allows you to convert MIME types to file extensions and Vice versa, and to add your own MIME types and file extensions.
You can change the functionality by implementing interfaces:
-
Devanych\Mime\MimeTypesInterface - contains methods to implement the functionality.
-
Devanych\Mime\MimeTypesMapsInterface - contains a map of MIME types and file extensions.
A guide with a detailed description in Russian language is available here.
Installation
This package requires PHP version 7.4 or later.
composer require devanych/mime-types
Usage MimeTypes
Creation:
use Devanych\Mime\MimeTypes; $mimeTypes = new MimeTypes();
Conversion:
/** * Gets the MIME types for the given file extension. * * @param string $extension * @return string[] an array of MIME types or an empty array if no match is found */ $mimeTypes->getMimeTypes('jpeg'); // ['image/jpeg', 'image/pjpeg'] /** * Gets the file extensions for the given MIME type. * * @param string $mimeType * @return string[] an array of extensions or an empty array if no match is found */ $mimeTypes->getExtensions('image/jpeg'); // ['jpeg', 'jpg', 'jpe']
Adding:
/** * Adds a custom map of MIME types and file extensions. * * The key is a MIME type and the value is an array of extensions. * * Example code: * * $map = [ * 'image/ico' => ['ico'], * 'image/icon' => ['ico'], * 'image/jp2' => ['jp2', 'jpg2'], * 'image/jpeg' => ['jpeg', 'jpg', 'jpe'], * 'image/jpeg2000' => ['jp2', 'jpg2'], * ]; * * If the map format is invalid, an `\InvalidArgumentException` will be thrown when the map is added. * * @param array $map */ $mimeTypes->addMap($map);
You can pass a map to the constructor when you create a
Devanych\Mime\MimeTypes
class, inside the constructor calls theaddMap()
method.
Usage MimeTypesAllowed
If you want to use only the allowed preset mime types and file extensions then use the Devanych\Mime\MimeTypesAllowed
instead of the Devanych\Mime\MimeTypes
.
use Devanych\Mime\MimeTypesAllowed; $map = [ 'image/gif' => ['gif'], 'image/png' => ['png'], 'image/jpeg' => ['jpeg', 'jpg', 'jpe'], ]; $mimeTypes = new MimeTypesAllowed($map);
When you create an instance of the Devanych\Mime\MimeTypesAllowed
class, you MUST pass the map. If you pass an empty or incorrect map, the exception \InvalidArgumentException
will be thrown.
When creating an instance of the
Devanych\Mime\MimeTypesAllowed
class, theaddMap()
method is called in the constructor, but for security reasons, a\LogicException
will be thrown if you try to call theaddMap()
method again.
The methods getMimeTypes()
and getExtensions()
work the same as in the Devanych\Mime\MimeTypes
, but the search is performed only in the preset mime types and file extensions that were passed to the constructor when creating an instance of the Devanych\Mime\MimeTypesAllowed
class.