Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| MimeContentType | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| doDetect | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace ImageMimeTypeGuesser\Detectors; |
| 4 | |
| 5 | class MimeContentType extends AbstractDetector |
| 6 | { |
| 7 | |
| 8 | /** |
| 9 | * Try to detect mime type of image using *mime_content_type()*. |
| 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 | // mime_content_type supposedly used to be deprecated, but it seems it isn't anymore |
| 24 | // it may return false on failure. |
| 25 | if (function_exists('mime_content_type')) { |
| 26 | try { |
| 27 | $result = mime_content_type($filePath); |
| 28 | if ($result !== false) { |
| 29 | if (strpos($result, 'image/') === 0) { |
| 30 | return $result; |
| 31 | } else { |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | } catch (\Exception $e) { |
| 36 | // we are unstoppable! |
| 37 | |
| 38 | // TODO: |
| 39 | // We should probably throw... - we will do in version 1.0.0 |
| 40 | //throw $e; |
| 41 | } |
| 42 | } |
| 43 | return null; |
| 44 | } |
| 45 | } |