Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for auto resize #14

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading