Completed
Push — master ( cefa14...785c06 )
by Daniel
03:06
created

getGrayImageDepth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 0
cts 5
cp 0
rs 9.6667
nc 2
cc 2
eloc 5
nop 0
crap 6
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 grayscale image compression distiller parameters trait
14
 *
15
 * @package GravityMedia\Ghostscript\Devices\DistillerParameters
16
 */
17 View Code Duplication
trait GrayscaleImageCompressionTrait
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...
18
{
19
    /**
20
     * Available grayscale image downsample type values
21
     *
22
     * @var string[]
23
     */
24
    protected static $grayImageDownsampleTypeValues = [
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 grayscale image filter values
33
     *
34
     * @var string[]
35
     */
36
    protected static $grayImageFilterValues = [
37
        DistillerParametersInterface::IMAGE_FILTER_DCT_ENCODE,
38
        DistillerParametersInterface::IMAGE_FILTER_FLATE_ENCODE
39
    ];
40
41
    /**
42
     * Get argument value
43
     *
44
     * @param string $name
45
     *
46
     * @return string
47
     */
48
    abstract protected function getArgumentValue($name);
49
50
    /**
51
     * Set argument
52
     *
53
     * @param string $argument
54
     *
55
     * @return $this
56
     */
57
    abstract protected function setArgument($argument);
58
59
    /**
60
     * Whether to anti alias grayscale images
61
     *
62
     * @return bool
63
     */
64
    public function isAntiAliasGrayImages()
65
    {
66
        $value = $this->getArgumentValue('-dAntiAliasGrayImages');
67
        if (null === $value) {
68
            return false;
69
        }
70
71
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
72
    }
73
74
    /**
75
     * Set anti alias grayscale images flag
76
     *
77
     * @param bool $antiAliasGrayImages
78
     *
79
     * @return $this
80
     */
81
    public function setAntiAliasGrayImages($antiAliasGrayImages)
82
    {
83
        $this->setArgument(sprintf('-dAntiAliasGrayImages=%s', $antiAliasGrayImages ? 'true' : 'false'));
84
85
        return $this;
86
    }
87
88
    /**
89
     * Whether to auto filter grayscale images
90
     *
91
     * @return bool
92
     */
93
    public function isAutoFilterGrayImages()
94
    {
95
        $value = filter_var($this->getArgumentValue('-dAutoFilterGrayImages'), FILTER_VALIDATE_BOOLEAN);
96
        if (null === $value) {
97
            return true;
98
        }
99
100
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
101
    }
102
103
    /**
104
     * Set auto filter grayscale images flag
105
     *
106
     * @param bool $autoFilterGrayImages
107
     *
108
     * @return $this
109
     */
110
    public function setAutoFilterGrayImages($autoFilterGrayImages)
111
    {
112
        $this->setArgument(sprintf('-dAutoFilterGrayImages=%s', $autoFilterGrayImages ? 'true' : 'false'));
113
114
        return $this;
115
    }
116
117
    /**
118
     * Whether to downsample gray images
119
     *
120
     * @return bool
121
     */
122
    public function isDownsampleGrayImages()
123
    {
124
        $value = $this->getArgumentValue('-dDownsampleGrayImages');
125
        if (null === $value) {
126
            return false;
127
        }
128
129
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
130
    }
131
132
    /**
133
     * Set downsample gray images flag
134
     *
135
     * @param bool $downsampleGrayImages
136
     *
137
     * @return $this
138
     */
139
    public function setDownsampleGrayImages($downsampleGrayImages)
140
    {
141
        $this->setArgument(sprintf('-dDownsampleGrayImages=%s', $downsampleGrayImages ? 'true' : 'false'));
142
143
        return $this;
144
    }
145
146
    /**
147
     * Whether to encode grayscale images
148
     *
149
     * @return bool
150
     */
151
    public function isEncodeGrayImages()
152
    {
153
        $value = $this->getArgumentValue('-dEncodeGrayImages');
154
        if (null === $value) {
155
            return true;
156
        }
157
158
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
159
    }
160
161
    /**
162
     * Set encode grayscale images flag
163
     *
164
     * @param bool $encodeGrayImages
165
     *
166
     * @return $this
167
     */
168
    public function setEncodeGrayImages($encodeGrayImages)
169
    {
170
        $this->setArgument(sprintf('-dEncodeGrayImages=%s', $encodeGrayImages ? 'true' : 'false'));
171
172
        return $this;
173
    }
174
175
    /**
176
     * Get gray image depth
177
     *
178
     * @return int
179
     */
180
    public function getGrayImageDepth()
181
    {
182
        $value = $this->getArgumentValue('-dGrayImageDepth');
183
        if (null === $value) {
184
            return -1;
185
        }
186
187
        return intval($value);
188
    }
189
190
    /**
191
     * Set gray image depth
192
     *
193
     * @param int $grayImageDepth
194
     *
195
     * @return $this
196
     */
197
    public function setGrayImageDepth($grayImageDepth)
198
    {
199
        $this->setArgument(sprintf('-dGrayImageDepth=%s', $grayImageDepth));
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get gray image downsample threshold
206
     *
207
     * @return float
208
     */
209
    public function getGrayImageDownsampleThreshold()
210
    {
211
        $value = $this->getArgumentValue('-dGrayImageDownsampleThreshold');
212
        if (null === $value) {
213
            return 1.5;
214
        }
215
216
        return floatval($value);
217
    }
218
219
    /**
220
     * Set gray image downsample threshold
221
     *
222
     * @param float $grayImageDownsampleThreshold
223
     *
224
     * @return $this
225
     */
226
    public function setGrayImageDownsampleThreshold($grayImageDownsampleThreshold)
227
    {
228
        $this->setArgument(sprintf('-dGrayImageDownsampleThreshold=%s', $grayImageDownsampleThreshold));
229
230
        return $this;
231
    }
232
233
    /**
234
     * Get gray image downsample type
235
     *
236
     * @return string
237
     */
238
    public function getGrayImageDownsampleType()
239
    {
240
        $value = $this->getArgumentValue('-dGrayImageDownsampleType');
241
        if (null === $value) {
242
            return DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_SUBSAMPLE;
243
        }
244
245
        return substr($value, 1);
246
    }
247
248
    /**
249
     * Set gray image downsample type
250
     *
251
     * @param string $grayImageDownsampleType
252
     *
253
     * @throws \InvalidArgumentException
254
     *
255
     * @return $this
256
     */
257
    public function setGrayImageDownsampleType($grayImageDownsampleType)
258
    {
259
        if (!in_array($grayImageDownsampleType, static::$grayImageDownsampleTypeValues)) {
260
            throw new \InvalidArgumentException('Invalid grayscale image downsample type argument');
261
        }
262
263
        $this->setArgument(sprintf('-dGrayImageDownsampleType=/%s', $grayImageDownsampleType));
264
265
        return $this;
266
    }
267
268
    /**
269
     * Get gray image filter
270
     *
271
     * @return string
272
     */
273
    public function getGrayImageFilter()
274
    {
275
        $value = $this->getArgumentValue('-dGrayImageFilter');
276
        if (null === $value) {
277
            return DistillerParametersInterface::IMAGE_FILTER_DCT_ENCODE;
278
        }
279
280
        return substr($value, 1);
281
    }
282
283
    /**
284
     * Set gray image filter
285
     *
286
     * @param string $grayImageFilter
287
     *
288
     * @throws \InvalidArgumentException
289
     *
290
     * @return $this
291
     */
292
    public function setGrayImageFilter($grayImageFilter)
293
    {
294
        if (!in_array($grayImageFilter, static::$grayImageDownsampleTypeValues)) {
295
            throw new \InvalidArgumentException('Invalid grayscale image filter argument');
296
        }
297
298
        $this->setArgument(sprintf('-dGrayImageFilter=/%s', $grayImageFilter));
299
300
        return $this;
301
    }
302
303
    /**
304
     * Get gray image resolution
305
     *
306
     * @return int
307
     */
308
    public function getGrayImageResolution()
309
    {
310
        $value = $this->getArgumentValue('-dGrayImageResolution');
311
        if (null === $value) {
312
            return 72;
313
        }
314
315
        return intval($value);
316
    }
317
318
    /**
319
     * Set gray image resolution
320
     *
321
     * @param int $grayImageResolution
322
     *
323
     * @return $this
324
     */
325
    public function setGrayImageResolution($grayImageResolution)
326
    {
327
        $this->setArgument(sprintf('-dGrayImageResolution=%s', $grayImageResolution));
328
329
        return $this;
330
    }
331
}
332