CropCommand.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Intervention\Image\Gd\Commands;
  3. use \Intervention\Image\Point;
  4. use \Intervention\Image\Size;
  5. class CropCommand extends ResizeCommand
  6. {
  7. /**
  8. * Crop an image instance
  9. *
  10. * @param \Intervention\Image\Image $image
  11. * @return boolean
  12. */
  13. public function execute($image)
  14. {
  15. $width = $this->argument(0)->type('digit')->required()->value();
  16. $height = $this->argument(1)->type('digit')->required()->value();
  17. $x = $this->argument(2)->type('digit')->value();
  18. $y = $this->argument(3)->type('digit')->value();
  19. if (is_null($width) || is_null($height)) {
  20. throw new \Intervention\Image\Exception\InvalidArgumentException(
  21. "Width and height of cutout needs to be defined."
  22. );
  23. }
  24. $cropped = new Size($width, $height);
  25. $position = new Point($x, $y);
  26. // align boxes
  27. if (is_null($x) && is_null($y)) {
  28. $position = $image->getSize()->align('center')->relativePosition($cropped->align('center'));
  29. }
  30. // crop image core
  31. return $this->modify($image, 0, 0, $position->x, $position->y, $cropped->width, $cropped->height, $cropped->width, $cropped->height);
  32. }
  33. }