CropCommand.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Intervention\Image\Imagick\Commands;
  3. use Intervention\Image\Point;
  4. use Intervention\Image\Size;
  5. class CropCommand extends \Intervention\Image\Commands\AbstractCommand
  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. $image->getCore()->cropImage($cropped->width, $cropped->height, $position->x, $position->y);
  32. $image->getCore()->setImagePage(0,0,0,0);
  33. return true;
  34. }
  35. }