File indexing completed on 2025-01-26 05:29:13

0001 <?php
0002 
0003 namespace Intervention\Image\Gd\Commands;
0004 
0005 use Intervention\Image\Point;
0006 use Intervention\Image\Size;
0007 
0008 class CropCommand extends ResizeCommand
0009 {
0010     /**
0011      * Crop an image instance
0012      *
0013      * @param  \Intervention\Image\Image $image
0014      * @return boolean
0015      */
0016     public function execute($image)
0017     {
0018         $width = $this->argument(0)->type('digit')->required()->value();
0019         $height = $this->argument(1)->type('digit')->required()->value();
0020         $x = $this->argument(2)->type('digit')->value();
0021         $y = $this->argument(3)->type('digit')->value();
0022 
0023         if (is_null($width) || is_null($height)) {
0024             throw new \Intervention\Image\Exception\InvalidArgumentException(
0025                 "Width and height of cutout needs to be defined."
0026             );
0027         }
0028 
0029         $cropped = new Size($width, $height);
0030         $position = new Point($x, $y);
0031 
0032         // align boxes
0033         if (is_null($x) && is_null($y)) {
0034             $position = $image->getSize()->align('center')->relativePosition($cropped->align('center'));
0035         }
0036 
0037         // crop image core
0038         return $this->modify($image, 0, 0, $position->x, $position->y, $cropped->width, $cropped->height, $cropped->width, $cropped->height);
0039     }
0040 }