|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\BaseConverters\BaseTraits; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException; |
|
6
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\InvalidOptionTypeException; |
|
7
|
|
|
|
|
8
|
|
|
trait OptionsTrait |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** @var array Provided conversion options */ |
|
12
|
|
|
public $providedOptions; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array Calculated conversion options (merge of default options and provided options)*/ |
|
15
|
|
|
public $options; |
|
16
|
|
|
|
|
17
|
|
|
// The concrete converters must supply this method... |
|
18
|
|
|
abstract protected function getOptionDefinitionsExtra(); |
|
19
|
|
|
|
|
20
|
|
|
abstract protected function getMimeTypeOfSource(); |
|
21
|
|
|
|
|
22
|
|
|
public static $optionDefinitionsBasic = [ |
|
23
|
|
|
['quality', 'number|string', 'auto'], |
|
24
|
|
|
['max-quality', 'number', 85], |
|
25
|
|
|
['default-quality', 'number', 75], |
|
26
|
|
|
['metadata', 'string', 'none'], |
|
27
|
|
|
['lossless', 'boolean|string', false], |
|
28
|
|
|
['skip', 'boolean', false], |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set "provided options" |
|
33
|
|
|
* This also sets the internal options array, by merging in the default options |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $providedOptions (optional) |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setProvidedOptions($providedOptions = []) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->providedOptions = $providedOptions; |
|
41
|
|
|
|
|
42
|
|
|
if (isset($this->providedOptions['png'])) { |
|
43
|
|
|
if ($this->getMimeTypeOfSource() == 'image/png') { |
|
44
|
|
|
$this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['png']); |
|
45
|
|
|
// $this->logLn(print_r($this->providedOptions, true)); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (isset($this->providedOptions['jpeg'])) { |
|
50
|
|
|
if ($this->getMimeTypeOfSource() == 'image/jpeg') { |
|
51
|
|
|
$this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['jpeg']); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
// - Merge $defaultOptions into provided options |
|
55
|
|
|
$this->options = array_merge($this->getDefaultOptions(), $this->providedOptions); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
public function getAllOptionDefinitions() |
|
60
|
|
|
{ |
|
61
|
|
|
return array_merge(self::$optionDefinitionsBasic, $this->getOptionDefinitionsExtra()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getDefaultOptions() |
|
65
|
|
|
{ |
|
66
|
|
|
$defaults = []; |
|
67
|
|
|
foreach ($this->getAllOptionDefinitions() as list($name, $type, $default)) { |
|
68
|
|
|
$defaults[$name] = $default; |
|
69
|
|
|
} |
|
70
|
|
|
if ($this->getMimeTypeOfSource() == 'image/png') { |
|
71
|
|
|
$defaults['lossless'] = 'auto'; |
|
72
|
|
|
} |
|
73
|
|
|
return $defaults; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function checkOptions() |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->getAllOptionDefinitions() as $def) { |
|
79
|
|
|
list($optionName, $optionType) = $def; |
|
80
|
|
|
|
|
81
|
|
|
if (isset($this->providedOptions[$optionName])) { |
|
82
|
|
|
//$this->logLn($optionName); |
|
83
|
|
|
|
|
84
|
|
|
$actualType = gettype($this->providedOptions[$optionName]); |
|
85
|
|
|
if ($actualType != $optionType) { |
|
86
|
|
|
$optionType = str_replace('number', 'integer|double', $optionType); |
|
87
|
|
|
if (!in_array($actualType, explode('|', $optionType))) { |
|
88
|
|
|
throw new InvalidOptionTypeException( |
|
89
|
|
|
'The provided ' . $optionName . ' option is not a ' . $optionType . |
|
90
|
|
|
' (it is a ' . $actualType . ')' |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$optionValue = $this->providedOptions[$optionName]; |
|
96
|
|
|
|
|
97
|
|
|
if ($optionName == 'quality') { |
|
98
|
|
|
if ($actualType == 'string') { |
|
99
|
|
|
if ($optionValue != 'auto') { |
|
100
|
|
|
throw new InvalidOptionTypeException( |
|
101
|
|
|
'Quality option must be either "auto" or a number between 0-100. ' . |
|
102
|
|
|
'A string, "' . $optionValue . '" was given' |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
if (($optionValue < 0) || ($optionValue > 100)) { |
|
107
|
|
|
throw new InvalidOptionTypeException( |
|
108
|
|
|
'Quality option must be either "auto" or a number between 0-100. ' . |
|
109
|
|
|
'The number you provided (' . strval($optionValue) . ') is out of range.' |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (($optionName == 'lossless') && ($actualType == 'string') && ($optionValue != 'auto')) { |
|
116
|
|
|
throw new InvalidOptionTypeException( |
|
117
|
|
|
'Lossless option must be true, false or "auto". It was set to: "' . $optionValue . '"' |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($this->options['skip']) { |
|
124
|
|
|
if (($this->getMimeTypeOfSource() == 'image/png') && isset($this->options['png']['skip'])) { |
|
125
|
|
|
throw new ConversionSkippedException( |
|
126
|
|
|
'skipped conversion (configured to do so for PNG)' |
|
127
|
|
|
); |
|
128
|
|
|
} else { |
|
129
|
|
|
throw new ConversionSkippedException( |
|
130
|
|
|
'skipped conversion (configured to do so)' |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Prepare options. |
|
138
|
|
|
*/ |
|
139
|
|
|
/* |
|
140
|
|
|
private function prepareOptions() |
|
141
|
|
|
{ |
|
142
|
|
|
//$defaultOptions = self::$defaultOptions; |
|
143
|
|
|
|
|
144
|
|
|
// - Merge defaults of the converters extra options into the standard default options. |
|
145
|
|
|
//$defaultOptions = array_merge($defaultOptions, array_column(static::$extraOptions, 'default', 'name')); |
|
146
|
|
|
//print_r($this->getOptionDefinitionsExtra()); |
|
147
|
|
|
//$extra = []; |
|
148
|
|
|
//$this->getDefaultOptionsExtra(); |
|
149
|
|
|
//echo '<br>'; |
|
150
|
|
|
//print_r(static::$extraOptions); |
|
151
|
|
|
//print_r(array_column(static::$extraOptions, 'default', 'name')); |
|
152
|
|
|
//$defaultOptions = array_merge($defaultOptions, $this->getDefaultOptionsExtra()); |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
//throw new \Exception('extra!' . print_r($this->getConverterDisplayName(), true)); |
|
156
|
|
|
|
|
157
|
|
|
// - Merge $defaultOptions into provided options |
|
158
|
|
|
//$this->options = array_merge($defaultOptions, $this->options); |
|
159
|
|
|
//$this->options = array_merge($this->getDefaultOptions(), $providedOptions); |
|
160
|
|
|
|
|
161
|
|
|
if ($this->getMimeTypeOfSource() == 'png') { |
|
162
|
|
|
// skip png's ? |
|
163
|
|
|
if ($this->options['skip-pngs']) { |
|
164
|
|
|
throw new ConversionSkippedException( |
|
165
|
|
|
'PNG file skipped (configured to do so)' |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
// TODO: Here we could test if quality is 0-100 or auto. |
|
172
|
|
|
// and if not, throw something extending InvalidArgumentException (which is a LogicException) |
|
173
|
|
|
}*/ |
|
174
|
|
|
} |
|
175
|
|
|
|