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
|
|
|
* TODO: jp2, jpx, ... |
28
|
|
|
* Returns: |
29
|
|
|
* - mimetype (if file extension could be mapped to an image type), |
30
|
|
|
* - false (if file extension could be mapped to a type known not to be an image type) |
31
|
|
|
* - null (if file extension could not be mapped to any mime type, using our little list) |
32
|
|
|
* |
33
|
|
|
* @param string $filePath The path to the file |
34
|
|
|
* @return string|false|null mimetype (if file extension could be mapped to an image type), |
35
|
|
|
* false (if file extension could be mapped to a type known not to be an image type) |
36
|
|
|
* or null (if file extension could not be mapped to any mime type, using our little list) |
37
|
|
|
*/ |
38
|
1 |
|
public static function guess($filePath) |
39
|
|
|
{ |
40
|
1 |
|
if (!@file_exists($filePath)) { |
41
|
1 |
|
return false; |
42
|
|
|
} |
43
|
|
|
/* |
44
|
|
|
Not using pathinfo, as it is locale aware, and I'm not sure if that could lead to problems |
45
|
|
|
|
46
|
|
|
if (!function_exists('pathinfo')) { |
47
|
|
|
// This is really a just in case! - We do not expect this to happen. |
48
|
|
|
// - in fact we have a test case asserting that this does not happen. |
49
|
|
|
return null; |
50
|
|
|
// |
51
|
|
|
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
52
|
|
|
$fileExtension = strtolower($fileExtension); |
53
|
|
|
}*/ |
54
|
|
|
|
55
|
1 |
|
$result = preg_match('#\\.([^.]*)$#', $filePath, $matches); |
56
|
1 |
|
if ($result !== 1) { |
57
|
1 |
|
return null; |
58
|
|
|
} |
59
|
1 |
|
$fileExtension = $matches[1]; |
60
|
|
|
|
61
|
|
|
// Trivial image mime types |
62
|
1 |
|
if (in_array($fileExtension, ['apng', 'avif', 'bmp', 'gif', 'jpeg', 'png', 'tiff', 'webp'])) { |
63
|
1 |
|
return 'image/' . $fileExtension; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Common extensions that are definitely not images |
67
|
1 |
|
if (in_array($fileExtension, ['txt', 'doc', 'zip', 'gz', 'exe'])) { |
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Non-trivial image mime types |
72
|
1 |
|
switch ($fileExtension) { |
73
|
1 |
|
case 'ico': |
74
|
1 |
|
case 'cur': |
75
|
1 |
|
return 'image/x-icon'; // or perhaps 'vnd.microsoft.icon' ? |
76
|
|
|
|
77
|
1 |
|
case 'jpg': |
78
|
1 |
|
return 'image/jpeg'; |
79
|
|
|
|
80
|
1 |
|
case 'svg': |
81
|
1 |
|
return 'image/svg+xml'; |
82
|
|
|
|
83
|
1 |
|
case 'tif': |
84
|
1 |
|
return 'image/tiff'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// We do not know this extension, return null |
88
|
1 |
|
return null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|