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