Completed
Branch master (0235df)
by Bjørn
13:03 queued 09:47
created

detectQualityOfJpgUsingImagickExtension()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 1
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\Helpers;
4
5
/**
6
 * Try to detect quality of a jpeg image using various tools.
7
 *
8
 * @package    WebPConvert
9
 * @author     Bjørn Rosell <[email protected]>
10
 * @since      Class available since Release 2.0.0
11
 */
12
class JpegQualityDetector
13
{
14
15
    /**
16
     * Try to detect quality of jpeg using imagick extension
17
     *
18
     * @param  string  $filename  A complete file path to file to be examined
19
     * @return int|null  Quality, or null if it was not possible to detect quality
20
     */
21 6
    private static function detectQualityOfJpgUsingImagickExtension($filename)
22
    {
23 6
        if (extension_loaded('imagick') && class_exists('\\Imagick')) {
24
            try {
25 6
                $img = new \Imagick($filename);
26
27
                // The required function is available as from PECL imagick v2.2.2
28 5
                if (method_exists($img, 'getImageCompressionQuality')) {
29 5
                    return $img->getImageCompressionQuality();
30
                }
31 1
            } catch (\Exception $e) {
32
                // Well well, it just didn't work out.
33
                // - But perhaps next method will work...
34
            }
35
        }
36 1
        return null;
37
    }
38
39
40
    /**
41
     * Try to detect quality of jpeg using imagick binary
42
     *
43
     * @param  string  $filename  A complete file path to file to be examined
44
     * @return int|null  Quality, or null if it was not possible to detect quality
45
     */
46 1
    private static function detectQualityOfJpgUsingImagickBinary($filename)
47
    {
48 1
        if (function_exists('exec')) {
49
            // Try Imagick using exec, and routing stderr to stdout (the "2>$1" magic)
50 1
            exec("identify -format '%Q' " . escapeshellarg($filename) . " 2>&1", $output, $returnCode);
51
            //echo 'out:' . print_r($output, true);
52 1
            if ((intval($returnCode) == 0) && (is_array($output)) && (count($output) == 1)) {
53
                return intval($output[0]);
54
            }
55
        }
56 1
        return null;
57
    }
58
59
60
    /**
61
     * Try to detect quality of jpeg using gmagick binary
62
     *
63
     * @param  string  $filename  A complete file path to file to be examined
64
     * @return int|null  Quality, or null if it was not possible to detect quality
65
     */
66 1
    private static function detectQualityOfJpgUsingGmagickBinary($filename)
67
    {
68 1
        if (function_exists('exec')) {
69
            // Try GraphicsMagick
70 1
            exec("gm identify -format '%Q' " . escapeshellarg($filename) . " 2>&1", $output, $returnCode);
71 1
            if ((intval($returnCode) == 0) && (is_array($output)) && (count($output) == 1)) {
72
                return intval($output[0]);
73
            }
74
        }
75 1
        return null;
76
    }
77
78
79
    /**
80
     * Try to detect quality of jpeg.
81
     *
82
     * Note: This method does not throw errors, but might dispatch warnings.
83
     * You can use the WarningsIntoExceptions class if it is critical to you that nothing gets "printed"
84
     *
85
     * @param  string  $filename  A complete file path to file to be examined
86
     * @return int|null  Quality, or null if it was not possible to detect quality
87
     */
88 8
    public static function detectQualityOfJpg($filename)
89
    {
90
91
        //trigger_error('warning test', E_USER_WARNING);
92
93
        // Test that file exists in order not to break things.
94 8
        if (!file_exists($filename)) {
95
            // One could argue that it would be better to throw an Exception...?
96 2
            return null;
97
        }
98
99
        // Try Imagick extension, if available
100 6
        $quality = self::detectQualityOfJpgUsingImagickExtension($filename);
101
102 6
        if (is_null($quality)) {
103 1
            $quality = self::detectQualityOfJpgUsingImagickBinary($filename);
104
        }
105
106 6
        if (is_null($quality)) {
107 1
            $quality = self::detectQualityOfJpgUsingGmagickBinary($filename);
108
        }
109
110 6
        return $quality;
111
    }
112
}
113