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\Device\DistillerParameters; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Ghostscript\Enum\ImageDownsampleType; |
11
|
|
|
use GravityMedia\Ghostscript\Enum\MonoImageFilter; |
12
|
|
|
use GravityMedia\Ghostscript\Enum\PdfSettings; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The monochrome image compression distiller parameters trait |
16
|
|
|
* |
17
|
|
|
* @package GravityMedia\Ghostscript\Device\DistillerParameters |
18
|
|
|
*/ |
19
|
|
|
trait MonoImageCompressionTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get argument value |
23
|
|
|
* |
24
|
|
|
* @param string $name |
25
|
|
|
* |
26
|
|
|
* @return null|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 monochrome images |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
3 |
|
public function isAntiAliasMonoImages() |
52
|
|
|
{ |
53
|
3 |
|
$value = $this->getArgumentValue('-dAntiAliasMonoImages'); |
54
|
3 |
|
if (null === $value) { |
55
|
3 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set anti alias monochrome images flag |
63
|
|
|
* |
64
|
|
|
* @param bool $antiAliasMonoImages |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
3 |
|
public function setAntiAliasMonoImages($antiAliasMonoImages) |
69
|
|
|
{ |
70
|
3 |
|
$this->setArgument(sprintf('-dAntiAliasMonoImages=%s', $antiAliasMonoImages ? 'true' : 'false')); |
71
|
|
|
|
72
|
3 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether to downsample monochrome images |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
15 |
|
public function isDownsampleMonoImages() |
81
|
|
|
{ |
82
|
15 |
|
$value = $this->getArgumentValue('-dDownsampleMonoImages'); |
83
|
15 |
|
if (null === $value) { |
84
|
15 |
|
switch ($this->getPdfSettings()) { |
85
|
15 |
|
case PdfSettings::SCREEN: |
86
|
15 |
|
case PdfSettings::EBOOK: |
87
|
6 |
|
return true; |
88
|
9 |
|
default: |
89
|
9 |
|
return false; |
90
|
9 |
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
15 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set downsample monochrome images flag |
98
|
|
|
* |
99
|
|
|
* @param bool $downsampleMonoImages |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
15 |
|
public function setDownsampleMonoImages($downsampleMonoImages) |
104
|
|
|
{ |
105
|
15 |
|
$this->setArgument(sprintf('-dDownsampleMonoImages=%s', $downsampleMonoImages ? 'true' : 'false')); |
106
|
|
|
|
107
|
15 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Whether to encode monochrome images |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
3 |
|
public function isEncodeMonoImages() |
116
|
|
|
{ |
117
|
3 |
|
$value = $this->getArgumentValue('-dEncodeMonoImages'); |
118
|
3 |
|
if (null === $value) { |
119
|
3 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set encode monochrome images flag |
127
|
|
|
* |
128
|
|
|
* @param bool $encodeMonoImages |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
3 |
|
public function setEncodeMonoImages($encodeMonoImages) |
133
|
|
|
{ |
134
|
3 |
|
$this->setArgument(sprintf('-dEncodeMonoImages=%s', $encodeMonoImages ? 'true' : 'false')); |
135
|
|
|
|
136
|
3 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get monochrome image depth |
141
|
|
|
* |
142
|
|
|
* @return int |
143
|
|
|
*/ |
144
|
3 |
|
public function getMonoImageDepth() |
145
|
|
|
{ |
146
|
3 |
|
$value = $this->getArgumentValue('-dMonoImageDepth'); |
147
|
3 |
|
if (null === $value) { |
148
|
3 |
|
return -1; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
return intval($value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set monochrome image depth |
156
|
|
|
* |
157
|
|
|
* @param int $monoImageDepth |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
3 |
|
public function setMonoImageDepth($monoImageDepth) |
162
|
|
|
{ |
163
|
3 |
|
$this->setArgument(sprintf('-dMonoImageDepth=%s', $monoImageDepth)); |
164
|
|
|
|
165
|
3 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get monochrome image downsample threshold |
170
|
|
|
* |
171
|
|
|
* @return float |
172
|
|
|
*/ |
173
|
3 |
|
public function getMonoImageDownsampleThreshold() |
174
|
|
|
{ |
175
|
3 |
|
$value = $this->getArgumentValue('-dMonoImageDownsampleThreshold'); |
176
|
3 |
|
if (null === $value) { |
177
|
3 |
|
return 1.5; |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
return floatval($value); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set monochrome image downsample threshold |
185
|
|
|
* |
186
|
|
|
* @param float $monoImageDownsampleThreshold |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
3 |
|
public function setMonoImageDownsampleThreshold($monoImageDownsampleThreshold) |
191
|
|
|
{ |
192
|
3 |
|
$this->setArgument(sprintf('-dMonoImageDownsampleThreshold=%s', $monoImageDownsampleThreshold)); |
193
|
|
|
|
194
|
3 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get monochrome image downsample type |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
15 |
View Code Duplication |
public function getMonoImageDownsampleType() |
|
|
|
|
203
|
|
|
{ |
204
|
15 |
|
$value = $this->getArgumentValue('-dMonoImageDownsampleType'); |
205
|
15 |
|
if (null === $value) { |
206
|
15 |
|
switch ($this->getPdfSettings()) { |
207
|
15 |
|
case PdfSettings::PREPRESS: |
208
|
3 |
|
return ImageDownsampleType::BICUBIC; |
209
|
12 |
|
default: |
210
|
12 |
|
return ImageDownsampleType::SUBSAMPLE; |
211
|
12 |
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
15 |
|
return ltrim($value, '/'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set monochrome image downsample type |
219
|
|
|
* |
220
|
|
|
* @param string $monoImageDownsampleType |
221
|
|
|
* |
222
|
|
|
* @throws \InvalidArgumentException |
223
|
|
|
* |
224
|
|
|
* @return $this |
225
|
|
|
*/ |
226
|
18 |
|
public function setMonoImageDownsampleType($monoImageDownsampleType) |
227
|
|
|
{ |
228
|
18 |
|
$monoImageDownsampleType = ltrim($monoImageDownsampleType, '/'); |
229
|
18 |
|
if (!in_array($monoImageDownsampleType, ImageDownsampleType::values())) { |
230
|
3 |
|
throw new \InvalidArgumentException('Invalid monochrome image downsample type argument'); |
231
|
|
|
} |
232
|
|
|
|
233
|
15 |
|
$this->setArgument(sprintf('-dMonoImageDownsampleType=/%s', $monoImageDownsampleType)); |
234
|
|
|
|
235
|
15 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get monochrome image filter |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
12 |
|
public function getMonoImageFilter() |
244
|
|
|
{ |
245
|
12 |
|
$value = $this->getArgumentValue('-dMonoImageFilter'); |
246
|
12 |
|
if (null === $value) { |
247
|
3 |
|
return MonoImageFilter::CCITT_FAX_ENCODE; |
248
|
|
|
} |
249
|
|
|
|
250
|
9 |
|
return ltrim($value, '/'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set monochrome image filter |
255
|
|
|
* |
256
|
|
|
* @param string $monoImageFilter |
257
|
|
|
* |
258
|
|
|
* @throws \InvalidArgumentException |
259
|
|
|
* |
260
|
|
|
* @return $this |
261
|
|
|
*/ |
262
|
12 |
|
public function setMonoImageFilter($monoImageFilter) |
263
|
|
|
{ |
264
|
12 |
|
$monoImageFilter = ltrim($monoImageFilter, '/'); |
265
|
12 |
|
if (!in_array($monoImageFilter, MonoImageFilter::values())) { |
266
|
3 |
|
throw new \InvalidArgumentException('Invalid monochrome image filter argument'); |
267
|
|
|
} |
268
|
|
|
|
269
|
9 |
|
$this->setArgument(sprintf('-dMonoImageFilter=/%s', $monoImageFilter)); |
270
|
|
|
|
271
|
9 |
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get monochrome image resolution |
276
|
|
|
* |
277
|
|
|
* @return int |
278
|
|
|
*/ |
279
|
15 |
View Code Duplication |
public function getMonoImageResolution() |
|
|
|
|
280
|
|
|
{ |
281
|
15 |
|
$value = $this->getArgumentValue('-dMonoImageResolution'); |
282
|
15 |
|
if (null === $value) { |
283
|
15 |
|
switch ($this->getPdfSettings()) { |
284
|
15 |
|
case PdfSettings::PRINTER: |
285
|
15 |
|
case PdfSettings::PREPRESS: |
286
|
6 |
|
return 1200; |
287
|
9 |
|
default: |
288
|
9 |
|
return 300; |
289
|
9 |
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
15 |
|
return intval($value); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Set monochrome image resolution |
297
|
|
|
* |
298
|
|
|
* @param int $monoImageResolution |
299
|
|
|
* |
300
|
|
|
* @return $this |
301
|
|
|
*/ |
302
|
15 |
|
public function setMonoImageResolution($monoImageResolution) |
303
|
|
|
{ |
304
|
15 |
|
$this->setArgument(sprintf('-dMonoImageResolution=%s', $monoImageResolution)); |
305
|
|
|
|
306
|
15 |
|
return $this; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
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.