diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 98c65038..16a3bea9 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -353,12 +353,12 @@ abstract class AbstractImage implements ImageInterface public function crop( int $width, int $height, - string $position = 'center', int $offset_x = 0, - int $offset_y = 0 + int $offset_y = 0, + string $position = 'top-left' ): ImageInterface { return $this->modify( - $this->resolveDriverClass('Modifiers\CropModifier', $width, $height, $position, $offset_x, $offset_y) + $this->resolveDriverClass('Modifiers\CropModifier', $width, $height, $offset_x, $offset_y, $position) ); } diff --git a/src/Drivers/Gd/Modifiers/CropModifier.php b/src/Drivers/Gd/Modifiers/CropModifier.php index 3d2ba781..fcfea575 100644 --- a/src/Drivers/Gd/Modifiers/CropModifier.php +++ b/src/Drivers/Gd/Modifiers/CropModifier.php @@ -13,9 +13,9 @@ class CropModifier implements ModifierInterface public function __construct( protected int $width, protected int $height, - protected string $position = 'center', protected int $offset_x = 0, - protected int $offset_y = 0 + protected int $offset_y = 0, + protected string $position = 'top-left' ) { // } diff --git a/src/Drivers/Imagick/Modifiers/CropModifier.php b/src/Drivers/Imagick/Modifiers/CropModifier.php index 8eccc916..274857b6 100644 --- a/src/Drivers/Imagick/Modifiers/CropModifier.php +++ b/src/Drivers/Imagick/Modifiers/CropModifier.php @@ -11,9 +11,9 @@ class CropModifier implements ModifierInterface public function __construct( protected int $width, protected int $height, - protected string $position = 'center', protected int $offset_x = 0, - protected int $offset_y = 0 + protected int $offset_y = 0, + protected string $position = 'top-left' ) { // } diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index a5bb64e1..9b3867d9 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -240,7 +240,12 @@ interface ImageInterface extends Traversable, Countable * @param int $offset_y * @return ImageInterface */ - public function place($element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0): ImageInterface; + public function place( + $element, + string $position = 'top-left', + int $offset_x = 0, + int $offset_y = 0 + ): ImageInterface; /** * Stretch the image to the desired size @@ -302,6 +307,12 @@ interface ImageInterface extends Traversable, Countable public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface; /** + * Padded resizing means that the original image is scaled until it fits the + * defined target size with unchanged aspect ratio. Compared to the fit() + * method, this call does not create cropped areas, but new empty areas + * on the sides of the result image. These are filled with the specified + * background color. + * * @param int $width * @param int $height * @param string $background @@ -311,13 +322,40 @@ interface ImageInterface extends Traversable, Countable public function pad(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface; /** + * This method does the same thing as pad() but does not exceed the size of + * the original image. You can use this if you want to prevent up-sampling. + * * @param int $width * @param int $height * @param string $background * @param string $position * @return ImageInterface */ - public function padDown(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface; + public function padDown( + int $width, + int $height, + $background = 'ffffff', + string $position = 'center' + ): ImageInterface; + + /** + * Cut out a rectangular part of the current image with given width and height at a given position. + * Define optional x,y offset coordinates to move the cutout by the given amount of pixels. + * + * @param int $width + * @param int $height + * @param int $offset_x + * @param int $offset_y + * @param string $position + * @return ImageInterface + */ + public function crop( + int $width, + int $height, + int $offset_x = 0, + int $offset_y = 0, + string $position = 'top-left' + ): ImageInterface; public function fill($color, ?int $x = null, ?int $y = null): ImageInterface; public function pixelate(int $size): ImageInterface; diff --git a/tests/Drivers/Gd/Modifiers/CropModifierTest.php b/tests/Drivers/Gd/Modifiers/CropModifierTest.php index ccc2fe6b..45001d76 100644 --- a/tests/Drivers/Gd/Modifiers/CropModifierTest.php +++ b/tests/Drivers/Gd/Modifiers/CropModifierTest.php @@ -17,7 +17,7 @@ class CropModifierTest extends TestCase public function testModify(): void { $image = $this->createTestImage('blocks.png'); - $image = $image->modify(new CropModifier(200, 200, 'bottom-right')); + $image = $image->modify(new CropModifier(200, 200, 0, 0, 'bottom-right')); $this->assertEquals(200, $image->width()); $this->assertEquals(200, $image->height()); $this->assertColor(255, 0, 0, 255, $image->pickColor(5, 5)); diff --git a/tests/Drivers/Imagick/Modifiers/CropModifierTest.php b/tests/Drivers/Imagick/Modifiers/CropModifierTest.php index 2c9dd108..75a0d247 100644 --- a/tests/Drivers/Imagick/Modifiers/CropModifierTest.php +++ b/tests/Drivers/Imagick/Modifiers/CropModifierTest.php @@ -17,7 +17,7 @@ class CropModifierTest extends TestCase public function testModify(): void { $image = $this->createTestImage('blocks.png'); - $image = $image->modify(new CropModifier(200, 200, 'bottom-right')); + $image = $image->modify(new CropModifier(200, 200, 0, 0, 'bottom-right')); $this->assertEquals(200, $image->width()); $this->assertEquals(200, $image->height()); $this->assertColor(255, 0, 0, 255, $image->pickColor(5, 5));