1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Drivers; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use FFMpeg\Coordinate\Dimension; |
7
|
|
|
use FFMpeg\Filters\Audio\SimpleFilter; |
8
|
|
|
use FFMpeg\Filters\FilterInterface; |
9
|
|
|
use FFMpeg\Filters\Video\ResizeFilter; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter; |
12
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\LegacyFilterMapping; |
13
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media; |
14
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filters\WatermarkFactory; |
15
|
|
|
|
16
|
|
|
trait InteractsWithFilters |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Support\Collection |
20
|
|
|
*/ |
21
|
|
|
protected $pendingComplexFilters; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns an array with the filters applied to the underlying media object. |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getFilters(): array |
29
|
|
|
{ |
30
|
|
|
return iterator_to_array($this->media->getFiltersCollection()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Helper method to provide multiple ways to add a filter to the underlying |
35
|
|
|
* media object. |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
|
|
public function addFilter(): self |
40
|
|
|
{ |
41
|
|
|
$arguments = func_get_args(); |
42
|
|
|
|
43
|
|
|
// to support '[in]filter[out]' complex filters |
44
|
|
|
if ($this->isAdvancedMedia() && count($arguments) === 3) { |
|
|
|
|
45
|
|
|
$this->media->filters()->custom(...$arguments); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// use a callback to add a filter |
51
|
|
|
if ($arguments[0] instanceof Closure) { |
52
|
|
|
call_user_func_array($arguments[0], [$this->media->filters()]); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// use an object to add a filter |
58
|
|
|
if ($arguments[0] instanceof FilterInterface) { |
59
|
|
|
call_user_func_array([$this->media, 'addFilter'], $arguments); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// use a single array with parameters to define a filter |
65
|
|
|
if (is_array($arguments[0])) { |
66
|
|
|
$this->media->addFilter(new SimpleFilter($arguments[0])); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// use all function arguments as a filter |
72
|
|
|
$this->media->addFilter(new SimpleFilter($arguments)); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Calls the callable with a WatermarkFactory instance and |
79
|
|
|
* adds the freshly generated WatermarkFilter. |
80
|
|
|
* |
81
|
|
|
* @param callable $withWatermarkFactory |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
|
|
public function addWatermark(callable $withWatermarkFactory): self |
85
|
|
|
{ |
86
|
|
|
$withWatermarkFactory( |
87
|
|
|
$watermarkFactory = new WatermarkFactory |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
return $this->addFilter($watermarkFactory->get()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Shortcut for adding a Resize filter. |
95
|
|
|
* |
96
|
|
|
* @param int $width |
97
|
|
|
* @param int $height |
98
|
|
|
* @param string $mode |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
|
|
public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT): self |
102
|
|
|
{ |
103
|
|
|
$dimension = new Dimension($width, $height); |
104
|
|
|
|
105
|
|
|
$filter = new ResizeFilter($dimension, $mode); |
106
|
|
|
|
107
|
|
|
return $this->addFilter($filter); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Maps the arguments into a 'LegacyFilterMapping' instance and |
112
|
|
|
* pushed it to the 'pendingComplexFilters' collection. These |
113
|
|
|
* filters will be applied later on by the MediaExporter. |
114
|
|
|
*/ |
115
|
|
|
public function addFilterAsComplexFilter($in, $out, ...$arguments): self |
116
|
|
|
{ |
117
|
|
|
$this->pendingComplexFilters->push(new LegacyFilterMapping( |
118
|
|
|
$in, |
119
|
|
|
$out, |
120
|
|
|
...$arguments |
121
|
|
|
)); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Getter for the pending complex filters. |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Support\Collection |
130
|
|
|
*/ |
131
|
|
|
public function getPendingComplexFilters(): Collection |
132
|
|
|
{ |
133
|
|
|
return $this->pendingComplexFilters; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|