PolygonCommand.php 1.3 KB

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