From a241ea22b54d0c3cd04b16b05ce8be88dd04a0be Mon Sep 17 00:00:00 2001 From: Constantine Nathanson Date: Mon, 27 May 2024 12:12:26 +0300 Subject: [PATCH] Add support for `auto` resize --- src/Transformation/Resize/Crop/CropTrait.php | 18 ++++++++++++++++++ .../Resize/Parameter/CropMode.php | 5 +++++ .../Unit/Transformation/Image/ResizeTest.php | 19 +++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Transformation/Resize/Crop/CropTrait.php b/src/Transformation/Resize/Crop/CropTrait.php index 8a7737e..e5e5878 100644 --- a/src/Transformation/Resize/Crop/CropTrait.php +++ b/src/Transformation/Resize/Crop/CropTrait.php @@ -51,6 +51,24 @@ public static function thumbnail($width = null, $height = null, $gravity = null, return static::createCrop(CropMode::THUMBNAIL, $width, $height, $gravity, $x, $y); } + /** + * Automatically determines the best crop based on the gravity and specified dimensions. + * + * If the requested dimensions are smaller than the best crop, the result is downscaled. + * If the requested dimensions are larger than the original image, the result is upscaled. + * Use this mode in conjunction with the g (gravity) parameter. + * + * @param int|float|string|null $width The required width of a transformed asset. + * @param int|float|null $height The required height of a transformed asset. + * @param Gravity $gravity Which part of the original image to include. + * + * @return Crop + */ + public static function auto($width = null, $height = null, $gravity = null) + { + return static::createCrop(CropMode::AUTO, $width, $height, $gravity); + } + /** * Creates Crop instance. * diff --git a/src/Transformation/Resize/Parameter/CropMode.php b/src/Transformation/Resize/Parameter/CropMode.php index 393278f..2f43aee 100644 --- a/src/Transformation/Resize/Parameter/CropMode.php +++ b/src/Transformation/Resize/Parameter/CropMode.php @@ -120,6 +120,11 @@ class CropMode extends BaseQualifier */ const THUMBNAIL = 'thumb'; + /** + * The AUTO crop mode automatically determines the best crop based on the gravity and specified dimensions. + */ + const AUTO = 'auto'; + /** * The IMAGGA_CROP crop mode crops your image based on automatically calculated areas of interest within each * specific photo. diff --git a/tests/Unit/Transformation/Image/ResizeTest.php b/tests/Unit/Transformation/Image/ResizeTest.php index de83ee6..9fc1daf 100644 --- a/tests/Unit/Transformation/Image/ResizeTest.php +++ b/tests/Unit/Transformation/Image/ResizeTest.php @@ -10,6 +10,7 @@ namespace Cloudinary\Test\Unit\Transformation\Image; +use Cloudinary\Test\TransformationTestCase; use Cloudinary\Transformation\Argument\Color; use Cloudinary\Transformation\AspectRatio; use Cloudinary\Transformation\AutoGravity; @@ -25,12 +26,11 @@ use Cloudinary\Transformation\ResizeMode; use Cloudinary\Transformation\Scale; use InvalidArgumentException; -use PHPUnit\Framework\TestCase; /** * Class ResizeTest */ -final class ResizeTest extends TestCase +final class ResizeTest extends TransformationTestCase { public function testScale() { @@ -238,6 +238,21 @@ public function testCrop() 'c_thumb,g_auto,h_200,w_100,z_0.5', (string)$thumb ); + + self::assertStrEquals( + 'c_auto,g_auto,h_200,w_100', + Crop::auto(100, 200, Gravity::auto()) + ); + + self::assertStrEquals( + 'c_auto,g_auto,h_200,w_100', + Crop::auto()->width(100)->height(200)->gravity(Gravity::auto()) + ); + + self::assertStrEquals( + 'ar_0.5,c_auto,g_auto', + Crop::auto()->gravity(Gravity::auto())->aspectRatio(0.5) + ); } public function testResize()