|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\Converters\BaseTraits; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Convert\Helpers\JpegQualityDetector; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait for handling the "quality:auto" option. |
|
9
|
|
|
* |
|
10
|
|
|
* This trait is only used in the AbstractConverter class. It has been extracted into a |
|
11
|
|
|
* trait in order to bundle the methods concerning auto quality. |
|
12
|
|
|
* |
|
13
|
|
|
* @package WebPConvert |
|
14
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
15
|
|
|
* @since Class available since Release 2.0.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
trait AutoQualityTrait |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var boolean Whether the quality option has been processed or not */ |
|
21
|
|
|
private $processed = false; |
|
22
|
|
|
|
|
23
|
|
|
/** @var boolean Whether the quality of the source could be detected or not (set upon processing) */ |
|
24
|
|
|
private $qualityCouldNotBeDetected = false; |
|
25
|
|
|
|
|
26
|
|
|
/** @var integer The calculated quality (set upon processing - on successful detection) */ |
|
27
|
|
|
private $calculatedQuality; |
|
28
|
|
|
|
|
29
|
|
|
abstract public function getMimeTypeOfSource(); |
|
30
|
|
|
abstract public function logLn($msg, $style = ''); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Determine if quality detection is required but failing. |
|
34
|
|
|
* |
|
35
|
|
|
* It is considered "required" when: |
|
36
|
|
|
* - Mime type is "image/jpeg" |
|
37
|
|
|
* - Quality is set to "auto" |
|
38
|
|
|
* |
|
39
|
|
|
* If quality option hasn't been proccessed yet, it is triggered. |
|
40
|
|
|
* |
|
41
|
|
|
* @return boolean |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function isQualityDetectionRequiredButFailing() |
|
44
|
|
|
{ |
|
45
|
3 |
|
$this->processQualityOptionIfNotAlready(); |
|
46
|
3 |
|
return $this->qualityCouldNotBeDetected; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get calculated quality. |
|
51
|
|
|
* |
|
52
|
|
|
* If the "quality" option is a number, that number is returned. |
|
53
|
|
|
* If mime type of source is something else than "image/jpeg", the "default-quality" option is returned |
|
54
|
|
|
* If quality is "auto" and source is a jpeg image, it will be attempted to detect jpeg quality. |
|
55
|
|
|
* In case of failure, the value of the "default-quality" option is returned. |
|
56
|
|
|
* In case of success, the detected quality is returned, or the value of the "max-quality" if that is lower. |
|
57
|
|
|
* |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function getCalculatedQuality() |
|
61
|
|
|
{ |
|
62
|
8 |
|
$this->processQualityOptionIfNotAlready(); |
|
63
|
8 |
|
return $this->calculatedQuality; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Process the quality option if it is not already processed. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
8 |
|
private function processQualityOptionIfNotAlready() |
|
72
|
|
|
{ |
|
73
|
8 |
|
if (!$this->processed) { |
|
74
|
8 |
|
$this->processed = true; |
|
75
|
8 |
|
$this->processQualityOption(); |
|
76
|
|
|
} |
|
77
|
8 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Process the quality option. |
|
81
|
|
|
* |
|
82
|
|
|
* Sets the private property "calculatedQuality" according to the description for the getCalculatedQuality |
|
83
|
|
|
* function. |
|
84
|
|
|
* In case quality detection was attempted and failed, the private property "qualityCouldNotBeDetected" is set |
|
85
|
|
|
* to true. This is used by the "isQualityDetectionRequiredButFailing" (and documented there too). |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
8 |
|
private function processQualityOption() |
|
90
|
|
|
{ |
|
91
|
8 |
|
$options = $this->options; |
|
92
|
8 |
|
$source = $this->source; |
|
93
|
|
|
|
|
94
|
8 |
|
$q = $options['quality']; |
|
95
|
8 |
|
if ($q == 'auto') { |
|
96
|
6 |
|
if (($this->getMimeTypeOfSource() == 'image/jpeg')) { |
|
97
|
4 |
|
$q = JpegQualityDetector::detectQualityOfJpg($source); |
|
98
|
4 |
|
if (is_null($q)) { |
|
99
|
1 |
|
$q = $options['default-quality']; |
|
100
|
1 |
|
$this->logLn( |
|
101
|
|
|
'Quality of source could not be established (Imagick or GraphicsMagick is required)' . |
|
102
|
1 |
|
' - Using default instead (' . $options['default-quality'] . ').' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
1 |
|
$this->qualityCouldNotBeDetected = true; |
|
106
|
|
|
} else { |
|
107
|
3 |
|
if ($q > $options['max-quality']) { |
|
108
|
2 |
|
$this->logLn( |
|
109
|
2 |
|
'Quality of source is ' . $q . '. ' . |
|
110
|
2 |
|
'This is higher than max-quality, so using max-quality instead (' . |
|
111
|
2 |
|
$options['max-quality'] . ')' |
|
112
|
|
|
); |
|
113
|
|
|
} else { |
|
114
|
1 |
|
$this->logLn('Quality set to same as source: ' . $q); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
4 |
|
$q = min($q, $options['max-quality']); |
|
118
|
|
|
} else { |
|
119
|
|
|
//$q = $options['default-quality']; |
|
120
|
2 |
|
$q = min($options['default-quality'], $options['max-quality']); |
|
121
|
6 |
|
$this->logLn('Quality: ' . $q . '. '); |
|
122
|
|
|
} |
|
123
|
|
|
} else { |
|
124
|
3 |
|
$this->logLn( |
|
125
|
3 |
|
'Quality: ' . $q . '. ' |
|
126
|
|
|
); |
|
127
|
3 |
|
if (($this->getMimeTypeOfSource() == 'image/jpeg')) { |
|
128
|
1 |
|
$this->logLn( |
|
129
|
1 |
|
'Consider setting quality to "auto" instead. It is generally a better idea' |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
8 |
|
$this->calculatedQuality = $q; |
|
134
|
8 |
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|