Completed
Push — master ( 7db5b7...e686f3 )
by Daniel
03:06
created

setGrayImageDownsampleType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 2
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\ColorAndGrayImageFilter;
11
use GravityMedia\Ghostscript\Enum\ImageDownsampleType;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The grayscale image compression distiller parameters trait
16
 *
17
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
18
 */
19 View Code Duplication
trait GrayImageCompressionTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

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