Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.57% |
11 / 14 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
ImageMimeTypeGuesser | |
78.57% |
11 / 14 |
|
66.67% |
4 / 6 |
9.80 | |
0.00% |
0 / 1 |
detect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
guess | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
lenientGuess | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
detectIsIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
guessIsIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lenientGuessIsIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * ImageMimeTypeGuesser - Detect / guess mime type of an image |
5 | * |
6 | * The library is born out of a discussion here: |
7 | * https://github.com/rosell-dk/webp-convert/issues/98 |
8 | * |
9 | * @link https://github.com/rosell-dk/image-mime-type-guesser |
10 | * @license MIT |
11 | */ |
12 | |
13 | namespace ImageMimeTypeGuesser; |
14 | |
15 | use \ImageMimeTypeGuesser\Detectors\Stack; |
16 | |
17 | class ImageMimeTypeGuesser |
18 | { |
19 | |
20 | |
21 | /** |
22 | * Try to detect mime type of image using all available detectors (the "stack" detector). |
23 | * |
24 | * Returns: |
25 | * - mime type (string) (if it is in fact an image, and type could be determined) |
26 | * - false (if it is not an image type that the server knowns about) |
27 | * - null (if nothing can be determined) |
28 | * |
29 | * @param string $filePath The path to the file |
30 | * @return string|false|null mimetype (if it is an image, and type could be determined), |
31 | * false (if it is not an image type that the server knowns about) |
32 | * or null (if nothing can be determined) |
33 | */ |
34 | public static function detect($filePath) |
35 | { |
36 | return Stack::detect($filePath); |
37 | } |
38 | |
39 | /** |
40 | * Try to detect mime type of image. If that fails, make a guess based on the file extension. |
41 | * |
42 | * Try to detect mime type of image using "stack" detector (all available methods, until one succeeds) |
43 | * If that fails (null), fall back to wild west guessing based solely on file extension. |
44 | * |
45 | * Returns: |
46 | * - mime type (string) (if it is an image, and type could be determined / mapped from file extension)) |
47 | * - false (if it is not an image type that the server knowns about) |
48 | * - null (if nothing can be determined) |
49 | * |
50 | * @param string $filePath The path to the file |
51 | * @return string|false|null mimetype (if it is an image, and type could be determined), |
52 | * false (if it is not an image type that the server knowns about) |
53 | * or null (if nothing can be determined) |
54 | */ |
55 | public static function guess($filePath) |
56 | { |
57 | $detectionResult = self::detect($filePath); |
58 | if (!is_null($detectionResult)) { |
59 | return $detectionResult; |
60 | } |
61 | |
62 | // fall back to the wild west method |
63 | return GuessFromExtension::guess($filePath); |
64 | } |
65 | |
66 | /** |
67 | * Try to detect mime type of image. If that fails, make a guess based on the file extension. |
68 | * |
69 | * Try to detect mime type of image using "stack" detector (all available methods, until one succeeds) |
70 | * If that fails (false or null), fall back to wild west guessing based solely on file extension. |
71 | * |
72 | * Returns: |
73 | * - mime type (string) (if it is an image, and type could be determined / mapped from file extension) |
74 | * - false (if it is not an image type that the server knowns about) |
75 | * - null (if nothing can be determined) |
76 | * |
77 | * @param string $filePath The path to the file |
78 | * @return string|false|null mimetype (if it is an image, and type could be determined / guessed), |
79 | * false (if it is not an image type that the server knowns about) |
80 | * or null (if nothing can be determined) |
81 | */ |
82 | public static function lenientGuess($filePath) |
83 | { |
84 | $detectResult = self::detect($filePath); |
85 | if ($detectResult === false) { |
86 | // The server does not recognize this image type. |
87 | // - but perhaps it is because it does not know about this image type. |
88 | // - so we turn to mapping the file extension |
89 | return GuessFromExtension::guess($filePath); |
90 | } elseif (is_null($detectResult)) { |
91 | // the mime type could not be determined |
92 | // perhaps we also in this case want to turn to mapping the file extension |
93 | return GuessFromExtension::guess($filePath); |
94 | } |
95 | return $detectResult; |
96 | } |
97 | |
98 | |
99 | /** |
100 | * Check if the *detected* mime type is in a list of accepted mime types. |
101 | * |
102 | * @param string $filePath The path to the file |
103 | * @param string[] $mimeTypes Mime types to accept |
104 | * @return bool Whether the detected mime type is in the $mimeTypes array or not |
105 | */ |
106 | public static function detectIsIn($filePath, $mimeTypes) |
107 | { |
108 | return in_array(self::detect($filePath), $mimeTypes); |
109 | } |
110 | |
111 | /** |
112 | * Check if the *guessed* mime type is in a list of accepted mime types. |
113 | * |
114 | * @param string $filePath The path to the file |
115 | * @param string[] $mimeTypes Mime types to accept |
116 | * @return bool Whether the detected / guessed mime type is in the $mimeTypes array or not |
117 | */ |
118 | public static function guessIsIn($filePath, $mimeTypes) |
119 | { |
120 | return in_array(self::guess($filePath), $mimeTypes); |
121 | } |
122 | |
123 | /** |
124 | * Check if the *leniently guessed* mime type is in a list of accepted mime types. |
125 | * |
126 | * @param string $filePath The path to the file |
127 | * @param string[] $mimeTypes Mime types to accept |
128 | * @return bool Whether the detected / leniently guessed mime type is in the $mimeTypes array or not |
129 | */ |
130 | public static function lenientGuessIsIn($filePath, $mimeTypes) |
131 | { |
132 | return in_array(self::lenientGuess($filePath), $mimeTypes); |
133 | } |
134 | } |