Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
119 / 119
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
Imagick
100.00% covered (success)
100.00%
119 / 119
100.00% covered (success)
100.00%
12 / 12
38
100.00% covered (success)
100.00%
1 / 1
 setOpacity
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 line
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 rectangle
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 square
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 roundedRectangle
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 roundedSquare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ellipse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 circle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 arc
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 chord
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
 pie
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
4
 polygon
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
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\Draw;
16
17use ImagickDrawException;
18
19/**
20 * Draw 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 AbstractDraw
30{
31
32    /**
33     * Opacity
34     * @var int|float|null
35     */
36    protected int|float|null $opacity = 100;
37
38    /**
39     * Set the opacity
40     *
41     * @param  int|float $opacity
42     * @return Imagick
43     */
44    public function setOpacity(int|float $opacity): Imagick
45    {
46        $this->opacity = $opacity;
47        return $this;
48    }
49
50    /**
51     * Draw a line on the image.
52     *
53     * @param  int $x1
54     * @param  int $y1
55     * @param  int $x2
56     * @param  int $y2
57     * @throws ImagickDrawException
58     * @return Imagick
59     */
60    public function line(int $x1, int $y1, int $x2, int $y2): Imagick
61    {
62        if ($this->hasImage()) {
63            $draw = new \ImagickDraw();
64            $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
65            $draw->setStrokeWidth($this->strokeWidth);
66            $draw->line($x1, $y1, $x2, $y2);
67            $this->image->getResource()->drawImage($draw);
68        }
69
70        return $this;
71    }
72
73    /**
74     * Draw a rectangle on the image.
75     *
76     * @param  int  $x
77     * @param  int  $y
78     * @param  int  $w
79     * @param  ?int $h
80     * @throws ImagickDrawException
81     * @return Imagick
82     */
83    public function rectangle(int $x, int $y, int $w, ?int $h = null): Imagick
84    {
85        if ($this->hasImage()) {
86            $x2 = $x + $w;
87            $y2 = $y + (($h === null) ? $w : $h);
88
89            $draw = new \ImagickDraw();
90
91            if ($this->fillColor !== null) {
92                $draw->setFillColor($this->image->createColor($this->fillColor, (int)$this->opacity));
93            }
94
95            if ($this->strokeWidth > 0) {
96                $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
97                $draw->setStrokeWidth($this->strokeWidth);
98            }
99
100            $draw->rectangle($x, $y, $x2, $y2);
101            $this->image->getResource()->drawImage($draw);
102        }
103
104        return $this;
105    }
106
107    /**
108     * Draw a square on the image.
109     *
110     * @param  int $x
111     * @param  int $y
112     * @param  int $w
113     * @throws ImagickDrawException
114     * @return Imagick
115     */
116    public function square(int $x, int $y, int $w): Imagick
117    {
118        return $this->rectangle($x, $y, $w, $w);
119    }
120
121    /**
122     * Draw a rounded rectangle on the image.
123     *
124     * @param  int  $x
125     * @param  int  $y
126     * @param  int  $w
127     * @param  ?int $h
128     * @param  ?int $rx
129     * @param  ?int $ry
130     * @throws ImagickDrawException
131     * @return Imagick
132     */
133    public function roundedRectangle(int $x, int $y, int $w, ?int $h = null, ?int $rx = 10, ?int $ry = null): Imagick
134    {
135        if ($this->hasImage()) {
136            $x2 = $x + $w;
137            $y2 = $y + (($h === null) ? $w : $h);
138            if ($ry === null) {
139                $ry = $rx;
140            }
141
142            $draw = new \ImagickDraw();
143
144            if ($this->fillColor !== null) {
145                $draw->setFillColor($this->image->createColor($this->fillColor, (int)$this->opacity));
146            }
147
148            if ($this->strokeWidth > 0) {
149                $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
150                $draw->setStrokeWidth($this->strokeWidth);
151            }
152
153            $draw->roundRectangle($x, $y, $x2, $y2, $rx, $ry);
154            $this->image->getResource()->drawImage($draw);
155        }
156
157        return $this;
158    }
159
160    /**
161     * Draw a rounded square on the image.
162     *
163     * @param  int  $x
164     * @param  int  $y
165     * @param  int  $w
166     * @param  int  $rx
167     * @param  ?int $ry
168     * @throws ImagickDrawException
169     * @return Imagick
170     */
171    public function roundedSquare(int $x, int $y, int $w, int $rx = 10, ?int $ry = null): Imagick
172    {
173        return $this->roundedRectangle($x, $y, $w, $w, $rx, $ry);
174    }
175
176    /**
177     * Draw an ellipse on the image.
178     *
179     * @param  int  $x
180     * @param  int  $y
181     * @param  int  $w
182     * @param  ?int $h
183     * @throws ImagickDrawException
184     * @return Imagick
185     */
186    public function ellipse(int $x, int $y, int $w, ?int $h = null): Imagick
187    {
188        if ($this->hasImage()) {
189            $wid = $w;
190            $hgt = ($h === null) ? $w : $h;
191
192            $draw = new \ImagickDraw();
193
194            if ($this->fillColor !== null) {
195                $draw->setFillColor($this->image->createColor($this->fillColor, (int)$this->opacity));
196            }
197
198            if ($this->strokeWidth > 0) {
199                $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
200                $draw->setStrokeWidth($this->strokeWidth);
201            }
202
203            $draw->ellipse($x, $y, $wid, $hgt, 0, 360);
204            $this->image->getResource()->drawImage($draw);
205        }
206
207        return $this;
208    }
209
210    /**
211     * Method to add a circle to the image.
212     *
213     * @param  int $x
214     * @param  int $y
215     * @param  int $w
216     * @throws ImagickDrawException
217     * @return Imagick
218     */
219    public function circle(int $x, int $y, int $w): Imagick
220    {
221        return $this->ellipse($x, $y, $w, $w);
222    }
223
224    /**
225     * Draw an arc on the image.
226     *
227     * @param  int  $x
228     * @param  int  $y
229     * @param  int  $start
230     * @param  int  $end
231     * @param  int  $w
232     * @param  ?int $h
233     * @throws ImagickDrawException
234     * @return Imagick
235     */
236    public function arc(int $x, int $y, int $start, int $end, int $w, ?int $h = null): Imagick
237    {
238        if ($this->hasImage()) {
239            if ($this->strokeWidth == 0) {
240                $this->setStrokeWidth(1);
241            }
242
243            $wid = $w;
244            $hgt = ($h === null) ? $w : $h;
245
246            $draw = new \ImagickDraw();
247            $draw->setFillOpacity(0);
248            $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
249            $draw->setStrokeWidth($this->strokeWidth);
250
251            $draw->ellipse($x, $y, $wid, $hgt, $start, $end);
252
253            $this->image->getResource()->drawImage($draw);
254        }
255
256        return $this;
257    }
258
259    /**
260     * Draw a chord on the image.
261     *
262     * @param  int  $x
263     * @param  int  $y
264     * @param  int  $start
265     * @param  int  $end
266     * @param  int  $w
267     * @param  ?int $h
268     * @throws ImagickDrawException
269     * @return Imagick
270     */
271    public function chord(int $x, int $y, int $start, int $end, int $w, ?int $h = null): Imagick
272    {
273        if ($this->hasImage()) {
274            $wid = $w;
275            $hgt = ($h === null) ? $w : $h;
276
277            $draw = new \ImagickDraw();
278            $draw->setFillColor($this->image->createColor($this->fillColor));
279
280            $x1 = $w * cos($start / 180 * pi());
281            $y1 = $h * sin($start / 180 * pi());
282            $x2 = $w * cos($end / 180 * pi());
283            $y2 = $h * sin($end / 180 * pi());
284
285            $draw->ellipse($x, $y, $wid, $hgt, $start, $end);
286            $this->image->getResource()->drawImage($draw);
287
288            if ($this->strokeWidth > 0) {
289                $draw = new \ImagickDraw();
290
291                $draw->setFillColor($this->image->createColor($this->fillColor));
292                $draw->setStrokeColor($this->image->createColor($this->strokeColor));
293                $draw->setStrokeWidth($this->strokeWidth);
294
295                $draw->ellipse($x, $y, $wid, $hgt, $start, $end);
296                $draw->line($x + $x1, $y + $y1, $x + $x2, $y + $y2);
297
298                $this->image->getResource()->drawImage($draw);
299            }
300        }
301
302        return $this;
303    }
304
305    /**
306     * Draw a pie slice on the image.
307     *
308     * @param  int  $x
309     * @param  int  $y
310     * @param  int  $start
311     * @param  int  $end
312     * @param  int  $w
313     * @param  ?int $h
314     * @throws ImagickDrawException
315     * @return Imagick
316     */
317    public function pie(int $x, int $y, int $start, int $end, int $w, ?int $h = null): Imagick
318    {
319        if ($this->hasImage()) {
320            $wid = $w;
321            $hgt = ($h === null) ? $w : $h;
322
323            $draw = new \ImagickDraw();
324            $draw->setFillColor($this->image->createColor($this->fillColor));
325
326            $x1 = $w * cos($start / 180 * pi());
327            $y1 = $h * sin($start / 180 * pi());
328            $x2 = $w * cos($end / 180 * pi());
329            $y2 = $h * sin($end / 180 * pi());
330
331            $points = [
332                ['x' => $x, 'y' => $y],
333                ['x' => $x + $x1, 'y' => $y + $y1],
334                ['x' => $x + $x2, 'y' => $y + $y2]
335            ];
336
337            $draw->polygon($points);
338
339            $draw->ellipse($x, $y, $wid, $hgt, $start, $end);
340            $this->image->getResource()->drawImage($draw);
341
342            if ($this->strokeWidth > 0) {
343                $draw = new \ImagickDraw();
344
345                $draw->setFillColor($this->image->createColor($this->fillColor));
346                $draw->setStrokeColor($this->image->createColor($this->strokeColor));
347                $draw->setStrokeWidth($this->strokeWidth);
348
349                $draw->ellipse($x, $y, $wid, $hgt, $start, $end);
350                $draw->line($x, $y, $x + $x1, $y + $y1);
351                $draw->line($x, $y, $x + $x2, $y + $y2);
352
353                $this->image->getResource()->drawImage($draw);
354            }
355        }
356
357        return $this;
358    }
359
360    /**
361     * Draw a polygon on the image.
362     *
363     * @param  array $points
364     * @throws ImagickDrawException
365     * @return Imagick
366     */
367    public function polygon(array $points): Imagick
368    {
369        if ($this->hasImage()) {
370            $draw = new \ImagickDraw();
371            if ($this->fillColor !== null) {
372                $draw->setFillColor($this->image->createColor($this->fillColor, (int)$this->opacity));
373            }
374
375            if ($this->strokeWidth > 0) {
376                $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
377                $draw->setStrokeWidth($this->strokeWidth);
378            }
379
380            $draw->polygon($points);
381            $this->image->getResource()->drawImage($draw);
382        }
383
384        return $this;
385    }
386
387}