LineShape.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Intervention\Image\Imagick\Shapes;
  3. use Intervention\Image\Image;
  4. use Intervention\Image\Imagick\Color;
  5. class LineShape extends \Intervention\Image\AbstractShape
  6. {
  7. /**
  8. * Starting point x-coordinate of line
  9. *
  10. * @var int
  11. */
  12. public $x = 0;
  13. /**
  14. * Starting point y-coordinate of line
  15. *
  16. * @var int
  17. */
  18. public $y = 0;
  19. /**
  20. * Color of line
  21. *
  22. * @var string
  23. */
  24. public $color = '#000000';
  25. /**
  26. * Width of line in pixels
  27. *
  28. * @var int
  29. */
  30. public $width = 1;
  31. /**
  32. * Create new line shape instance
  33. *
  34. * @param int $x
  35. * @param int $y
  36. */
  37. public function __construct($x = null, $y = null)
  38. {
  39. $this->x = is_numeric($x) ? intval($x) : $this->x;
  40. $this->y = is_numeric($y) ? intval($y) : $this->y;
  41. }
  42. /**
  43. * Set current line color
  44. *
  45. * @param string $color
  46. * @return void
  47. */
  48. public function color($color)
  49. {
  50. $this->color = $color;
  51. }
  52. /**
  53. * Set current line width in pixels
  54. *
  55. * @param int $width
  56. * @return void
  57. */
  58. public function width($width)
  59. {
  60. $this->width = $width;
  61. }
  62. /**
  63. * Draw current instance of line to given endpoint on given image
  64. *
  65. * @param Image $image
  66. * @param int $x
  67. * @param int $y
  68. * @return boolean
  69. */
  70. public function applyToImage(Image $image, $x = 0, $y = 0)
  71. {
  72. $line = new \ImagickDraw;
  73. $color = new Color($this->color);
  74. $line->setStrokeColor($color->getPixel());
  75. $line->setStrokeWidth($this->width);
  76. $line->line($this->x, $this->y, $x, $y);
  77. $image->getCore()->drawImage($line);
  78. return true;
  79. }
  80. }