Completed
Push — master ( d7633b...7db5b7 )
by Daniel
02:36
created

setEncodeGrayImages()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

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