Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       91 / 91  | 
               | 
       100.00%  | 
       10 / 10  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| Gd |         | 
       100.00%  | 
       91 / 91  | 
               | 
       100.00%  | 
       10 / 10  | 
       49 |         | 
       100.00%  | 
       1 / 1  | 
      
| setOpacity |         | 
       100.00%  | 
       2 / 2  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| line |         | 
       100.00%  | 
       6 / 6  | 
               | 
       100.00%  | 
       1 / 1  | 
       4 | |||
| rectangle |         | 
       100.00%  | 
       13 / 13  | 
               | 
       100.00%  | 
       1 / 1  | 
       7 | |||
| square |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| ellipse |         | 
       100.00%  | 
       12 / 12  | 
               | 
       100.00%  | 
       1 / 1  | 
       7 | |||
| circle |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| arc |         | 
       100.00%  | 
       10 / 10  | 
               | 
       100.00%  | 
       1 / 1  | 
       5 | |||
| chord |         | 
       100.00%  | 
       15 / 15  | 
               | 
       100.00%  | 
       1 / 1  | 
       7 | |||
| pie |         | 
       100.00%  | 
       15 / 15  | 
               | 
       100.00%  | 
       1 / 1  | 
       7 | |||
| polygon |         | 
       100.00%  | 
       16 / 16  | 
               | 
       100.00%  | 
       1 / 1  | 
       9 | |||
