Rectangle.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Grafika\Gd\DrawingObject;
  3. use Grafika\DrawingObject\Rectangle as Base;
  4. use Grafika\DrawingObjectInterface;
  5. use Grafika\Gd\Editor;
  6. /**
  7. * Class Rectangle
  8. * @package Grafika
  9. */
  10. class Rectangle extends Base implements DrawingObjectInterface
  11. {
  12. public function draw($image)
  13. {
  14. $x1 = $this->pos[0];
  15. $x2 = $x1 + $this->getWidth();
  16. $y1 = $this->pos[1];
  17. $y2 = $y1 + $this->getHeight();
  18. if( null !== $this->fillColor ){
  19. list($r, $g, $b, $alpha) = $this->fillColor->getRgba();
  20. $fillColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
  21. imagefilledrectangle($image->getCore(), $x1, $y1, $x2, $y2, $fillColorResource);
  22. }
  23. // Create borders. It will be placed on top of the filled rectangle (if present)
  24. if ( 0 < $this->getBorderSize() and null !== $this->borderColor) { // With border > 0 AND borderColor !== null
  25. list($r, $g, $b, $alpha) = $this->borderColor->getRgba();
  26. $borderColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
  27. imagerectangle($image->getCore(), $x1, $y1, $x2, $y2, $borderColorResource);
  28. }
  29. return $image;
  30. }
  31. }