CropCommand.php 1.3 KB

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