1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Ghostscript package |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Ghostscript\Devices\DistillerParameters; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Ghostscript\Devices\DistillerParametersInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The color image compression distiller parameters trait |
14
|
|
|
* |
15
|
|
|
* @package GravityMedia\Ghostscript\Devices\DistillerParameters |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
trait ColorImageCompressionTrait |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Available color image downsample type values |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected static $colorImageDownsampleTypeValues = [ |
25
|
|
|
DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_AVERAGE, |
26
|
|
|
DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_BICUBIC, |
27
|
|
|
DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_SUBSAMPLE, |
28
|
|
|
DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_NONE, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Available color image filter values |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
protected static $colorImageFilterValues = [ |
37
|
|
|
DistillerParametersInterface::IMAGE_FILTER_DCT_ENCODE, |
38
|
|
|
DistillerParametersInterface::IMAGE_FILTER_FLATE_ENCODE |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get argument value |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
abstract protected function getArgumentValue($name); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set argument |
52
|
|
|
* |
53
|
|
|
* @param string $argument |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
abstract protected function setArgument($argument); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Whether to anti alias color images |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function isAntiAliasColorImages() |
65
|
|
|
{ |
66
|
|
|
$value = $this->getArgumentValue('-dAntiAliasColorImages'); |
67
|
|
|
if (null === $value) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set anti alias color images flag |
76
|
|
|
* |
77
|
|
|
* @param bool $antiAliasColorImages |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setAntiAliasColorImages($antiAliasColorImages) |
82
|
|
|
{ |
83
|
|
|
$this->setArgument(sprintf('-dAntiAliasColorImages=%s', $antiAliasColorImages ? 'true' : 'false')); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Whether to auto filter color images |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isAutoFilterColorImages() |
94
|
|
|
{ |
95
|
|
|
$value = $this->getArgumentValue('-dAutoFilterColorImages'); |
96
|
|
|
if (null === $value) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set auto filter color images flag |
105
|
|
|
* |
106
|
|
|
* @param bool $autoFilterColorImages |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setAutoFilterColorImages($autoFilterColorImages) |
111
|
|
|
{ |
112
|
|
|
$this->setArgument(sprintf('-dAutoFilterColorImages=%s', $autoFilterColorImages ? 'true' : 'false')); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get color image depth |
119
|
|
|
* |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getColorImageDepth() |
123
|
|
|
{ |
124
|
|
|
$value = $this->getArgumentValue('-dColorImageDepth'); |
125
|
|
|
if (null === $value) { |
126
|
|
|
return -1; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return intval($value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set color image depth |
134
|
|
|
* |
135
|
|
|
* @param int $colorImageDepth |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function setColorImageDepth($colorImageDepth) |
140
|
|
|
{ |
141
|
|
|
$this->setArgument(sprintf('-dColorImageDepth=%s', $colorImageDepth)); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get color image downsample threshold |
148
|
|
|
* |
149
|
|
|
* @return float |
150
|
|
|
*/ |
151
|
|
|
public function getColorImageDownsampleThreshold() |
152
|
|
|
{ |
153
|
|
|
$value = $this->getArgumentValue('-dColorImageDownsampleThreshold'); |
154
|
|
|
if (null === $value) { |
155
|
|
|
return 1.5; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return floatval($value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set color image downsample threshold |
163
|
|
|
* |
164
|
|
|
* @param float $colorImageDownsampleThreshold |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setColorImageDownsampleThreshold($colorImageDownsampleThreshold) |
169
|
|
|
{ |
170
|
|
|
$this->setArgument(sprintf('-dColorImageDownsampleThreshold=%s', $colorImageDownsampleThreshold)); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get color image downsample type |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getColorImageDownsampleType() |
181
|
|
|
{ |
182
|
|
|
$value = $this->getArgumentValue('-dColorImageDownsampleType'); |
183
|
|
|
if (null === $value) { |
184
|
|
|
return DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_SUBSAMPLE; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return substr($value, 1); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set color image downsample type |
192
|
|
|
* |
193
|
|
|
* @param string $colorImageDownsampleType |
194
|
|
|
* |
195
|
|
|
* @throws \InvalidArgumentException |
196
|
|
|
* |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function setColorImageDownsampleType($colorImageDownsampleType) |
200
|
|
|
{ |
201
|
|
|
if (!in_array($colorImageDownsampleType, static::$colorImageDownsampleTypeValues)) { |
202
|
|
|
throw new \InvalidArgumentException('Invalid color image downsample type argument'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->setArgument(sprintf('-dColorImageDownsampleType=/%s', $colorImageDownsampleType)); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get color image filter |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getColorImageFilter() |
216
|
|
|
{ |
217
|
|
|
$value = $this->getArgumentValue('-dColorImageFilter'); |
218
|
|
|
if (null === $value) { |
219
|
|
|
return DistillerParametersInterface::IMAGE_FILTER_DCT_ENCODE; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return substr($value, 1); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set color image filter |
227
|
|
|
* |
228
|
|
|
* @param string $colorImageFilter |
229
|
|
|
* |
230
|
|
|
* @throws \InvalidArgumentException |
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function setColorImageFilter($colorImageFilter) |
235
|
|
|
{ |
236
|
|
|
if (!in_array($colorImageFilter, static::$colorImageFilterValues)) { |
237
|
|
|
throw new \InvalidArgumentException('Invalid color image filter argument'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->setArgument(sprintf('-dColorImageFilter=/%s', $colorImageFilter)); |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get color image resolution |
247
|
|
|
* |
248
|
|
|
* @return int |
249
|
|
|
*/ |
250
|
|
|
public function getColorImageResolution() |
251
|
|
|
{ |
252
|
|
|
$value = $this->getArgumentValue('-dColorImageResolution'); |
253
|
|
|
if (null === $value) { |
254
|
|
|
return 72; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return intval($value); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set color image resolution |
262
|
|
|
* |
263
|
|
|
* @param int $colorImageResolution |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
|
|
public function setColorImageResolution($colorImageResolution) |
268
|
|
|
{ |
269
|
|
|
$this->setArgument(sprintf('-dColorImageResolution=%s', $colorImageResolution)); |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Whether to downsample color images |
276
|
|
|
* |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function isDownsampleColorImages() |
280
|
|
|
{ |
281
|
|
|
$value = $this->getArgumentValue('-dDownsampleColorImages'); |
282
|
|
|
if (null === $value) { |
283
|
|
|
return false; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Set downsample color images flag |
291
|
|
|
* |
292
|
|
|
* @param bool $downsampleColorImages |
293
|
|
|
* |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
|
|
public function setDownsampleColorImages($downsampleColorImages) |
297
|
|
|
{ |
298
|
|
|
$this->setArgument(sprintf('-dDownsampleColorImages=%s', $downsampleColorImages ? 'true' : 'false'));; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Whether to encode color images |
305
|
|
|
* |
306
|
|
|
* @return bool |
307
|
|
|
*/ |
308
|
|
|
public function isEncodeColorImages() |
309
|
|
|
{ |
310
|
|
|
$value = $this->getArgumentValue('-dEncodeColorImages'); |
311
|
|
|
if (null === $value) { |
312
|
|
|
return true; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Set encode color images flag |
320
|
|
|
* |
321
|
|
|
* @param bool $encodeColorImages |
322
|
|
|
* |
323
|
|
|
* @return $this |
324
|
|
|
*/ |
325
|
|
|
public function setEncodeColorImages($encodeColorImages) |
326
|
|
|
{ |
327
|
|
|
$this->setArgument(sprintf('-dEncodeColorImages=%s', $encodeColorImages ? 'true' : 'false')); |
328
|
|
|
|
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.