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