Skip to content

Commit 1eee89c

Browse files
Add support for auto resize
1 parent 99cc982 commit 1eee89c

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Transformation/Resize/Crop/CropTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ public static function thumbnail($width = null, $height = null, $gravity = null,
5151
return static::createCrop(CropMode::THUMBNAIL, $width, $height, $gravity, $x, $y);
5252
}
5353

54+
/**
55+
* Automatically determines the best crop based on the gravity and specified dimensions.
56+
*
57+
* If the requested dimensions are smaller than the best crop, the result is downscaled.
58+
* If the requested dimensions are larger than the original image, the result is upscaled.
59+
* Use this mode in conjunction with the g (gravity) parameter.
60+
*
61+
* @param int|float|string|null $width The required width of a transformed asset.
62+
* @param int|float|null $height The required height of a transformed asset.
63+
* @param Gravity $gravity Which part of the original image to include.
64+
*
65+
* @return Crop
66+
*/
67+
public static function auto($width = null, $height = null, $gravity = null)
68+
{
69+
return static::createCrop(CropMode::AUTO, $width, $height, $gravity);
70+
}
71+
5472
/**
5573
* Creates Crop instance.
5674
*

src/Transformation/Resize/Parameter/CropMode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ class CropMode extends BaseQualifier
120120
*/
121121
const THUMBNAIL = 'thumb';
122122

123+
/**
124+
* The AUTO crop mode automatically determines the best crop based on the gravity and specified dimensions.
125+
*/
126+
const AUTO = 'auto';
127+
123128
/**
124129
* The IMAGGA_CROP crop mode crops your image based on automatically calculated areas of interest within each
125130
* specific photo.

tests/Unit/Transformation/Image/ResizeTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Cloudinary\Test\Unit\Transformation\Image;
1212

13+
use Cloudinary\Test\TransformationTestCase;
1314
use Cloudinary\Transformation\Argument\Color;
1415
use Cloudinary\Transformation\AspectRatio;
1516
use Cloudinary\Transformation\AutoGravity;
@@ -25,12 +26,11 @@
2526
use Cloudinary\Transformation\ResizeMode;
2627
use Cloudinary\Transformation\Scale;
2728
use InvalidArgumentException;
28-
use PHPUnit\Framework\TestCase;
2929

3030
/**
3131
* Class ResizeTest
3232
*/
33-
final class ResizeTest extends TestCase
33+
final class ResizeTest extends TransformationTestCase
3434
{
3535
public function testScale()
3636
{
@@ -238,6 +238,21 @@ public function testCrop()
238238
'c_thumb,g_auto,h_200,w_100,z_0.5',
239239
(string)$thumb
240240
);
241+
242+
self::assertStrEquals(
243+
'c_auto,g_auto,h_200,w_100',
244+
Crop::auto(100, 200, Gravity::auto())
245+
);
246+
247+
self::assertStrEquals(
248+
'c_auto,g_auto,h_200,w_100',
249+
Crop::auto()->width(100)->height(200)->gravity(Gravity::auto())
250+
);
251+
252+
self::assertStrEquals(
253+
'ar_0.5,c_auto,g_auto',
254+
Crop::auto()->gravity(Gravity::auto())->aspectRatio(0.5)
255+
);
241256
}
242257

243258
public function testResize()

0 commit comments

Comments
 (0)