| 1 | <?php | 
| 2 | /** | 
| 3 | * Pop PHP Framework (https://www.popphp.org/) | 
| 4 | * | 
| 5 | * @link https://github.com/popphp/popphp-framework | 
| 6 | * @author Nick Sagona, III <dev@noladev.com> | 
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. | 
| 8 | * @license https://www.popphp.org/license New BSD License | 
| 9 | */ | 
| 10 | |
| 11 | /** | 
| 12 | * @namespace | 
| 13 | */ | 
| 14 | namespace Pop\Image\Draw; | 
| 15 | |
| 16 | /** | 
| 17 | * Draw class for Gd | 
| 18 | * | 
| 19 | * @category Pop | 
| 20 | * @package Pop\Image | 
| 21 | * @author Nick Sagona, III <dev@noladev.com> | 
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. | 
| 23 | * @license https://www.popphp.org/license New BSD License | 
| 24 | * @version 4.1.1 | 
| 25 | */ | 
| 26 | class Gd extends AbstractDraw | 
| 27 | { | 
| 28 | |
| 29 | /** | 
| 30 | * Opacity | 
| 31 | * @var int | 
| 32 | */ | 
| 33 | protected int|float|null $opacity = 0; | 
| 34 | |
| 35 | /** | 
| 36 | * Set the opacity | 
| 37 | * | 
| 38 | * @param int|float $opacity | 
| 39 | * @return Gd | 
| 40 | */ | 
| 41 | public function setOpacity(int|float $opacity): Gd | 
| 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|float $x1 | 
| 51 | * @param int|float $y1 | 
| 52 | * @param int|float $x2 | 
| 53 | * @param int|float $y2 | 
| 54 | * @return Gd | 
| 55 | */ | 
| 56 | public function line(int|float $x1, int|float $y1, int|float $x2, int|float $y2): Gd | 
| 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|float $x | 
| 73 | * @param int|float $y | 
| 74 | * @param int|float $w | 
| 75 | * @param int|float|null $h | 
| 76 | * @return Gd | 
| 77 | */ | 
| 78 | public function rectangle(int|float $x, int|float $y, int|float $w, int|float|null $h = null): Gd | 
| 79 | { | 
| 80 | if ($this->hasImage()) { | 
| 81 | $x2 = $x + $w; | 
| 82 | $y2 = $y + (($h === null) ? $w : $h); | 
| 83 | |
| 84 | if ($this->fillColor !== null) { | 
| 85 | $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) : | 
| 86 | $this->image->createColor($this->fillColor, $this->opacity); | 
| 87 | |
| 88 | imagefilledrectangle($this->image->getResource(), (int)$x, (int)$y, (int)$x2, (int)$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|float $x | 
| 106 | * @param int|float $y | 
| 107 | * @param int|float $w | 
| 108 | * @return Gd | 
| 109 | */ | 
| 110 | public function square(int|float $x, int|float $y, int|float $w): Gd | 
| 111 | { | 
| 112 | return $this->rectangle($x, $y, $w, $w); | 
| 113 | } | 
| 114 | |
| 115 | /** | 
| 116 | * Draw an ellipse on the image. | 
| 117 | * | 
| 118 | * @param int|float $x | 
| 119 | * @param int|float $y | 
| 120 | * @param int|float $w | 
| 121 | * @param int|float|null $h | 
| 122 | * @return Gd | 
| 123 | */ | 
| 124 | public function ellipse(int|float $x, int|float $y, int|float $w, int|float|null $h = null): Gd | 
| 125 | { | 
| 126 | if ($this->hasImage()) { | 
| 127 | $wid = $w; | 
| 128 | $hgt = ($h === null) ? $w : $h; | 
| 129 | |
| 130 | if ($this->fillColor !== null) { | 
| 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|float $x | 
| 151 | * @param int|float $y | 
| 152 | * @param int|float $w | 
| 153 | * @return Gd | 
| 154 | */ | 
| 155 | public function circle(int|float $x, int|float $y, int|float $w): Gd | 
| 156 | { | 
| 157 | return $this->ellipse($x, $y, $w, $w); | 
| 158 | } | 
| 159 | |
| 160 | /** | 
| 161 | * Draw an arc on the image. | 
| 162 | * | 
| 163 | * @param int|float $x | 
| 164 | * @param int|float $y | 
| 165 | * @param int|float $start | 
| 166 | * @param int|float $end | 
| 167 | * @param int|float $w | 
| 168 | * @param int|float|null $h | 
| 169 | * @return Gd | 
| 170 | */ | 
| 171 | public function arc(int|float $x, int|float $y, int|float $start, int|float $end, int|float $w, int|float|null $h = null): Gd | 
| 172 | { | 
| 173 | if ($this->hasImage()) { | 
| 174 | if ($this->strokeWidth == 0) { | 
| 175 | $this->setStrokeWidth(1); | 
| 176 | } | 
| 177 | |
| 178 | $wid = $w; | 
| 179 | $hgt = ($h === null) ? $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|float $x | 
| 195 | * @param int|float $y | 
| 196 | * @param int|float $start | 
| 197 | * @param int|float $end | 
| 198 | * @param int|float $w | 
| 199 | * @param int|float|null $h | 
| 200 | * @return Gd | 
| 201 | */ | 
| 202 | public function chord(int|float $x, int|float $y, int|float $start, int|float $end, int|float $w, int|float|null $h = null): Gd | 
| 203 | { | 
| 204 | if ($this->hasImage()) { | 
| 205 | $wid = $w; | 
| 206 | $hgt = ($h === null) ? $w : $h; | 
| 207 | |
| 208 | if ($this->fillColor !== null) { | 
| 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|float $x | 
| 232 | * @param int|float $y | 
| 233 | * @param int|float $start | 
| 234 | * @param int|float $end | 
| 235 | * @param int|float $w | 
| 236 | * @param int|float|null $h | 
| 237 | * @return Gd | 
| 238 | */ | 
| 239 | public function pie(int|float $x, int|float $y, int|float $start, int|float $end, int|float $w, int|float|null $h = null): Gd | 
| 240 | { | 
| 241 | if ($this->hasImage()) { | 
| 242 | $wid = $w; | 
| 243 | $hgt = ($h === null) ? $w : $h; | 
| 244 | |
| 245 | if ($this->fillColor !== null) { | 
| 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(array $points): Gd | 
| 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 ($this->fillColor !== null) { | 
| 283 | $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor) : | 
| 284 | $this->image->createColor($this->fillColor, $this->opacity); | 
| 285 | imagefilledpolygon($this->image->getResource(), $realPoints, $fillColor); | 
| 286 | } | 
| 287 | |
| 288 | if ($this->strokeWidth > 0) { | 
| 289 | $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor) : | 
| 290 | $this->image->createColor($this->strokeColor, $this->opacity); | 
| 291 | |
| 292 | imagesetthickness($this->image->getResource(), $this->strokeWidth); | 
| 293 | imagepolygon($this->image->getResource(), $realPoints, $strokeColor); | 
| 294 | } | 
| 295 | } | 
| 296 | |
| 297 | return $this; | 
| 298 | } | 
| 299 | |
| 300 | } |