PHP code example of intervention / mimesniffer

1. Go to this page and download the library: Download intervention/mimesniffer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

intervention / mimesniffer example snippets


use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageJpeg;

// universal factory method
$sniffer = MimeSniffer::create($content);

// or detect given string
$sniffer = MimeSniffer::createFromString($content);

// or detect given file
$sniffer = MimeSniffer::createFromFilename('image.jpg');

// or detect from file pointer
$sniffer = MimeSniffer::createFromFilename(fopen('test.jpg', 'r'));

// returns object of detected type 
$type = $sniffer->getType(); 

$bool = $type->isBinary(); // check if we have binary data
$bool = $type->isImage(); // check if we are dealing with an image
$bool = $type->isVideo(); // check video data was detected
$bool = $type->isAudio(); // check if we have detected audio data
$bool = $type->isArchive(); // check if an archive was detected
$type = (string) $type; // cast type to string (e.g. "image/jpeg")

// you can also check, if the content matches a specific type
$bool = $sniffer->matches(new ImageJpeg);

// or check, if the content matches an array of types
$bool = $sniffer->matches([ImageJpeg::class, ImageGif::class]);

// or check, if the content matches an array of type objects
$bool = $sniffer->matches([new ImageJpeg, $type]);

use Intervention\MimeSniffer\MimeSniffer;

// create instance with constructor
$sniffer = new MimeSniffer($content);

// with setter for given content
$type = $sniffer->setFromString($other_content)->getType();

// or with setter for filename
$type = $sniffer->setFromFilename('images/image.jpg')->getType();

// or with setter for file pointer
$type = $sniffer->setFromPointer(fopen('images/image.jpg', 'r'))->getType();