FitCommand.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Intervention\Image\Imagick\Commands;
  3. use \Intervention\Image\Size;
  4. class FitCommand extends \Intervention\Image\Commands\AbstractCommand
  5. {
  6. /**
  7. * Crops and resized an image at the same time
  8. *
  9. * @param \Intervention\Image\Image $image
  10. * @return boolean
  11. */
  12. public function execute($image)
  13. {
  14. $width = $this->argument(0)->type('digit')->required()->value();
  15. $height = $this->argument(1)->type('digit')->value($width);
  16. $constraints = $this->argument(2)->type('closure')->value();
  17. $position = $this->argument(3)->type('string')->value('center');
  18. // calculate size
  19. $cropped = $image->getSize()->fit(new Size($width, $height), $position);
  20. $resized = clone $cropped;
  21. $resized = $resized->resize($width, $height, $constraints);
  22. // crop image
  23. $image->getCore()->cropImage(
  24. $cropped->width,
  25. $cropped->height,
  26. $cropped->pivot->x,
  27. $cropped->pivot->y
  28. );
  29. // resize image
  30. $image->getCore()->scaleImage($resized->getWidth(), $resized->getHeight());
  31. $image->getCore()->setImagePage(0,0,0,0);
  32. return true;
  33. }
  34. }