Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ExifImageType | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |
0.00% |
0 / 1 |
| doDetect | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace ImageMimeTypeGuesser\Detectors; |
| 4 | |
| 5 | use \ImageMimeTypeGuesser\Detectors\AbstractDetector; |
| 6 | |
| 7 | class ExifImageType extends AbstractDetector |
| 8 | { |
| 9 | |
| 10 | /** |
| 11 | * Try to detect mime type of image using *exif_imagetype*. |
| 12 | * |
| 13 | * Returns: |
| 14 | * - mime type (string) (if it is in fact an image, and type could be determined) |
| 15 | * - false (if it is not an image type that the server knowns about) |
| 16 | * - null (if nothing can be determined) |
| 17 | * |
| 18 | * @param string $filePath The path to the file |
| 19 | * @return string|false|null mimetype (if it is an image, and type could be determined), |
| 20 | * false (if it is not an image type that the server knowns about) |
| 21 | * or null (if nothing can be determined) |
| 22 | */ |
| 23 | protected function doDetect($filePath) |
| 24 | { |
| 25 | // exif_imagetype is fast, however not available on all systems, |
| 26 | // It may return false. In that case we can rely on that the file is not an image (and return false) |
| 27 | if (function_exists('exif_imagetype')) { |
| 28 | try { |
| 29 | $imageType = exif_imagetype($filePath); |
| 30 | return ($imageType ? image_type_to_mime_type($imageType) : false); |
| 31 | } catch (\Exception $e) { |
| 32 | // Might for example get "Read error!" |
| 33 | // (for some reason, this happens on very small files) |
| 34 | // We handle such errors as indeterminable (null) |
| 35 | return null; |
| 36 | // well well, don't let this stop us |
| 37 | //echo $e->getMessage(); |
| 38 | //throw($e); |
| 39 | } |
| 40 | } |
| 41 | return null; |
| 42 | } |
| 43 | } |