Giter VIP home page Giter VIP logo

mime-types's Introduction

Mime Types

License Latest Stable Version Total Downloads GitHub Build Status GitHub Static Analysis Status Scrutinizer Code Coverage Scrutinizer Code Quality

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:

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 the addMap() 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, the addMap() method is called in the constructor, but for security reasons, a \LogicException will be thrown if you try to call the addMap() 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.

mime-types's People

Contributors

devanych avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.