RectangleShape.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Intervention\Image\Gd\Shapes;
  3. use Intervention\Image\Image;
  4. use Intervention\Image\Gd\Color;
  5. class RectangleShape extends \Intervention\Image\AbstractShape
  6. {
  7. /**
  8. * X-Coordinate of top-left point
  9. *
  10. * @var integer
  11. */
  12. public $x1 = 0;
  13. /**
  14. * Y-Coordinate of top-left point
  15. *
  16. * @var integer
  17. */
  18. public $y1 = 0;
  19. /**
  20. * X-Coordinate of bottom-right point
  21. *
  22. * @var integer
  23. */
  24. public $x2 = 0;
  25. /**
  26. * Y-Coordinate of bottom-right point
  27. *
  28. * @var integer
  29. */
  30. public $y2 = 0;
  31. /**
  32. * Create new rectangle shape instance
  33. *
  34. * @param integer $x1
  35. * @param integer $y1
  36. * @param integer $x2
  37. * @param integer $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 integer $x
  51. * @param integer $y
  52. * @return boolean
  53. */
  54. public function applyToImage(Image $image, $x = 0, $y = 0)
  55. {
  56. $background = new Color($this->background);
  57. imagefilledrectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $background->getInt());
  58. if ($this->hasBorder()) {
  59. $border_color = new Color($this->border_color);
  60. imagesetthickness($image->getCore(), $this->border_width);
  61. imagerectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $border_color->getInt());
  62. }
  63. return true;
  64. }
  65. }