Giter Club home page Giter Club logo

league-mime-type-detection's Introduction

League Mime Type Detection

Author Source Code Latest Version Software License Build Status Coverage Status Quality Score Total Downloads php 7.2+

This package supplies a generic mime-type detection interface with a finfo based implementation.

Usage

composer require league/mime-type-detection

Consumer interface

Your code is advised to couple to the following interfaces:

  • League\MimetypeDetection\MimeTypeDetector
    This contract is used to detect mimetypes based on file names and file contents.
  • League\MimetypeDetection\ExtensionLookup
    This contract is used to lookup one or all mimetypes for a given file extension. Added in 1.13.0.

Detectors

Finfo with extension fallback:

$detector = new League\MimeTypeDetection\FinfoMimeTypeDetector();

// Detect by contents, fall back to detection by extension.
$mimeType = $detector->detectMimeType('some/path.php', 'string contents');

// Detect by contents only, no extension fallback.
$mimeType = $detector->detectMimeTypeFromBuffer('string contents');

// Detect by actual file, no extension fallback.
$mimeType = $detector->detectMimeTypeFromFile('existing/path.php');

// Only detect by extension
$mimeType = $detector->detectMimeTypeFromPath('any/path.php');

// Constructor options
$detector = new League\MimeTypeDetection\FinfoMimeTypeDetector(
  $pathToMimeDatabase, // Custom mime database location, default: ''
  $customExtensionMap, // Custom extension fallback mapp, default: null
  $bufferSampleSize // Buffer size limit, used to take a sample (substr) from the input buffer to reduce memory consumption.
);

Extension only:

$detector = new League\MimeTypeDetection\ExtensionMimeTypeDetector();

// Only detect by extension, ignores the file contents
$mimeType = $detector->detectMimeType('some/path.php', 'string contents');

// Always returns null
$mimeType = $detector->detectMimeTypeFromBuffer('string contents');

// Only detect by extension
$mimeType = $detector->detectMimeTypeFromFile('existing/path.php');

// Only detect by extension
$mimeType = $detector->detectMimeTypeFromPath('any/path.php');

Extension lookup by mime-type

This feature was added in version 1.13.0

The various implementations can look up the extensions that can be used for a given mime-type.

// string | null
$extension = $detector->lookupExtension($mime$type);

// array<string>
$allExtensions = $detector->lookupAllExtensions($mimeType);

Extension mime-type lookup

As a fallback for finfo based lookup, an extension map is used to determine the mime-type. There is an advised implementation shipped, which is generated from information collected by the npm package mime-db.

Provided extension maps

Generated:

$map = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();

// string mime-type or NULL
$mimeType = $map->lookupMimeType('png');

Overriding decorator

$innerMap = new League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap();
$map = new League\MimeTypeDetection\OverridingExtensionToMimeTypeMap($innerMap, ['png' => 'custom/mimetype']);

// string "custom/mimetype"
$mimeType = $map->lookupMimeType('png');

Empty:

$map = new League\MimeTypeDetection\EmptyExtensionToMimeTypeMap();

// Always returns NULL
$mimeType = $map->lookupMimeType('png');

league-mime-type-detection's People

Contributors

frankdejonge avatar blackbitdevs avatar grahamcampbell avatar awilum avatar villfa avatar maglnet avatar phil-davis avatar remicollet avatar rgson avatar rouatbih avatar alex-dna avatar lctrs 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.