File indexing completed on 2025-01-26 05:29:14

0001 <?php
0002 
0003 namespace Intervention\Image\Imagick\Shapes;
0004 
0005 use Intervention\Image\Image;
0006 use Intervention\Image\Imagick\Color;
0007 
0008 class RectangleShape extends \Intervention\Image\AbstractShape
0009 {
0010     /**
0011      * X-Coordinate of top-left point
0012      *
0013      * @var int
0014      */
0015     public $x1 = 0;
0016 
0017     /**
0018      * Y-Coordinate of top-left point
0019      *
0020      * @var int
0021      */
0022     public $y1 = 0;
0023 
0024     /**
0025      * X-Coordinate of bottom-right point
0026      *
0027      * @var int
0028      */
0029     public $x2 = 0;
0030 
0031     /**
0032      * Y-Coordinate of bottom-right point
0033      *
0034      * @var int
0035      */
0036     public $y2 = 0;
0037 
0038     /**
0039      * Create new rectangle shape instance
0040      *
0041      * @param int $x1
0042      * @param int $y1
0043      * @param int $x2
0044      * @param int $y2
0045      */
0046     public function __construct($x1 = null, $y1 = null, $x2 = null, $y2 = null)
0047     {
0048         $this->x1 = is_numeric($x1) ? intval($x1) : $this->x1;
0049         $this->y1 = is_numeric($y1) ? intval($y1) : $this->y1;
0050         $this->x2 = is_numeric($x2) ? intval($x2) : $this->x2;
0051         $this->y2 = is_numeric($y2) ? intval($y2) : $this->y2;
0052     }
0053 
0054     /**
0055      * Draw rectangle to given image at certain position
0056      *
0057      * @param  Image   $image
0058      * @param  int     $x
0059      * @param  int     $y
0060      * @return boolean
0061      */
0062     public function applyToImage(Image $image, $x = 0, $y = 0)
0063     {
0064         $rectangle = new \ImagickDraw;
0065 
0066         // set background
0067         $bgcolor = new Color($this->background);
0068         $rectangle->setFillColor($bgcolor->getPixel());
0069 
0070         // set border
0071         if ($this->hasBorder()) {
0072             $border_color = new Color($this->border_color);
0073             $rectangle->setStrokeWidth($this->border_width);
0074             $rectangle->setStrokeColor($border_color->getPixel());
0075         }
0076 
0077         $rectangle->rectangle($this->x1, $this->y1, $this->x2, $this->y2);
0078 
0079         $image->getCore()->drawImage($rectangle);
0080 
0081         return true;
0082     }
0083 }