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