|
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
|
|
|
* @param boolean $forceStandards |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
|
|
public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT, $forceStandards = true): self |
|
103
|
|
|
{ |
|
104
|
|
|
$dimension = new Dimension($width, $height); |
|
105
|
|
|
|
|
106
|
|
|
$filter = new ResizeFilter($dimension, $mode, $forceStandards); |
|
107
|
|
|
|
|
108
|
|
|
return $this->addFilter($filter); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Maps the arguments into a 'LegacyFilterMapping' instance and |
|
113
|
|
|
* pushed it to the 'pendingComplexFilters' collection. These |
|
114
|
|
|
* filters will be applied later on by the MediaExporter. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function addFilterAsComplexFilter($in, $out, ...$arguments): self |
|
117
|
|
|
{ |
|
118
|
|
|
$this->pendingComplexFilters->push(new LegacyFilterMapping( |
|
119
|
|
|
$in, |
|
120
|
|
|
$out, |
|
121
|
|
|
...$arguments |
|
122
|
|
|
)); |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Getter for the pending complex filters. |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Illuminate\Support\Collection |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getPendingComplexFilters(): Collection |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->pendingComplexFilters; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|