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

getMonoImageResolution()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
ccs 0
cts 11
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 0
crap 20
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\ImageDownsampleType;
11
use GravityMedia\Ghostscript\Enum\MonoImageFilter;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The monochrome image compression distiller parameters trait
16
 *
17
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
18
 */
19
trait MonochromeImageCompressionTrait
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 monochrome images
48
     *
49
     * @return bool
50
     */
51
    public function isAntiAliasMonoImages()
52
    {
53
        $value = $this->getArgumentValue('-dAntiAliasMonoImages');
54
        if (null === $value) {
55
            return false;
56
        }
57
58
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
59
    }
60
61
    /**
62
     * Set anti alias monochrome images flag
63
     *
64
     * @param bool $antiAliasMonoImages
65
     *
66
     * @return $this
67
     */
68
    public function setAntiAliasMonoImages($antiAliasMonoImages)
69
    {
70
        $this->setArgument(sprintf('-dAntiAliasMonoImages=%s', $antiAliasMonoImages ? 'true' : 'false'));
71
72
        return $this;
73
    }
74
75
    /**
76
     * Whether to downsample monochrome images
77
     *
78
     * @return bool
79
     */
80
    public function isDownsampleMonoImages()
81
    {
82
        $value = $this->getArgumentValue('-dDownsampleMonoImages');
83
        if (null === $value) {
84
            switch ($this->getPdfSettings()) {
85
                case PdfSettings::SCREEN:
86
                case PdfSettings::EBOOK:
87
                    return true;
88
                default:
89
                    return false;
90
            }
91
        }
92
93
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
94
    }
95
96
    /**
97
     * Set downsample monochrome images flag
98
     *
99
     * @param bool $downsampleMonoImages
100
     *
101
     * @return $this
102
     */
103
    public function setDownsampleMonoImages($downsampleMonoImages)
104
    {
105
        $this->setArgument(sprintf('-dDownsampleMonoImages=%s', $downsampleMonoImages ? 'true' : 'false'));
106
107
        return $this;
108
    }
109
110
    /**
111
     * Whether to encode monochrome images
112
     *
113
     * @return bool
114
     */
115
    public function isEncodeMonoImages()
116
    {
117
        $value = $this->getArgumentValue('-dEncodeMonoImages');
118
        if (null === $value) {
119
            return true;
120
        }
121
122
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
123
    }
124
125
    /**
126
     * Set encode monochrome images flag
127
     *
128
     * @param bool $encodeMonoImages
129
     *
130
     * @return $this
131
     */
132
    public function setEncodeMonoImages($encodeMonoImages)
133
    {
134
        $this->setArgument(sprintf('-dEncodeMonoImages=%s', $encodeMonoImages ? 'true' : 'false'));
135
136
        return $this;
137
    }
138
139
    /**
140
     * Get monochrome image depth
141
     *
142
     * @return int
143
     */
144
    public function getMonoImageDepth()
145
    {
146
        $value = $this->getArgumentValue('-dMonoImageDepth');
147
        if (null === $value) {
148
            return -1;
149
        }
150
151
        return intval($value);
152
    }
153
154
    /**
155
     * Set monochrome image depth
156
     *
157
     * @param int $monoImageDepth
158
     *
159
     * @return $this
160
     */
161
    public function setMonoImageDepth($monoImageDepth)
162
    {
163
        $this->setArgument(sprintf('-dMonoImageDepth=%s', $monoImageDepth));
164
165
        return $this;
166
    }
167
168
    /**
169
     * Get monochrome image downsample threshold
170
     *
171
     * @return float
172
     */
173
    public function getMonoImageDownsampleThreshold()
174
    {
175
        $value = $this->getArgumentValue('-dMonoImageDownsampleThreshold');
176
        if (null === $value) {
177
            return 1.5;
178
        }
179
180
        return floatval($value);
181
    }
