Skip to content

Commit b7461cd

Browse files
Add support for generativeFill background
1 parent a42b7d8 commit b7461cd

File tree

5 files changed

+140
-10
lines changed

5 files changed

+140
-10
lines changed

src/Transformation/Background/AutoBackground.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ class AutoBackground extends Background
4444
*/
4545
const PREDOMINANT = 'predominant';
4646

47-
/**
48-
* @var string $name The name.
49-
*/
50-
protected static $name = 'background';
51-
5247
/**
5348
* @var string $type The type of the background color. Use the constants defined in this class.
5449
*/

src/Transformation/Background/Background.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Background extends BaseQualifier
3232
use AutoGradientBackgroundTrait;
3333
use ColorValueTrait;
3434

35+
/**
36+
* @var string $name The name.
37+
*/
38+
protected static $name = 'background';
39+
3540
/**
3641
* Background constructor.
3742
*
@@ -82,4 +87,17 @@ public static function auto($autoBackground = null)
8287
{
8388
return ClassUtils::forceInstance($autoBackground, AutoBackground::class);
8489
}
90+
91+
/**
92+
* Applies generative AI background.
93+
*
94+
* @param string|array $prompt Use natural language to describe what generate in the image.
95+
* @param bool $ignoreForeground Whether to take foreground elements into account.
96+
*
97+
* @return GenerativeFillBackground
98+
*/
99+
public static function generativeFill($prompt = null, $ignoreForeground = null)
100+
{
101+
return new GenerativeFillBackground($prompt, $ignoreForeground);
102+
}
85103
}

src/Transformation/Background/BlurredBackground.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
*/
2222
class BlurredBackground extends Background
2323
{
24-
/**
25-
* @var string $name The name.
26-
*/
27-
protected static $name = 'background';
28-
2924
/**
3025
* @var array $valueOrder The order of the values.
3126
*/
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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\ArrayUtils;
14+
use Cloudinary\TransformationUtils;
15+
16+
/**
17+
* Using generative AI, you can automatically add visually realistic pixels to either or both dimensions of the image.
18+
*
19+
* **Learn more**:
20+
* <a href="https://cloudinary.com/documentation/effects_and_artistic_enhancements#generative_fill"
21+
* target="_blank">Generative fill</a>
22+
*
23+
* @api
24+
*/
25+
class GenerativeFillBackground extends Background
26+
{
27+
const GEN_FILL = 'gen_fill';
28+
29+
const IGNORE_FOREGROUND = 'ignore-foreground';
30+
31+
const PROPERTIES = 'properties';
32+
33+
/**
34+
* @var array $valueOrder The order of the values.
35+
*/
36+
protected $valueOrder = [0, self::PROPERTIES];
37+
38+
/**
39+
* GenerativeFillBackground constructor.
40+
*
41+
* @param string|array $prompt Use natural language to describe what generate in the image.
42+
* @param bool $ignoreForeground Whether to take foreground elements into account.
43+
*/
44+
public function __construct($prompt = null, $ignoreForeground = null)
45+
{
46+
parent::__construct(self::GEN_FILL);
47+
48+
$this->value->setSimpleValue(self::PROPERTIES, new ListQualifierMultiValue());
49+
50+
$this->prompt($prompt);
51+
$this->ignoreForeground($ignoreForeground);
52+
}
53+
54+
/**
55+
* Use natural language to describe what you want to affect in the image.
56+
*
57+
* @param string|array $prompt A list of prompts.
58+
*
59+
* @return $this
60+
*/
61+
public function prompt($prompt)
62+
{
63+
$this->value->getSimpleValue(self::PROPERTIES)->setSimpleNamedValue(
64+
GenerativeEffectAction::PROMPT,
65+
new FullListQualifierMultiValue(...ArrayUtils::build($prompt))
66+
);
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* Whether to take foreground elements into account.
73+
*
74+
* @param bool $ignoreForeground Whether to take foreground elements into account.
75+
*
76+
* @return $this
77+
*/
78+
public function ignoreForeground($ignoreForeground = true)
79+
{
80+
$this->value->getSimpleValue(self::PROPERTIES)->setSimpleNamedValue(
81+
self::IGNORE_FOREGROUND,
82+
TransformationUtils::boolToString($ignoreForeground)
83+
);
84+
85+
return $this;
86+
}
87+
}

tests/Unit/Transformation/Common/BackgroundTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,39 @@ public function testBackgroundAuto()
113113
)
114114
);
115115
}
116+
117+
public function testGenerativeFillBackground()
118+
{
119+
self::assertStrEquals(
120+
'b_gen_fill,c_pad,h_500,w_500',
121+
(new Transformation())->resize(
122+
Resize::pad(500, 500)->background(Background::generativeFill())
123+
)
124+
);
125+
126+
self::assertStrEquals(
127+
'b_gen_fill:prompt_bowls of cereal,c_pad,h_500,w_500',
128+
(new Transformation())->resize(
129+
Resize::pad(500, 500)->background(Background::generativeFill()->prompt('bowls of cereal'))
130+
)
131+
);
132+
133+
self::assertStrEquals(
134+
'b_gen_fill:ignore-foreground_true;prompt_bowls of cereal,c_pad,h_500,w_500',
135+
(new Transformation())->resize(
136+
Resize::pad(500, 500)->background(
137+
Background::generativeFill()->prompt('bowls of cereal')->ignoreForeground()
138+
)
139+
)
140+
);
141+
142+
self::assertStrEquals(
143+
'b_gen_fill:ignore-foreground_false,c_pad,h_500,w_500',
144+
(new Transformation())->resize(
145+
Resize::pad(500, 500)->background(
146+
Background::generativeFill()->ignoreForeground(false)
147+
)
148+
)
149+
);
150+
}
116151
}

0 commit comments

Comments
 (0)