Skip to content

Commit

Permalink
Add support for auto resize
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed May 27, 2024
1 parent a88d355 commit a241ea2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Transformation/Resize/Crop/CropTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Transformation/Resize/Parameter/CropMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 17 additions & 2 deletions tests/Unit/Transformation/Image/ResizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a241ea2

Please sign in to comment.