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