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