Passed
Push — master ( 2d60b9...33913d )
by Bjørn
01:46
created

src/ImageMimeTypeGuesser.php (1 issue)

1
<?php
2
3
/**
4
 * ImageMimeTypeGuesser - Detect / guess mime type of an image
5
 *
6
 * @link https://github.com/rosell-dk/image-mime-type-guesser
7
 * @license MIT
8
 */
9
10
namespace ImageMimeTypeGuesser;
11
12
use \ImageMimeTypeGuesser\Detectors\Stack;
13
14
class ImageMimeTypeGuesser
15
{
16
17
18
    /**
19
     *  Try to detect mime type of image using "stack" detector (all available methods, until one succeeds)
20
     *
21
     *  returns:
22
     *  - null  (if it cannot be determined)
23
     *  - false (if it is not an image that the server knows about)
24
     *  - mime  (if it is in fact an image, and type could be determined)
25
     *  @return  mime type | null | false.
26
     */
27
    public static function detect($filePath)
28
    {
29
        // Result of the discussion here:
30
        // https://github.com/rosell-dk/webp-convert/issues/98
31
32
        return Stack::detect($filePath);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ImageMimeTypeGues...tack::detect($filePath) returns the type false|string which is incompatible with the documented return type ImageMimeTypeGuesser\mime.
Loading history...
33
    }
34
35
    /**
36
     *  Try to detect mime type of image using "stack" detector (all available methods, until one succeeds)
37
     *  If that fails, fall back to wild west guessing based solely on file extension, which always has an answer
38
     *  (this method never returns null)
39
     *
40
     *  returns:
41
     *  - false (if it is not an image that the server knows about)
42
     *  - mime  (if it looks like an image)
43
     *  @return  mime type | false.
44
     */
45
    public static function guess($filePath)
46
    {
47
        $detectionResult = self::detect($filePath);
48
        if (!is_null($detectionResult)) {
49
            return $detectionResult;
50
        }
51
52
        // fall back to the wild west method
53
        return GuessFromExtension::guess($filePath);
54
    }
55
56
    /**
57
     *  Try to detect mime type of image using "stack" detector (all available methods, until one succeeds)
58
     *  But do not take no for an answer, as "no", really only means that the server has not registred that mime type
59
     *
60
     *  (this method never returns null)
61
     *
62
     *  returns:
63
     *  - false (if it can be determined that this is not an image)
64
     *  - mime  (if it looks like an image)
65
     *  @return  mime type | false.
66
     */
67
    public static function lenientGuess($filePath)
68
    {
69
        $detectResult = self::detect($filePath);
70
        if ($detectResult === false) {
71
            // The server does not recognize this image type.
72
            // - but perhaps it is because it does not know about this image type.
73
            // - so we turn to mapping the file extension
74
            return GuessFromExtension::guess($filePath);
75
        } elseif (is_null($detectResult)) {
76
            // the mime type could not be determined
77
            // perhaps we also in this case want to turn to mapping the file extension
78
            return GuessFromExtension::guess($filePath);
79
        }
80
        return $detectResult;
81
    }
82
83
84
85
    public static function guessIsIn($filePath, $mimeTypes)
86
    {
87
        return in_array(self::guess($filePath), $mimeTypes);
88
    }
89
90
    public static function detectIsIn($filePath, $mimeTypes)
91
    {
92
        return in_array(self::detect($filePath), $mimeTypes);
93
    }
94
95
    public static function lenientGuessIsIn($filePath, $mimeTypes)
96
    {
97
        return in_array(self::lenientGuess($filePath), $mimeTypes);
98
    }
99
}
100