Completed
Push — master ( 5f5fca...2fa1e0 )
by Bjørn
12:28 queued 02:24
created

JpegQualityDetector   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 51.52%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 99
ccs 17
cts 33
cp 0.5152
rs 10
c 0
b 0
f 0
wmc 19

4 Methods

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