public static function getMime($stream, $mime)
    {
        if (is_string($mime)) {
            return $mime;
        }
        if (!$mime || !$stream->isSeekable() || !$stream->isReadable()) {
            return;
        }
        $finfo = finfo_open(FILEINFO_MIME_TYPE);

        $old = $stream->tell();
        $stream->rewind();
        $end = $stream->end();

        $size = min($end - 0, 4);
        if ($size === 0) {
            return;
        }

        $stream->seek($size, SEEK_SET);
        $signature = $stream->read($size, false);

        $size = min($end - 0, 1024);
        $stream->rewind();
        $signature = $stream->read($size, false) . $signature;
        $stream->seek($old, SEEK_SET);

        return finfo_buffer($finfo, $signature);
    }
public static function getMime($stream, $mime)
    {
        if (is_string($mime)) {
            return $mime;
        }
        if (!$mime || !$stream->isSeekable() || !$stream->isReadable()) {
            return;
        }
        $finfo = finfo_open(FILEINFO_MIME_TYPE);

        $old = $stream->tell();
        $stream->rewind();
        $end = $stream->end();

        $size = min($end - 0, 4);
        if ($size === 0) {
            return;
        }

        $stream->seek($size, SEEK_SET);
        $signature = $stream->read($size, false);

        $size = min($end - 0, 1024);
        $stream->rewind();
        $signature = $stream->read($size, false) . $signature;
        $stream->seek($old, SEEK_SET);

        return finfo_buffer($finfo, $signature);
    }
public static function getMime ( $ stream , $ mime ) { if ( is_string ( $ mime ) ) { return $ mime ; } if ( ! $ mime || ! $ stream -> isSeekable ( ) || ! $ stream -> isReadable ( ) ) { return ; } $ finfo = finfo_open ( FILEINFO_MIME_TYPE ) ; $ old = $ stream -> tell ( ) ; $ stream -> rewind ( ) ; $ end = $ stream -> end ( ) ; $ size = min ( $ end - 0 , 4 ) ; if ( $ size === 0 ) { return ; } $ stream -> seek ( $ size , SEEK_SET ) ; $ signature = $ stream -> read ( $ size , false ) ; $ size = min ( $ end - 0 , 1024 ) ; $ stream -> rewind ( ) ; $ signature = $ stream -> read ( $ size , false ) . $ signature ; $ stream -> seek ( $ old , SEEK_SET ) ; return finfo_buffer ( $ finfo , $ signature ) ; }
Mime detector.
Concat the first 1024 bytes + the last 4 bytes of readable & seekable streams
to detext the mime info.

@param string $stream The stream to extract mime value from.
@param string $mime   The mime type detection. Possible values are:
-`true`    : auto detect the mime.
- a string : don't detect the mime and use the passed string instead.
-`false`   : don't detect the mime.
@return string        The detected mime.
Mime detector . Concat the first 1024 bytes + the last 4 bytes of readable & seekable streams to detext the mime info .