Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| FInfo | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |
0.00% |
0 / 1 |
| doDetect | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace ImageMimeTypeGuesser\Detectors; |
| 4 | |
| 5 | class FInfo extends AbstractDetector |
| 6 | { |
| 7 | |
| 8 | /** |
| 9 | * Try to detect mime type of image using *finfo* class. |
| 10 | * |
| 11 | * Returns: |
| 12 | * - mime type (string) (if it is in fact an image, and type could be determined) |
| 13 | * - false (if it is not an image type that the server knowns about) |
| 14 | * - null (if nothing can be determined) |
| 15 | * |
| 16 | * @param string $filePath The path to the file |
| 17 | * @return string|false|null mimetype (if it is an image, and type could be determined), |
| 18 | * false (if it is not an image type that the server knowns about) |
| 19 | * or null (if nothing can be determined) |
| 20 | */ |
| 21 | protected function doDetect($filePath) |
| 22 | { |
| 23 | |
| 24 | if (class_exists('finfo')) { |
| 25 | // phpcs:ignore PHPCompatibility.PHP.NewClasses.finfoFound |
| 26 | $finfo = new \finfo(FILEINFO_MIME); |
| 27 | $result = $finfo->file($filePath); |
| 28 | if ($result === false) { |
| 29 | // false means an error occured |
| 30 | return null; |
| 31 | } else { |
| 32 | $mime = explode('; ', $result); |
| 33 | $result = $mime[0]; |
| 34 | |
| 35 | if (strpos($result, 'image/') === 0) { |
| 36 | return $result; |
| 37 | } else { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return null; |
| 43 | } |
| 44 | } |