182
183
    /**
184
     * Set monochrome image downsample threshold
185
     *
186
     * @param float $monoImageDownsampleThreshold
187
     *
188
     * @return $this
189
     */
190
    public function setMonoImageDownsampleThreshold($monoImageDownsampleThreshold)
191
    {
192
        $this->setArgument(sprintf('-dMonoImageDownsampleThreshold=%s', $monoImageDownsampleThreshold));
193
194
        return $this;
195
    }
196
197
    /**
198
     * Get monochrome image downsample type
199
     *
200
     * @return string
201
     */
202 View Code Duplication
    public function getMonoImageDownsampleType()
0 ignored issues
show
Duplication introduced by
This method 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...
203
    {
204
        $value = $this->getArgumentValue('-dMonoImageDownsampleType');
205
        if (null === $value) {
206
            switch ($this->getPdfSettings()) {
207
                case PdfSettings::PREPRESS:
208
                    return ImageDownsampleType::BICUBIC;
209
                default:
210
                    return ImageDownsampleType::SUBSAMPLE;
211
            }
212
        }
213
214
        return substr($value, 1);
215
    }
216
217
    /**
218
     * Set monochrome image downsample type
219
     *
220
     * @param string $monoImageDownsampleType
221
     *
222
     * @throws \InvalidArgumentException
223
     *
224
     * @return $this
225
     */
226
    public function setMonoImageDownsampleType($monoImageDownsampleType)
227
    {
228
        $monoImageDownsampleType = ltrim($monoImageDownsampleType, '/');
229
        if (!in_array($monoImageDownsampleType, ImageDownsampleType::values())) {
230
            throw new \InvalidArgumentException('Invalid monochrome image downsample type argument');
231
        }
232
233
        $this->setArgument(sprintf('-dMonoImageDownsampleType=/%s', $monoImageDownsampleType));
234
235
        return $this;
236
    }
237
238
    /**
239
     * Get monochrome image filter
240
     *
241
     * @return string
242
     */
243
    public function getMonoImageFilter()
244
    {
245
        $value = $this->getArgumentValue('-dMonoImageFilter');
246
        if (null === $value) {
247
            return MonoImageFilter::CCITT_FAX_ENCODE;
248
        }
249
250
        return substr($value, 1);
251
    }
252
253
    /**
254
     * Set monochrome image filter
255
     *
256
     * @param string $monoImageFilter
257
     *
258
     * @throws \InvalidArgumentException
259
     *
260
     * @return $this
261
     */
262
    public function setMonoImageFilter($monoImageFilter)
263
    {
264
        $monoImageFilter = ltrim($monoImageFilter, '/');
265
        if (!in_array($monoImageFilter, MonoImageFilter::values())) {
266
            throw new \InvalidArgumentException('Invalid monochrome image filter argument');
267
        }
268
269
        $this->setArgument(sprintf('-dMonoImageFilter=/%s', $monoImageFilter));
270
271
        return $this;
272
    }
273
274
    /**
275
     * Get monochrome image resolution
276
     *
277
     * @return int
278
     */
279 View Code Duplication
    public function getMonoImageResolution()
0 ignored issues
show
Duplication introduced by
This method 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...
280
    {
281
        $value = $this->getArgumentValue('-dMonoImageResolution');
282
        if (null === $value) {
283
            switch ($this->getPdfSettings()) {
284
                case PdfSettings::PRINTER:
285
                case PdfSettings::PREPRESS:
286
                    return 1200;
287
                default:
288
                    return 300;
289
            }
290
        }
291
292
        return intval($value);
293
    }
294
295
    /**
296
     * Set monochrome image resolution
297
     *
298
     * @param int $monoImageResolution
299
     *
300
     * @return $this
301
     */
302
    public function setMonoImageResolution($monoImageResolution)
303
    {
304
        $this->setArgument(sprintf('-dMonoImageResolution=%s', $monoImageResolution));
305
306
        return $this;
307
    }
308
}
309