1
0

PolygonCommand.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Intervention\Image\Commands;
  3. use \Closure;
  4. class PolygonCommand extends \Intervention\Image\Commands\AbstractCommand
  5. {
  6. /**
  7. * Draw a polygon on given image
  8. *
  9. * @param \Intervention\Image\image $image
  10. * @return boolean
  11. */
  12. public function execute($image)
  13. {
  14. $points = $this->argument(0)->type('array')->required()->value();
  15. $callback = $this->argument(1)->type('closure')->value();
  16. $vertices_count = count($points);
  17. // check if number if coordinates is even
  18. if ($vertices_count % 2 !== 0) {
  19. throw new \Intervention\Image\Exception\InvalidArgumentException(
  20. "The number of given polygon vertices must be even."
  21. );
  22. }
  23. if ($vertices_count < 6) {
  24. throw new \Intervention\Image\Exception\InvalidArgumentException(
  25. "You must have at least 3 points in your array."
  26. );
  27. }
  28. $polygon_classname = sprintf('\Intervention\Image\%s\Shapes\PolygonShape',
  29. $image->getDriver()->getDriverName());
  30. $polygon = new $polygon_classname($points);
  31. if ($callback instanceof Closure) {
  32. $callback($polygon);
  33. }
  34. $polygon->applyToImage($image);
  35. return true;
  36. }
  37. }