RectangleShape.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Intervention\Image\Imagick\Shapes;
  3. use Intervention\Image\Image;
  4. use Intervention\Image\Imagick\Color;
  5. class RectangleShape extends \Intervention\Image\AbstractShape
  6. {
  7. /**
  8. * X-Coordinate of top-left point
  9. *
  10. * @var int
  11. */
  12. public $x1 = 0;
  13. /**
  14. * Y-Coordinate of top-left point
  15. *
  16. * @var int
  17. */
  18. public $y1 = 0;
  19. /**
  20. * X-Coordinate of bottom-right point
  21. *
  22. * @var int
  23. */
  24. public $x2 = 0;
  25. /**
  26. * Y-Coordinate of bottom-right point
  27. *
  28. * @var int
  29. */
  30. public $y2 = 0;
  31. /**
  32. * Create new rectangle shape instance
  33. *
  34. * @param int $x1
  35. * @param int $y1
  36. * @param int $x2
  37. * @param int $y2
  38. */
  39. public function __construct($x1 = null, $y1 = null, $x2 = null, $y2 = null)
  40. {
  41. $this->x1 = is_numeric($x1) ? intval($x1) : $this->x1;
  42. $this->y1 = is_numeric($y1) ? intval($y1) : $this->y1;
  43. $this->x2 = is_numeric($x2) ? intval($x2) : $this->x2;
  44. $this->y2 = is_numeric($y2) ? intval($y2) : $this->y2;
  45. }
  46. /**
  47. * Draw rectangle to given image at certain position
  48. *
  49. * @param Image $image
  50. * @param int $x
  51. * @param int $y
  52. * @return boolean
  53. */
  54. public function applyToImage(Image $image, $x = 0, $y = 0)
  55. {
  56. $rectangle = new \ImagickDraw;
  57. // set background
  58. $bgcolor = new Color($this->background);
  59. $rectangle->setFillColor($bgcolor->getPixel());
  60. // set border
  61. if ($this->hasBorder()) {
  62. $border_color = new Color($this->border_color);
  63. $rectangle->setStrokeWidth($this->border_width);
  64. $rectangle->setStrokeColor($border_color->getPixel());
  65. }
  66. $rectangle->rectangle($this->x1, $this->y1, $this->x2, $this->y2);
  67. $image->getCore()->drawImage($rectangle);
  68. return true;
  69. }
  70. }