1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Conversion; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Spatie\Image\Manipulations; |
7
|
|
|
|
8
|
|
|
/** @mixin \Spatie\Image\Manipulations */ |
9
|
|
|
class Conversion |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $name = ''; |
13
|
|
|
|
14
|
|
|
/** @var int */ |
15
|
|
|
protected $extractVideoFrameAtSecond = 0; |
16
|
|
|
|
17
|
|
|
/** @var \Spatie\Image\Manipulations */ |
18
|
|
|
protected $manipulations; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $performOnCollections = []; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
protected $performOnQueue = true; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $keepOriginalImageFormat = false; |
28
|
|
|
|
29
|
|
|
public function __construct(string $name) |
30
|
|
|
{ |
31
|
|
|
$this->name = $name; |
32
|
|
|
|
33
|
|
|
$this->manipulations = (new Manipulations()) |
34
|
|
|
->optimize(config('medialibrary.image_optimizers')) |
35
|
|
|
->format('jpg'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function create(string $name) |
39
|
|
|
{ |
40
|
|
|
return new static($name); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getName(): string |
44
|
|
|
{ |
45
|
|
|
return $this->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
* Set the timecode in seconds to extract a video thumbnail. |
50
|
|
|
* Only used on video media. |
51
|
|
|
*/ |
52
|
|
|
public function extractVideoFrameAtSecond(int $timecode): Conversion |
53
|
|
|
{ |
54
|
|
|
$this->extractVideoFrameAtSecond = $timecode; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getExtractVideoFrameAtSecond(): int |
60
|
|
|
{ |
61
|
|
|
return $this->extractVideoFrameAtSecond; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function keepOriginalImageFormat(): Conversion |
65
|
|
|
{ |
66
|
|
|
$this->keepOriginalImageFormat = true; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function shouldKeepOriginalImageFormat(): Bool |
72
|
|
|
{ |
73
|
|
|
return $this->keepOriginalImageFormat; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getManipulations(): Manipulations |
77
|
|
|
{ |
78
|
|
|
return $this->manipulations; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function removeManipulation(string $manipulationName) |
82
|
|
|
{ |
83
|
|
|
$this->manipulations->removeManipulation($manipulationName); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __call($name, $arguments) |
89
|
|
|
{ |
90
|
|
|
if (! method_exists($this->manipulations, $name)) { |
91
|
|
|
throw new BadMethodCallException("Manipulation `{$name}` does not exist"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->manipulations->$name(...$arguments); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the manipulations for this conversion. |
101
|
|
|
* |
102
|
|
|
* @param \Spatie\Image\Manipulations|closure $manipulations |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setManipulations($manipulations) |
107
|
|
|
{ |
108
|
|
|
if ($manipulations instanceof Manipulations) { |
109
|
|
|
$this->manipulations = $this->manipulations->mergeManipulations($manipulations); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (is_callable($manipulations)) { |
113
|
|
|
$manipulations($this->manipulations); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add the given manipulations as the first ones. |
121
|
|
|
* |
122
|
|
|
* @param \Spatie\Image\Manipulations $manipulations |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function addAsFirstManipulations(Manipulations $manipulations) |
127
|
|
|
{ |
128
|
|
|
$manipulationSequence = $manipulations->getManipulationSequence()->toArray(); |
129
|
|
|
|
130
|
|
|
$this->manipulations |
131
|
|
|
->getManipulationSequence() |
132
|
|
|
->mergeArray($manipulationSequence); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the collection names on which this conversion must be performed. |
139
|
|
|
* |
140
|
|
|
* @param $collectionNames |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function performOnCollections(...$collectionNames) |
145
|
|
|
{ |
146
|
|
|
$this->performOnCollections = $collectionNames; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/* |
152
|
|
|
* Determine if this conversion should be performed on the given |
153
|
|
|
* collection. |
154
|
|
|
*/ |
155
|
|
|
public function shouldBePerformedOn(string $collectionName): bool |
156
|
|
|
{ |
157
|
|
|
//if no collections were specified, perform conversion on all collections |
158
|
|
|
if (! count($this->performOnCollections)) { |
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (in_array('*', $this->performOnCollections)) { |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return in_array($collectionName, $this->performOnCollections); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Mark this conversion as one that should be queued. |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function queued() |
175
|
|
|
{ |
176
|
|
|
$this->performOnQueue = true; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Mark this conversion as one that should not be queued. |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function nonQueued() |
187
|
|
|
{ |
188
|
|
|
$this->performOnQueue = false; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Avoid optimization of the converted image. |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function nonOptimized() |
199
|
|
|
{ |
200
|
|
|
$this->removeManipulation('optimize'); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/* |
206
|
|
|
* Determine if the conversion should be queued. |
207
|
|
|
*/ |
208
|
|
|
public function shouldBeQueued(): bool |
209
|
|
|
{ |
210
|
|
|
return $this->performOnQueue; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/* |
214
|
|
|
* Get the extension that the result of this conversion must have. |
215
|
|
|
*/ |
216
|
|
|
public function getResultExtension(string $originalFileExtension = ''): string |
217
|
|
|
{ |
218
|
|
|
if ($this->shouldKeepOriginalImageFormat()) { |
219
|
|
|
return $originalFileExtension; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) { |
223
|
|
|
return $manipulationArgument; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $originalFileExtension; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|