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