Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.85% covered (success)
97.85%
91 / 93
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Gd
97.85% covered (success)
97.85%
91 / 93
90.00% covered (success)
90.00%
9 / 10
51
0.00% covered (danger)
0.00%
0 / 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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 rectangle
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
 square
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
7
 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%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 chord
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 pie
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 polygon
90.00% covered (success)
90.00%
18 / 20
0.00% covered (danger)
0.00%
0 / 1
11.12
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-2023 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\Draw;
15
16/**
17 * Draw class for Gd
18 *
19 * @category   Pop
20 * @package    Pop\Image
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.4.0
25 */
26class Gd extends AbstractDraw
27{
28
29    /**
30     * Opacity
31     * @var int
32     */
33    protected $opacity = 0;
34
35    /**
36     * Set the opacity
37     *
38     * @param  int $opacity
39     * @return Gd
40     */
41    public function setOpacity($opacity)
42    {
43        $this->opacity = (int)round((127 - (127 * ($opacity / 100))));
44        return $this;
45    }
46
47    /**
48     * Draw a line on the image.
49     *
50     * @param  int $x1
51     * @param  int $y1
52     * @param  int $x2
53     * @param  int $y2
54     * @return Gd
55     */
56    public function line($x1, $y1, $x2, $y2)
57    {
58        if ($this->hasImage()) {
59            $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
60                $this->image->createColor($this->strokeColor, $this->opacity);
61
62            // Draw the line.
63            imagesetthickness($this->image->getResource(), (($this->strokeWidth == 0) ? 1 : $this->strokeWidth));
64            imageline($this->image->getResource(), $x1, $y1, $x2, $y2, $strokeColor);
65        }
66        return $this;
67    }
68
69    /**
70     * Draw a rectangle on the image.
71     *
72     * @param  int $x
73     * @param  int $y
74     * @param  int $w
75     * @param  int $h
76     * @return Gd
77     */
78    public function rectangle($x, $y, $w, $h = null)
79    {
80        if ($this->hasImage()) {
81            $x2 = $x + $w;
82            $y2 = $y + ((null === $h) ? $w : $h);
83
84            if (null !== $this->fillColor) {
85                $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) :
86                    $this->image->createColor($this->fillColor, $this->opacity);
87
88                imagefilledrectangle($this->image->getResource(), $x, $y, $x2, $y2, $fillColor);
89            }
90
91            if ($this->strokeWidth > 0) {
92                $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
93                    $this->image->createColor($this->strokeColor, $this->opacity);
94                imagesetthickness($this->image->getResource(), $this->strokeWidth);
95                imagerectangle($this->image->getResource(), $x, $y, $x2, $y2, $strokeColor);
96            }
97        }
98
99        return $this;
100    }
101
102    /**
103     * Draw a square on the image.
104     *
105     * @param  int     $x
106     * @param  int     $y
107     * @param  int     $w
108     * @return Gd
109     */
110    public function square($x, $y, $w)
111    {
112        return $this->rectangle($x, $y, $w, $w);
113    }
114
115    /**
116     * Draw an ellipse on the image.
117     *
118     * @param  int $x
119     * @param  int $y
120     * @param  int $w
121     * @param  int $h
122     * @return Gd
123     */
124    public function ellipse($x, $y, $w, $h = null)
125    {
126        if ($this->hasImage()) {
127            $wid = $w;
128            $hgt = (null === $h) ? $w : $h;
129
130            if (null !== $this->fillColor) {
131                $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) :
132                    $this->image->createColor($this->fillColor, $this->opacity);
133                imagefilledellipse($this->image->getResource(), $x, $y, $wid, $hgt, $fillColor);
134            }
135
136            if ($this->strokeWidth > 0) {
137                $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
138                    $this->image->createColor($this->strokeColor, $this->opacity);
139
140                imageellipse($this->image->getResource(), $x, $y, ($wid + $this->strokeWidth), ($hgt + $this->strokeWidth), $strokeColor);
141            }
142        }
143
144        return $this;
145    }
146
147    /**
148     * Method to add a circle to the image.
149     *
150     * @param  int     $x
151     * @param  int     $y
152     * @param  int     $w
153     * @return Gd
154     */
155    public function circle($x, $y, $w)
156    {
157        return $this->ellipse($x, $y, $w, $w);
158    }
159
160    /**
161     * Draw an arc on the image.
162     *
163     * @param  int $x
164     * @param  int $y
165     * @param  int $start
166     * @param  int $end
167     * @param  int $w
168     * @param  int $h
169     * @return Gd
170     */
171    public function arc($x, $y, $start, $end, $w, $h = null)
172    {
173        if ($this->hasImage()) {
174            if ($this->strokeWidth == 0) {
175                $this->setStrokeWidth(1);
176            }
177
178            $wid = $w;
179            $hgt = (null === $h) ? $w : $h;
180
181            $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
182                $this->image->createColor($this->strokeColor, $this->opacity);
183
184            imagesetthickness($this->image->getResource(), $this->strokeWidth);
185            imagearc($this->image->getResource(), $x, $y, $wid, $hgt, $start, $end, $strokeColor);
186        }
187
188        return $this;
189    }
190
191    /**
192     * Draw a chord on the image.
193     *
194     * @param  int $x
195     * @param  int $y
196     * @param  int $start
197     * @param  int $end
198     * @param  int $w
199     * @param  int $h
200     * @return Gd
201     */
202    public function chord($x, $y, $start, $end, $w, $h = null)
203    {
204        if ($this->hasImage()) {
205            $wid = $w;
206            $hgt = (null === $h) ? $w : $h;
207
208            if (null !== $this->fillColor) {
209                $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) :
210                    $this->image->createColor($this->fillColor, $this->opacity);
211                imagefilledarc($this->image->getResource(), $x, $y, $wid, $hgt, $start, $end, $fillColor, IMG_ARC_CHORD);
212            }
213
214            if ($this->strokeWidth > 0) {
215                $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
216                    $this->image->createColor($this->strokeColor, $this->opacity);
217
218                imagesetthickness($this->image->getResource(), $this->strokeWidth);
219                imagefilledarc(
220                    $this->image->getResource(), $x, $y, $wid, $hgt, $start, $end, $strokeColor, IMG_ARC_EDGED | IMG_ARC_CHORD | IMG_ARC_NOFILL
221                );
222            }
223        }
224
225        return $this;
226    }
227
228    /**
229     * Draw a slice on the image.
230     *
231     * @param  int $x
232     * @param  int $y
233     * @param  int $start
234     * @param  int $end
235     * @param  int $w
236     * @param  int $h
237     * @return Gd
238     */
239    public function pie($x, $y, $start, $end, $w, $h = null)
240    {
241        if ($this->hasImage()) {
242            $wid = $w;
243            $hgt = (null === $h) ? $w : $h;
244
245            if (null !== $this->fillColor) {
246                $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) :
247                    $this->image->createColor($this->fillColor, $this->opacity);
248                imagefilledarc($this->image->getResource(), $x, $y, $wid, $hgt, $start, $end, $fillColor, IMG_ARC_PIE);
249            }
250
251            if ($this->strokeWidth > 0) {
252                $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
253                    $this->image->createColor($this->strokeColor, $this->opacity);
254
255                imagesetthickness($this->image->getResource(), $this->strokeWidth);
256                imagefilledarc(
257                    $this->image->getResource(), $x, $y, $wid, $hgt, $start, $end, $strokeColor, IMG_ARC_EDGED | IMG_ARC_PIE | IMG_ARC_NOFILL
258                );
259            }
260        }
261
262        return $this;
263    }
264
265    /**
266     * Draw a polygon on the image.
267     *
268     * @param  array   $points
269     * @return Gd
270     */
271    public function polygon($points)
272    {
273        if ($this->hasImage()) {
274            $realPoints = [];
275            foreach ($points as $coord) {
276                if (isset($coord['x']) && isset($coord['y'])) {
277                    $realPoints[] = $coord['x'];
278                    $realPoints[] = $coord['y'];
279                }
280            }
281
282            if (null !== $this->fillColor) {
283                $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) :
284                    $this->image->createColor($this->fillColor, $this->opacity);
285                if (strpos(PHP_VERSION, '8.1') !== false) {
286                    imagefilledpolygon($this->image->getResource(), $realPoints, $fillColor);
287                } else {
288                    imagefilledpolygon($this->image->getResource(), $realPoints, count($points), $fillColor);
289                }
290
291            }
292
293            if ($this->strokeWidth > 0) {
294                $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) :
295                    $this->image->createColor($this->strokeColor, $this->opacity);
296
297                imagesetthickness($this->image->getResource(), $this->strokeWidth);
298                if (strpos(PHP_VERSION, '8.1') !== false) {
299                    imagepolygon($this->image->getResource(), $realPoints, $strokeColor);
300                } else {
301                    imagepolygon($this->image->getResource(), $realPoints, count($points), $strokeColor);
302                }
303
304            }
305        }
306
307        return $this;
308    }
309
310}