Code Duplication    Length = 325-325 lines in 2 locations

src/Device/DistillerParameters/ColorImageCompressionTrait.php 1 location

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

src/Device/DistillerParameters/GrayscaleImageCompressionTrait.php 1 location

@@ 19-343 (lines=325) @@
16
 *
17
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
18
 */
19
trait GrayscaleImageCompressionTrait
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