ExifImageType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 35
ccs 6
cts 7
cp 0.8571
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A doDetect() 0 19 4
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 1
    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 1
        if (function_exists('exif_imagetype')) {
28
            try {
29 1
                $imageType = exif_imagetype($filePath);
30 1
                return ($imageType ? image_type_to_mime_type($imageType) : false);
31 1
            } 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 1
                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
}
44