Skip to content

Commit f5fa243

Browse files
Add support for autoPad resize
1 parent 0a24cb0 commit f5fa243

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

src/Transformation/Background/BackgroundTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait BackgroundTrait
2626
* The image background is visible when padding is added with one of the padding crop modes, when rounding corners,
2727
* when adding overlays, and with semi-transparent PNGs and GIFs.
2828
*
29-
* @param Background|ColorValue|string $background The the background to set.
29+
* @param Background|ColorValue|string $background The background to set.
3030
*
3131
* @return $this
3232
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file is part of the Cloudinary PHP package.
4+
*
5+
* (c) Cloudinary
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Cloudinary\Transformation;
12+
13+
use Cloudinary\Transformation\Argument\ColorValue;
14+
use Cloudinary\Transformation\Expression\Expression;
15+
use InvalidArgumentException;
16+
17+
/**
18+
* Class CropPad
19+
*
20+
* @internal
21+
*/
22+
class CropPad extends Crop
23+
{
24+
use CropPadTrait;
25+
use BackgroundTrait;
26+
27+
/**
28+
* CropPad constructor.
29+
*
30+
* @param string|CropMode $cropMode
31+
* @param int|string|Expression $width
32+
* @param int|string|Expression $height
33+
* @param mixed $gravity
34+
* @param string|Background|ColorValue $background
35+
*/
36+
public function __construct($cropMode, $width = null, $height = null, $gravity = null, $background = null)
37+
{
38+
if ($gravity === null) {
39+
$gravity = Gravity::auto();
40+
}
41+
42+
parent::__construct($cropMode, $width, $height, $gravity);
43+
44+
$this->background($background);
45+
}
46+
47+
/**
48+
* Sets the gravity to use when using the FILL_PAD crop mode.
49+
*
50+
* @param $autoGravity
51+
*
52+
* @return $this
53+
*/
54+
public function gravity($autoGravity)
55+
{
56+
if (! $autoGravity instanceof AutoGravity) {
57+
throw new InvalidArgumentException('CropPad only supports Auto Gravity');
58+
}
59+
60+
$this->addQualifier($autoGravity);
61+
62+
return $this;
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* This file is part of the Cloudinary PHP package.
4+
*
5+
* (c) Cloudinary
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Cloudinary\Transformation;
12+
13+
use Cloudinary\Transformation\Argument\ColorValue;
14+
15+
/**
16+
* Trait CropPadTrait
17+
*
18+
* @api
19+
*/
20+
trait CropPadTrait
21+
{
22+
/**
23+
* Tries to prevent a "bad crop" by first attempting to use the auto cropping mode, but adding some padding if the
24+
* algorithm determines that more of the original image needs to be included in the final image.
25+
*
26+
* Especially useful if the aspect ratio of the delivered image is considerably different from the original's
27+
* aspect ratio.
28+
*
29+
* Only supported in conjunction with Automatic cropping (Gravity::auto())
30+
*
31+
* @param int|float|string|null $width The required width of a transformed asset.
32+
* @param int|float|null $height The required height of a transformed asset.
33+
* @param FocalGravity|string $gravity Specifies which part of the original image to include.
34+
* @param Background|ColorValue|string $background The background color of the image.
35+
*
36+
* @return CropPad
37+
*
38+
* @see Gravity::auto
39+
*/
40+
public static function autoPad($width = null, $height = null, $gravity = null, $background = null)
41+
{
42+
return new CropPad(CropMode::AUTO_PAD, $width, $height, $gravity, $background);
43+
}
44+
}

src/Transformation/Resize/Parameter/CropMode.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ class CropMode extends BaseQualifier
125125
*/
126126
const AUTO = 'auto';
127127

128+
/**
129+
* The AUTO_PAD crop mode tries to prevent a "bad crop" by first attempting to use the auto cropping mode,
130+
* but adding some padding if the algorithm determines that more of the original image needs to be included in the
131+
* final image.
132+
*/
133+
const AUTO_PAD = 'auto_pad';
134+
128135
/**
129136
* The IMAGGA_CROP crop mode crops your image based on automatically calculated areas of interest within each
130137
* specific photo.

src/Transformation/Resize/ResizeTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ trait ResizeTrait
2323
use FillTrait;
2424
use FillPadTrait;
2525
use CropTrait;
26+
use CropPadTrait;
2627
use ImaggaTrait;
2728
}

tests/Unit/Transformation/Image/ResizeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
use Cloudinary\Transformation\Argument\Color;
1515
use Cloudinary\Transformation\AspectRatio;
1616
use Cloudinary\Transformation\AutoGravity;
17+
use Cloudinary\Transformation\Background;
1718
use Cloudinary\Transformation\CompassGravity;
1819
use Cloudinary\Transformation\CompassPosition;
1920
use Cloudinary\Transformation\Crop;
21+
use Cloudinary\Transformation\CropPad;
2022
use Cloudinary\Transformation\Fill;
2123
use Cloudinary\Transformation\FillPad;
24+
use Cloudinary\Transformation\FocusOn;
2225
use Cloudinary\Transformation\Gravity;
2326
use Cloudinary\Transformation\Pad;
2427
use Cloudinary\Transformation\Qualifier;
@@ -255,6 +258,31 @@ public function testCrop()
255258
);
256259
}
257260

261+
public function testCropAutoPad()
262+
{
263+
self::assertStrEquals(
264+
'c_auto_pad,g_auto,h_200,w_100',
265+
CropPad::autoPad(100, 200)
266+
);
267+
268+
self::assertStrEquals(
269+
'c_auto_pad,g_auto,h_200,w_100,z_0.5',
270+
CropPad::autoPad(100, 200, Gravity::auto())->zoom(0.5)
271+
);
272+
273+
self::assertStrEquals(
274+
'c_auto_pad,g_auto:dog,h_200,w_100',
275+
CropPad::autoPad(100, 200, Gravity::auto()->autoFocus(FocusOn::dog()))
276+
);
277+
278+
self::assertStrEquals(
279+
'b_gen_fill,c_auto_pad,g_auto:dog,h_200,w_100',
280+
CropPad::autoPad(100, 200, Gravity::auto()->autoFocus(FocusOn::dog()))->background(
281+
Background::generativeFill()
282+
)
283+
);
284+
}
285+
258286
public function testResize()
259287
{
260288
/** @noinspection PhpUndefinedMethodInspection */
@@ -303,5 +331,10 @@ public function testResize()
303331
'c_crop,h_70,w_50,z_0.5',
304332
(string)Resize::crop(50, 70)->zoom(0.5)
305333
);
334+
335+
self::assertStrEquals(
336+
'b_gen_fill,c_auto_pad,g_auto:dog,h_200,w_100',
337+
Resize::autoPad(100, 200, Gravity::auto()->autoFocus(FocusOn::dog()), Background::generativeFill())
338+
);
306339
}
307340
}

0 commit comments

Comments
 (0)