Skip to content

Commit 4c35d38

Browse files
Add support for audio layers
1 parent 9cf755f commit 4c35d38

17 files changed

+448
-35
lines changed

src/Transformation/Effect/Pixel/CutOut.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct($source)
5151
*
5252
* @return array
5353
*
54-
* @see BaseSourceContainer::getSubActionQualifiers
54+
* @see BasePositionalSourceContainer::getSubActionQualifiers
5555
*/
5656
protected function getSubActionQualifiers()
5757
{

src/Transformation/Image/ImageSpecificTransformationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function overlay($layer, $position = null, $blendMode = null)
4242
return $this->addAction(
4343
ClassUtils::verifyInstance(
4444
$layer,
45-
BaseSourceContainer::class,
45+
BasePositionalSourceContainer::class,
4646
ImageOverlay::class,
4747
$position,
4848
$blendMode

src/Transformation/Layer/AssetBasedSource.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
namespace Cloudinary\Transformation;
1212

13-
use Cloudinary\Asset\AssetDescriptor;
14-
use Cloudinary\Asset\BaseAsset;
15-
1613
/**
1714
* Class AssetBasedLayer
1815
*/
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\ClassUtils;
15+
16+
/**
17+
* Defines how the audio layer is applied.
18+
*
19+
* **Learn more**: <a
20+
* href="https://cloudinary.com/documentation/video_layers#audio_overlays" target="_blank">
21+
* Audio overlays</a>
22+
*
23+
* @api
24+
*/
25+
class AudioOverlay extends BaseSourceContainer
26+
{
27+
/**
28+
* @var Timeline $timeline The timeline position of the overlay.
29+
*/
30+
protected $timeline;
31+
32+
/**
33+
* BaseLayerContainer constructor.
34+
*
35+
* @param BaseSource|string $source
36+
* @param Timeline|null $timeline
37+
*/
38+
public function __construct(
39+
$source = null,
40+
$timeline = null
41+
) {
42+
parent::__construct($source);
43+
44+
$this->timeline($timeline);
45+
}
46+
47+
/**
48+
* Sets the timeline position of the overlay.
49+
*
50+
* @param Timeline|null $timeline The timeline position of the overlay.
51+
*
52+
* @return AudioOverlay
53+
*/
54+
public function timeline(Timeline $timeline = null)
55+
{
56+
$this->timeline = $timeline;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Indicates that the video should be concatenated on to the container video and not added as an overlay.
63+
*
64+
* @return $this
65+
*/
66+
protected function concatenate()
67+
{
68+
$this->source->setFlag(LayerFlag::splice());
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* Sets the source.
75+
*
76+
* @param BaseSource $source The source.
77+
*
78+
* @return static
79+
*/
80+
public function source($source)
81+
{
82+
$this->source = ClassUtils::verifyInstance($source, BaseSource::class, AudioSource::class);
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
protected function getSubActionQualifiers()
91+
{
92+
$subActionQualifiers = parent::getSubActionQualifiers();
93+
94+
$subActionQualifiers['additional'] = ArrayUtils::mergeNonEmpty(
95+
$subActionQualifiers['additional'],
96+
$this->timeline? $this->timeline->getStringQualifiers(): []
97+
);
98+
99+
return $subActionQualifiers;
100+
}
101+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\ClassUtils;
14+
15+
/**
16+
* Defines how to manipulate an audio layer.
17+
*
18+
* **Learn more**: <a
19+
* href="https://cloudinary.com/documentation/video_manipulation_and_delivery#adding_video_overlays" target="_blank">
20+
* Video overlays</a>
21+
*
22+
* @api
23+
*/
24+
class AudioSource extends AssetBasedSource
25+
{
26+
use VideoTransformationTrait; // FIXME: keep only relevant audio transformations.
27+
use AudioSourceTrait;
28+
29+
/**
30+
* AudioSource constructor.
31+
*
32+
* @param $source
33+
*/
34+
public function __construct($source)
35+
{
36+
parent::__construct(ClassUtils::verifyInstance($source, AudioSourceQualifier::class));
37+
}
38+
39+
/**
40+
* Getter for the audio (video) transformation.
41+
*
42+
* Creates a new VideoTransformation if not initialized.
43+
*
44+
* @return VideoTransformation
45+
*
46+
* @internal
47+
*/
48+
public function getTransformation()
49+
{
50+
if (! isset($this->transformation)) {
51+
$this->transformation = new VideoTransformation();
52+
}
53+
54+
return $this->transformation;
55+
}
56+
57+
/**
58+
* Getter for the layer qualifier.
59+
*
60+
* @return AudioSourceQualifier
61+
*
62+
* @internal
63+
*/
64+
protected function getSourceQualifier()
65+
{
66+
if (! isset($this->qualifiers['source'])) {
67+
$this->qualifiers['source'] = new AudioSourceQualifier(null);
68+
}
69+
70+
return $this->qualifiers['source'];
71+
}
72+
73+
74+
/**
75+
* Named constructor.
76+
*
77+
* @param BaseSource|string $source The layer source.
78+
*
79+
* @return static
80+
*/
81+
protected static function createWithSource($source)
82+
{
83+
return $source;
84+
}
85+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\ClassUtils;
14+
15+
/**
16+
* Class AudioSourceQualifier
17+
*/
18+
class AudioSourceQualifier extends BaseSourceQualifier
19+
{
20+
/**
21+
* @var string $sourceType The type of the layer.
22+
*/
23+
protected $sourceType = 'audio';
24+
25+
/**
26+
* AudioSourceQualifier constructor.
27+
*
28+
* @param $source
29+
*/
30+
public function __construct($source)
31+
{
32+
parent::__construct();
33+
34+
$this->audio($source);
35+
}
36+
37+
/**
38+
* Sets the audio source.
39+
*
40+
* @param SourceValue|string $source The audio source.
41+
*
42+
* @return $this
43+
*/
44+
public function audio($source)
45+
{
46+
$this->value->setValue(ClassUtils::verifyInstance($source, SourceValue::class));
47+
48+
return $this;
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\ClassUtils;
14+
15+
/**
16+
* Trait AudioSourceTrait
17+
*
18+
* @api
19+
*/
20+
trait AudioSourceTrait
21+
{
22+
/**
23+
* Adds another audio layer.
24+
*
25+
* @param string $audioId The public ID of the new audio layer.
26+
*
27+
* @return static|AudioSource
28+
*/
29+
public static function audio($audioId = null)
30+
{
31+
return static::createWithSource(ClassUtils::verifyInstance($audioId, AudioSource::class));
32+
}
33+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
15+
/**
16+
* Class BasePositionalSourceContainer
17+
*
18+
* This is a base class for all source (layer) containers (overlays/underlays) having position.
19+
*
20+
* @internal
21+
*/
22+
abstract class BasePositionalSourceContainer extends BaseSourceContainer
23+
{
24+
25+
/**
26+
* @var Position $position Layer position.
27+
*/
28+
protected $position;
29+
30+
/**
31+
* BaseLayerContainer constructor.
32+
*
33+
* @param BaseSource|string $source The source.
34+
* @param Position $position Layer position.
35+
*/
36+
public function __construct($source = null, $position = null)
37+
{
38+
parent::__construct();
39+
40+
$this->source($source);
41+
$this->position($position);
42+
}
43+
44+
/**
45+
* Sets the source position.
46+
*
47+
* @param Position $position The Position of the layer.
48+
*
49+
* @return static
50+
*/
51+
abstract public function position($position = null);
52+
53+
/**
54+
* Collects source based action grouped by sub-actions.
55+
*
56+
* Typical source based action consists of 2 to 3 components.
57+
*
58+
* For example, if we take:
59+
* l_logo/c_scale,w_100/e_screen,fl_layer_apply,fl_no_overflow,g_south,y_20
60+
*
61+
* We can see:
62+
* - source part (l_).
63+
* - nested transformation (optional).
64+
* - fl_layer_apply part with position, blend mode, and additional flags/qualifiers.
65+
*
66+
* Occasionally the source part(l_) has additional qualifiers/flags, they come with the source itself.
67+
*
68+
* @return array An array of grouped qualifiers
69+
*
70+
* @internal
71+
*/
72+
protected function getSubActionQualifiers()
73+
{
74+
$subActionQualifiers = parent::getSubActionQualifiers();
75+
76+
$subActionQualifiers['additional'] = ArrayUtils::mergeNonEmpty(
77+
$subActionQualifiers['additional'],
78+
$this->position ? $this->position->getStringQualifiers() : []
79+
);
80+
81+
return $subActionQualifiers;
82+
}
83+
84+
/**
85+
* Serializes to json.
86+
*
87+
* @return array
88+
*/
89+
public function jsonSerialize()
90+
{
91+
$result = parent::jsonSerialize();
92+
93+
$result['position'] = $this->position;
94+
95+
return $result;
96+
}
97+
}

0 commit comments

Comments
 (0)