Skip to content

Commit 5a3cfbf

Browse files
Add support for BackgroundRemoval effect
1 parent 7e6fd25 commit 5a3cfbf

File tree

8 files changed

+492
-11
lines changed

8 files changed

+492
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
15+
* Class ListEffectQualifier
16+
*/
17+
class ListEffectQualifier extends EffectQualifier
18+
{
19+
const PROPERTIES = 'properties';
20+
21+
/**
22+
* ListEffectQualifier constructor.
23+
*
24+
* @param string $effectName The name of the effect.
25+
* @param mixed ...$values The effect values.
26+
*/
27+
public function __construct($effectName, ...$values)
28+
{
29+
parent::__construct($effectName);
30+
31+
$this->getValue()->setSimpleValue(self::PROPERTIES, new ListExpressionQualifierMultiValue(...$values));
32+
}
33+
34+
/**
35+
* @return mixed
36+
*/
37+
public function getPropertiesValue()
38+
{
39+
return parent::getValue()->getSimpleValue(self::PROPERTIES);
40+
}
41+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 BackgroundRemoval
15+
*
16+
* Uses the Cloudinary AI Background Removal add-on to make the background of an image transparent.
17+
*
18+
*
19+
* **Learn more**: <a
20+
* href="https://cloudinary.com/documentation/transformation_reference#e_background_removal" target="_blank">
21+
* Background removal</a>
22+
*
23+
* @api
24+
*/
25+
class BackgroundRemoval extends EffectAction
26+
{
27+
const FINE_EDGES = 'fineedges';
28+
const HINTS = 'hints';
29+
30+
/**
31+
* RemoveBackground constructor.
32+
*
33+
* @param bool|null $fineEdges Enables detailed background removal around a foreground object with fine
34+
* detail around its edges.
35+
* @param array $hints A list of foreground objects to keep.
36+
*/
37+
public function __construct($fineEdges = null, $hints = [])
38+
{
39+
parent::__construct(new ListEffectQualifier(PixelEffect::BACKGROUND_REMOVAL));
40+
41+
$this->fineEdges($fineEdges);
42+
$this->hints($hints);
43+
}
44+
45+
/**
46+
* Provides better results if you know that the object has more clear-cut edges.
47+
*
48+
* @param bool|null $fineEdges Whether to apply fine edges mode.
49+
*
50+
* @return BackgroundRemoval
51+
*/
52+
public function fineEdges($fineEdges = true)
53+
{
54+
$value = $fineEdges === true ? 'y' : ($fineEdges === false ? 'n' : $fineEdges);
55+
56+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(self::FINE_EDGES, $value);
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Sets a list of foreground objects to keep.
63+
*
64+
* @param array $foregroundObjects A list of foreground objects to keep.
65+
*
66+
* @return BackgroundRemoval
67+
*/
68+
public function hints(...$foregroundObjects)
69+
{
70+
$this->getMainQualifier()->getPropertiesValue()->setSimpleNamedValue(
71+
self::HINTS,
72+
new FullListExpressionQualifierMultiValue(
73+
...$foregroundObjects
74+
)
75+
);
76+
77+
return $this;
78+
}
79+
}

src/Transformation/Effect/Pixel/ImagePixelEffectTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ public static function removeBackground($screen = false, $colorToRemove = null)
6868
return new RemoveBackground($screen, $colorToRemove);
6969
}
7070

71+
/**
72+
* Uses the Cloudinary AI Background Removal add-on to make the background of an image transparent.
73+
*
74+
*
75+
* @param bool|null $fineEdges Enables detailed background removal around a foreground object with fine
76+
* detail around its edges.
77+
* @param array $hints A list of foreground objects to keep.
78+
*
79+
* @return BackgroundRemoval
80+
*
81+
* @see \Cloudinary\Transformation\BackgroundRemoval
82+
*/
83+
public static function backgroundRemoval($fineEdges = null, $hints = [])
84+
{
85+
return new BackgroundRemoval($fineEdges, $hints);
86+
}
87+
7188
/**
7289
* Applies an ordered dither filter to the image.
7390
*

src/Transformation/Effect/Pixel/PixelEffect.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ abstract class PixelEffect
2020
const VIGNETTE = 'vignette';
2121

2222
// Image
23-
const BLUR_FACES = 'blur_faces';
24-
const BLUR_REGION = 'blur_region';
25-
const PIXELATE = 'pixelate';
26-
const PIXELATE_REGION = 'pixelate_region';
27-
const PIXELATE_FACES = 'pixelate_faces';
28-
const ORDERED_DITHER = 'ordered_dither';
29-
const GRADIENT_FADE = 'gradient_fade';
30-
const MAKE_TRANSPARENT = 'make_transparent';
31-
const REMOVE_BACKGROUND = 'bgremoval';
32-
const CUT_OUT = 'cut_out';
23+
const BLUR_FACES = 'blur_faces';
24+
const BLUR_REGION = 'blur_region';
25+
const PIXELATE = 'pixelate';
26+
const PIXELATE_REGION = 'pixelate_region';
27+
const PIXELATE_FACES = 'pixelate_faces';
28+
const ORDERED_DITHER = 'ordered_dither';
29+
const GRADIENT_FADE = 'gradient_fade';
30+
const MAKE_TRANSPARENT = 'make_transparent';
31+
const REMOVE_BACKGROUND = 'bgremoval';
32+
const BACKGROUND_REMOVAL = 'background_removal';
33+
const CUT_OUT = 'cut_out';
3334

3435
use PixelEffectTrait;
3536
use ImagePixelEffectTrait;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* Defines the available foreground objects.
15+
*
16+
* @api
17+
*/
18+
class ForegroundObject extends BaseArgument
19+
{
20+
const AIRPLANE = 'airplane';
21+
const BUS = 'bus';
22+
const DINING_TABLE = 'dining_table';
23+
const SHEEP = 'sheep';
24+
const BICYCLE = 'bicycle';
25+
const CAR = 'car';
26+
const DOG = 'dog';
27+
const SOFA = 'sofa';
28+
const BIRD = 'bird';
29+
const CAT = 'cat';
30+
const HORSE = 'horse';
31+
const TRAIN = 'train';
32+
const BOAT = 'boat';
33+
const CHAIR = 'chair';
34+
const PERSON = 'person';
35+
const TV = 'tv';
36+
const BOTTLE = 'bottle';
37+
const COW = 'cow';
38+
const POTTED_PLANT = 'potted_plant';
39+
const MOTORBIKE = 'motorbike';
40+
41+
use ForegroundObjectTrait;
42+
}

0 commit comments

Comments
 (0)