Skip to content

Commit a42b7d8

Browse files
Add support for AI generative effects
1 parent 5cb9046 commit a42b7d8

21 files changed

+669
-12
lines changed

src/Transformation/Effect/EffectAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ class EffectAction extends Action
2929
*/
3030
public function __construct($effect, ...$args)
3131
{
32-
parent::__construct(ClassUtils::verifyInstance($effect, EffectQualifier::class, null, ...$args));
32+
parent::__construct(ClassUtils::verifyInstance($effect, static::MAIN_QUALIFIER, null, ...$args));
3333
}
3434
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Cloudinary\Transformation;
4+
5+
use Cloudinary\TransformationUtils;
6+
7+
/**
8+
* Trait DetectMultipleTrait
9+
*/
10+
trait DetectMultipleTrait
11+
{
12+
/**
13+
* Whether to detect all instances of the prompt in the image.
14+
*
15+
* When used with multiple prompts, it’s always true even if not explicitly set.
16+
*
17+
* @param bool $detectMultiple Whether to detect multiple objects.
18+
*
19+
* @return $this
20+
*/
21+
public function detectMultiple($detectMultiple = true)
22+
{
23+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(
24+
GenerativeEffectAction::MULTIPLE,
25+
TransformationUtils::boolToString($detectMultiple)
26+
);
27+
28+
return $this;
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
/**
14+
* Class GenerativeEffect
15+
*/
16+
abstract class GenerativeEffect
17+
{
18+
const GENERATIVE_RECOLOR = 'gen_recolor';
19+
const GENERATIVE_REMOVE = 'gen_remove';
20+
const GENERATIVE_REPLACE = 'gen_replace';
21+
const GENERATIVE_RESTORE = 'gen_restore';
22+
23+
use GenerativeEffectTrait;
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
/**
14+
* Class GenerativeEffectAction
15+
*/
16+
class GenerativeEffectAction extends EffectAction
17+
{
18+
const MAIN_QUALIFIER = ListEffectQualifier::class;
19+
20+
const MULTIPLE = 'multiple';
21+
const PROMPT = 'prompt';
22+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
/**
14+
* Trait GenerativeEffectTrait
15+
*
16+
* @api
17+
*/
18+
trait GenerativeEffectTrait
19+
{
20+
/**
21+
* Applies a generative restore effect to the asset.
22+
*
23+
* @return GenerativeEffectAction
24+
*/
25+
public static function generativeRestore()
26+
{
27+
return new GenerativeEffectAction(GenerativeEffect::GENERATIVE_RESTORE);
28+
}
29+
30+
/**
31+
* Applies a generative recolor effect to the asset.
32+
*
33+
* @param string|array $prompt Use natural language to describe what you want to affect in the image.
34+
* @param string $toColor The target color.
35+
* @param bool $detectMultiple Whether to recolor all instances of the prompt in the image.
36+
*
37+
* @return GenerativeRecolor
38+
*/
39+
public static function generativeRecolor($prompt, $toColor, $detectMultiple = null)
40+
{
41+
return new GenerativeRecolor($prompt, $toColor, $detectMultiple);
42+
}
43+
44+
/**
45+
* Applies a generative remove effect to the asset.
46+
*
47+
* @param string|array $prompt Use natural language to describe what you want to affect in the image.
48+
* @param string|array $region Remove items from the specified region(s).
49+
* @param bool $detectMultiple Whether to detect all instances of the prompt in the image.
50+
* @param bool $removeShadow Whether to remove shadows and reflections.
51+
*
52+
* @return GenerativeRemove
53+
*/
54+
public static function generativeRemove(
55+
$prompt = null,
56+
$region = null,
57+
$detectMultiple = null,
58+
$removeShadow = null
59+
) {
60+
return new GenerativeRemove($prompt, $region, $detectMultiple, $removeShadow);
61+
}
62+
63+
/**
64+
* Applies a generative replacement effect to the asset.
65+
*
66+
* @param string $fromPrompt Use natural language to describe what you want to replace.
67+
* @param string $toPrompt Use natural language to describe the replacement.
68+
* @param bool $preserveGeometry Whether to maintain the shape of the object you're replacing.
69+
* @param bool $detectMultiple Whether to detect all instances of the prompt in the image.
70+
*
71+
* @return GenerativeReplace
72+
*/
73+
public static function generativeReplace($fromPrompt, $toPrompt, $preserveGeometry = null, $detectMultiple = null)
74+
{
75+
return new GenerativeReplace($fromPrompt, $toPrompt, $preserveGeometry, $detectMultiple);
76+
}
77+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\StringUtils;
14+
use Cloudinary\Transformation\Argument\ColorValue;
15+
16+
/**
17+
* Class GenerativeRecolor
18+
*/
19+
class GenerativeRecolor extends GenerativeEffectAction
20+
{
21+
use PromptTrait;
22+
use DetectMultipleTrait;
23+
24+
const TO_COLOR = 'to-color';
25+
26+
/**
27+
* GenerativeRecolor constructor.
28+
*
29+
* @param string|array $prompt Use natural language to describe what you want to affect in the image.
30+
* @param string $toColor The target color.
31+
* @param bool $detectMultiple Whether to detect all instances of the prompt in the image.
32+
* @param mixed ...$args
33+
*/
34+
public function __construct($prompt, $toColor, $detectMultiple = null, ...$args)
35+
{
36+
parent::__construct(GenerativeEffect::GENERATIVE_RECOLOR, ...$args);
37+
38+
$this->prompt($prompt);
39+
$this->toColor($toColor);
40+
$this->detectMultiple($detectMultiple);
41+
}
42+
43+
/**
44+
* Sets the target color.
45+
*
46+
* @param string|ColorValue $toColor The HTML name or RGB/A hex code of the target color.
47+
*
48+
* @return $this
49+
*/
50+
public function toColor($toColor)
51+
{
52+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(
53+
self::TO_COLOR,
54+
StringUtils::truncatePrefix((string)$toColor, '#')
55+
);
56+
57+
return $this;
58+
}
59+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\StringUtils;
15+
use Cloudinary\Transformation\Argument\ColorValue;
16+
use Cloudinary\TransformationUtils;
17+
18+
/**
19+
* Class GenerativeRemove
20+
*/
21+
class GenerativeRemove extends GenerativeEffectAction
22+
{
23+
use PromptTrait;
24+
use DetectMultipleTrait;
25+
26+
const REGION = 'region';
27+
const REMOVE_SHADOW = 'remove-shadow';
28+
29+
/**
30+
* GenerativeRemove constructor.
31+
*
32+
* @param string|array $prompt Use natural language to describe what you want to affect in the image.
33+
* @param string|array $region Remove items from the specified region(s).
34+
* @param bool $detectMultiple Whether to detect all instances of the prompt in the image.
35+
* @param bool $removeShadow Whether to remove shadows and reflections.
36+
* @param mixed ...$args
37+
*/
38+
public function __construct($prompt = null, $region = null, $detectMultiple = null, $removeShadow = null, ...$args)
39+
{
40+
parent::__construct(GenerativeEffect::GENERATIVE_REMOVE, ...$args);
41+
42+
$this->prompt($prompt);
43+
$this->region($region);
44+
$this->detectMultiple($detectMultiple);
45+
$this->removeShadow($removeShadow);
46+
}
47+
48+
/**
49+
* Sets the target region.
50+
*
51+
* @param $region
52+
*
53+
* @return $this
54+
*/
55+
public function region(...$region)
56+
{
57+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(
58+
self::REGION,
59+
new FullListQualifierMultiValue(
60+
...ArrayUtils::build($region)
61+
)
62+
);
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* Whether to remove the shadow in addition to the object(s).
69+
*
70+
* @param bool $removeShadow Whether to remove shadow.
71+
*
72+
* @return $this
73+
*/
74+
public function removeShadow($removeShadow = true)
75+
{
76+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(
77+
self::REMOVE_SHADOW,
78+
TransformationUtils::boolToString($removeShadow)
79+
);
80+
81+
return $this;
82+
}
83+
}

0 commit comments

Comments
 (0)