Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.15% covered (success)
98.15%
53 / 54
93.33% covered (success)
93.33%
14 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Imagick
98.15% covered (success)
98.15%
53 / 54
93.33% covered (success)
93.33%
14 / 15
34
0.00% covered (danger)
0.00%
0 / 1
 blur
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 adaptiveBlur
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 gaussianBlur
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 motionBlur
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 sharpen
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 negate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 paint
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 posterize
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 noise
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 diffuse
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 skew
88.89% covered (success)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
5.03
 swirl
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 wave
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 pixelate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 pencil
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Image\Filter;
16
17use Pop\Color\Color;
18
19/**
20 * Filter class for Imagick
21 *
22 * @category   Pop
23 * @package    Pop\Image
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29class Imagick extends AbstractFilter
30{
31
32    /**
33     * Blur the image.
34     *
35     * @param  mixed $radius
36     * @param  mixed $sigma
37     * @param  int   $channel
38     * @return Imagick
39     */
40    public function blur(mixed $radius = 0, mixed $sigma = 0, int $channel = \Imagick::CHANNEL_ALL): Imagick
41    {
42        if ($this->hasImage()) {
43            $this->image->getResource()->blurImage($radius, $sigma, $channel);
44        }
45        return $this;
46    }
47
48    /**
49     * Blur the image.
50     *
51     * @param  mixed $radius
52     * @param  mixed $sigma
53     * @param  int   $channel
54     * @return Imagick
55     */
56    public function adaptiveBlur(mixed $radius = 0, mixed $sigma = 0, int $channel = \Imagick::CHANNEL_DEFAULT): Imagick
57    {
58        if ($this->hasImage()) {
59            $this->image->getResource()->adaptiveBlurImage($radius, $sigma, $channel);
60        }
61        return $this;
62    }
63
64    /**
65     * Blur the image.
66     *
67     * @param  mixed $radius
68     * @param  mixed $sigma
69     * @param  int   $channel
70     * @return Imagick
71     */
72    public function gaussianBlur(mixed $radius = 0, mixed $sigma = 0, int $channel = \Imagick::CHANNEL_ALL): Imagick
73    {
74        if ($this->hasImage()) {
75            $this->image->getResource()->gaussianBlurImage($radius, $sigma, $channel);
76        }
77        return $this;
78    }
79
80    /**
81     * Blur the image.
82     *
83     * @param  mixed $radius
84     * @param  mixed $sigma
85     * @param  int   $angle
86     * @param  int   $channel
87     * @return Imagick
88     */
89    public function motionBlur(mixed $radius = 0, mixed $sigma = 0, int $angle = 0, int $channel = \Imagick::CHANNEL_DEFAULT): Imagick
90    {
91        if ($this->hasImage()) {
92            $this->image->getResource()->motionBlurImage($radius, $sigma, $angle, $channel);
93        }
94        return $this;
95    }
96
97    /**
98     * Sharpen the image
99     *
100     * @param  mixed $radius
101     * @param  mixed $sigma
102     * @param  int   $channel
103     * @return Imagick
104     */
105    public function sharpen(mixed $radius = 0, mixed $sigma = 0, int $channel = \Imagick::CHANNEL_ALL): Imagick
106    {
107        if ($this->hasImage()) {
108            $this->image->getResource()->sharpenImage($radius, $sigma, $channel);
109        }
110        return $this;
111    }
112
113    /**
114     * Create a negative of the image
115     *
116     * @return Imagick
117     */
118    public function negate(): Imagick
119    {
120        if ($this->hasImage()) {
121            $this->image->getResource()->negateImage(false);
122        }
123        return $this;
124    }
125
126    /**
127     * Apply an oil paint effect to the image using the pixel radius threshold
128     *
129     * @param  int $radius
130     * @return Imagick
131     */
132    public function paint(int $radius): Imagick
133    {
134        if ($this->hasImage()) {
135            $this->image->getResource()->oilPaintImage($radius);
136        }
137        return $this;
138    }
139
140    /**
141     * Apply a posterize effect to the image
142     *
143     * @param  int  $levels
144     * @param  bool $dither
145     * @return Imagick
146     */
147    public function posterize(int $levels, bool $dither = false): Imagick
148    {
149        if ($this->hasImage()) {
150            $this->image->getResource()->posterizeImage($levels, $dither);
151        }
152        return $this;
153    }
154
155    /**
156     * Apply a noise effect to the image
157     *
158     * @param  int $type
159     * @param  int $channel
160     * @return Imagick
161     */
162    public function noise(int $type = \Imagick::NOISE_MULTIPLICATIVEGAUSSIAN, int $channel = \Imagick::CHANNEL_DEFAULT): Imagick
163    {
164        if ($this->hasImage()) {
165            $this->image->getResource()->addNoiseImage($type, $channel);
166        }
167        return $this;
168    }
169
170    /**
171     * Apply a diffusion effect to the image
172     *
173     * @param  int $radius
174     * @return Imagick
175     */
176    public function diffuse(int $radius): Imagick
177    {
178        if ($this->hasImage()) {
179            $this->image->getResource()->spreadImage($radius);
180        }
181        return $this;
182    }
183
184    /**
185     * Apply a skew effect to the image
186     *
187     * @param  int                   $x
188     * @param  int                   $y
189     * @param  ?Color\ColorInterface $color
190     * @throws Exception
191     * @return Imagick
192     */
193    public function skew(int $x, int $y, ?Color\ColorInterface $color = null): Imagick
194    {
195        if ($this->hasImage()) {
196            if ($color === null) {
197                $color = new Color\Rgb(255, 255, 255);
198            }
199            if (!($color instanceof Color\Rgb)) {
200                if (!method_exists($color, 'toRgb')) {
201                    throw new Exception('Error: The color object does not support conversion to RGB.');
202                }
203                $color = $color->toRgb();
204            }
205            $this->image->getResource()->shearImage($color->render(Color\Rgb::CSS), $x, $y);
206        }
207
208        return $this;
209    }
210
211    /**
212     * Apply a swirl effect to the image
213     *
214     * @param  int $degrees
215     * @return Imagick
216     */
217    public function swirl(int $degrees): Imagick
218    {
219        if ($this->hasImage()) {
220            $this->image->getResource()->swirlImage($degrees);
221        }
222        return $this;
223    }
224
225    /**
226     * Apply a wave effect to the image
227     *
228     * @param  mixed $amp
229     * @param  mixed $length
230     * @return Imagick
231     */
232    public function wave(mixed $amp, mixed $length): Imagick
233    {
234        if ($this->hasImage()) {
235            $this->image->getResource()->waveImage($amp, $length);
236        }
237        return $this;
238    }
239
240    /**
241     * Apply a mosaic pixelate effect to the image
242     *
243     * @param  int  $w
244     * @param  ?int $h
245     * @return Imagick
246     */
247    public function pixelate(int $w, ?int $h = null): Imagick
248    {
249        if ($this->hasImage()) {
250            $x = $this->image->getWidth() / $w;
251            $y = $this->image->getHeight() / (($h === null) ? $w : $h);
252
253            $this->image->getResource()->scaleImage($x, $y);
254            $this->image->getResource()->scaleImage($this->image->getWidth(), $this->image->getHeight());
255        }
256
257        return $this;
258    }
259
260    /**
261     * Apply a pencil/sketch effect to the image
262     *
263     * @param  mixed $radius
264     * @param  mixed $sigma
265     * @param  mixed $angle
266     * @return Imagick
267     */
268    public function pencil(mixed $radius, mixed $sigma, mixed $angle): Imagick
269    {
270        if ($this->hasImage()) {
271            $this->image->getResource()->sketchImage($radius, $sigma, $angle);
272        }
273        return $this;
274    }
275
276}