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 PolygonShape extends \Intervention\Image\AbstractShape
0009 {
0010     /**
0011      * Array of points of polygon
0012      *
0013      * @var array
0014      */
0015     public $points;
0016 
0017     /**
0018      * Create new polygon instance
0019      *
0020      * @param array $points
0021      */
0022     public function __construct($points)
0023     {
0024         $this->points = $this->formatPoints($points);
0025     }
0026 
0027     /**
0028      * Draw polygon on given image
0029      *
0030      * @param  Image   $image
0031      * @param  int     $x
0032      * @param  int     $y
0033      * @return boolean
0034      */
0035     public function applyToImage(Image $image, $x = 0, $y = 0)
0036     {
0037         $polygon = new \ImagickDraw;
0038 
0039         // set background
0040         $bgcolor = new Color($this->background);
0041         $polygon->setFillColor($bgcolor->getPixel());
0042 
0043         // set border
0044         if ($this->hasBorder()) {
0045             $border_color = new Color($this->border_color);
0046             $polygon->setStrokeWidth($this->border_width);
0047             $polygon->setStrokeColor($border_color->getPixel());
0048         }
0049 
0050         $polygon->polygon($this->points);
0051 
0052         $image->getCore()->drawImage($polygon);
0053 
0054         return true;
0055     }
0056 
0057     /**
0058      * Format polygon points to Imagick format
0059      *
0060      * @param  Array $points
0061      * @return Array
0062      */
0063     private function formatPoints($points)
0064     {
0065         $ipoints = [];
0066         $count = 1;
0067 
0068         foreach ($points as $key => $value) {
0069             if ($count%2 === 0) {
0070                 $y = $value;
0071                 $ipoints[] = ['x' => $x, 'y' => $y];
0072             } else {
0073                 $x = $value;
0074             }
0075             $count++;
0076         }
0077 
0078         return $ipoints;
0079     }
0080 }