Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
GuessFromExtension | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
guess | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
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 | class GuessFromExtension |
13 | { |
14 | |
15 | |
16 | /** |
17 | * Make a wild guess based on file extension. |
18 | * |
19 | * - and I mean wild! |
20 | * |
21 | * Only most popular image types are recognized. |
22 | * Many are not. See this list: https://www.iana.org/assignments/media-types/media-types.xhtml |
23 | * - and the constants here: https://secure.php.net/manual/en/function.exif-imagetype.php |
24 | * |
25 | * If no mapping found, nothing is returned |
26 | * |
27 | * Returns: |
28 | * - mimetype (if file extension could be mapped to an image type), |
29 | * - false (if file extension could be mapped to a type known not to be an image type) |
30 | * - null (if file extension could not be mapped to any mime type, using our little list) |
31 | * |
32 | * @param string $filePath The path to the file |
33 | * @return string|false|null mimetype (if file extension could be mapped to an image type), |
34 | * false (if file extension could be mapped to a type known not to be an image type) |
35 | * or null (if file extension could not be mapped to any mime type, using our little list) |
36 | */ |
37 | public static function guess($filePath) |
38 | { |
39 | if (!@file_exists($filePath)) { |
40 | return false; |
41 | } |
42 | /* |
43 | Not using pathinfo, as it is locale aware, and I'm not sure if that could lead to problems |
44 | |
45 | if (!function_exists('pathinfo')) { |
46 | // This is really a just in case! - We do not expect this to happen. |
47 | // - in fact we have a test case asserting that this does not happen. |
48 | return null; |
49 | // |
50 | $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
51 | $fileExtension = strtolower($fileExtension); |
52 | }*/ |
53 | |
54 | return MimeMap::filenameToMime($filePath); |
55 | } |
56 | } |