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

0001 <?php
0002 
0003 namespace Intervention\Image\Commands;
0004 
0005 use Closure;
0006 
0007 class PolygonCommand extends \Intervention\Image\Commands\AbstractCommand
0008 {
0009     /**
0010      * Draw a polygon on given image
0011      *
0012      * @param  \Intervention\Image\image $image
0013      * @return boolean
0014      */
0015     public function execute($image)
0016     {
0017         $points = $this->argument(0)->type('array')->required()->value();
0018         $callback = $this->argument(1)->type('closure')->value();
0019 
0020         $vertices_count = count($points);
0021 
0022         // check if number if coordinates is even
0023         if ($vertices_count % 2 !== 0) {
0024             throw new \Intervention\Image\Exception\InvalidArgumentException(
0025                 "The number of given polygon vertices must be even."
0026             );
0027         }
0028 
0029         if ($vertices_count < 6) {
0030             throw new \Intervention\Image\Exception\InvalidArgumentException(
0031                 "You must have at least 3 points in your array."
0032             );
0033         }
0034         
0035         $polygon_classname = sprintf('\Intervention\Image\%s\Shapes\PolygonShape',
0036             $image->getDriver()->getDriverName());
0037 
0038         $polygon = new $polygon_classname($points);
0039         
0040         if ($callback instanceof Closure) {
0041             $callback($polygon);
0042         }
0043 
0044         $polygon->applyToImage($image);
0045 
0046         return true;
0047     }
0048 }