1 | <?php |
||
2 | |||
3 | namespace Jackal\ImageMerge\Command; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use Jackal\ImageMerge\Command\Options\CropCommandOption; |
||
7 | use Jackal\ImageMerge\Model\Image; |
||
8 | |||
9 | /** |
||
10 | * Class CropCommand |
||
11 | * @package Jackal\ImageMerge\Command |
||
12 | */ |
||
13 | class CropCommand extends AbstractCommand |
||
14 | { |
||
15 | /** |
||
16 | * CropCommand constructor. |
||
17 | * @param CropCommandOption $options |
||
18 | */ |
||
19 | public function __construct(CropCommandOption $options) |
||
20 | { |
||
21 | parent::__construct($options); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param Image $image |
||
26 | * @return Image |
||
27 | */ |
||
28 | public function execute(Image $image) |
||
29 | { |
||
30 | if ($image->getWidth() == $this->options->getDimention()->getWidth() and $image->getHeight() == $this->options->getDimention()->getHeight()) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | return $image; |
||
32 | } |
||
33 | |||
34 | if ($this->options->getDimention()->getWidth() > $image->getWidth() || $this->options->getDimention()->getHeight() > $image->getHeight()) { |
||
35 | throw new InvalidArgumentException(sprintf('Crop area exceed, max dimensions are: %s X %s', $image->getWidth(), $image->getHeight())); |
||
36 | } |
||
37 | |||
38 | /** @var CropCommandOption $options */ |
||
39 | $options = $this->options; |
||
40 | $newImage = imagecrop($image->getResource(), [ |
||
41 | 'x' => $options->getCoordinate1()->getX(), |
||
42 | 'y' => $options->getCoordinate1()->getY(), |
||
43 | 'width' => $options->getDimention()->getWidth(), |
||
44 | 'height' => $options->getDimention()->getHeight(), |
||
45 | ]); |
||
46 | |||
47 | return $image->assignResource($newImage); |
||
48 | } |
||
49 | } |
||
50 |