1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Conversion; |
4
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\Exceptions\InvalidConversionParameter; |
6
|
|
|
|
7
|
|
|
class Conversion |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string name |
11
|
|
|
*/ |
12
|
|
|
protected $name = ''; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $extractVideoFrameAtSecond = 0; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $manipulations = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $performOnCollections = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $performOnQueue = true; |
33
|
|
|
|
34
|
|
|
public function __construct(string $name) |
35
|
|
|
{ |
36
|
|
|
$this->name = $name; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function create(string $name) |
40
|
|
|
{ |
41
|
|
|
return new static($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getName() : string |
45
|
|
|
{ |
46
|
|
|
return $this->name; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* Set the timecode in seconds to extract a video thumbnail. |
51
|
|
|
* Only used on video media. |
52
|
|
|
*/ |
53
|
|
|
public function setExtractVideoFrameAtSecond(int $timecode) : Conversion |
54
|
|
|
{ |
55
|
|
|
$this->extractVideoFrameAtSecond = $timecode; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getExtractVideoFrameAtSecond(): int |
61
|
|
|
{ |
62
|
|
|
return $this->extractVideoFrameAtSecond; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the manipulations of this conversion. |
67
|
|
|
*/ |
68
|
|
|
public function getManipulations() : array |
69
|
|
|
{ |
70
|
|
|
$manipulations = $this->manipulations; |
71
|
|
|
|
72
|
|
|
//if format not is specified, create a jpg |
73
|
|
|
if (count($manipulations) && !$this->containsFormatManipulation($manipulations)) { |
74
|
|
|
$manipulations[0]['fm'] = 'jpg'; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
return $manipulations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the manipulations for this conversion. |
82
|
|
|
* |
83
|
|
|
* @param $manipulations |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function setManipulations(...$manipulations) |
88
|
|
|
{ |
89
|
|
|
$this->manipulations = $manipulations; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add the given manipulation as the first manipulation. |
96
|
|
|
* |
97
|
|
|
* @param array $manipulation |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function addAsFirstManipulation(array $manipulation) |
102
|
|
|
{ |
103
|
|
|
array_unshift($this->manipulations, $manipulation); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the collection names on which this conversion must be performed. |
110
|
|
|
* |
111
|
|
|
* @param array $collectionNames |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function performOnCollections(...$collectionNames) |
116
|
|
|
{ |
117
|
|
|
$this->performOnCollections = $collectionNames; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* Determine if this conversion should be performed on the given |
124
|
|
|
* collection. |
125
|
|
|
*/ |
126
|
|
|
public function shouldBePerformedOn(string $collectionName) : bool |
127
|
|
|
{ |
128
|
|
|
//if no collections were specified, perform conversion on all collections |
129
|
|
|
if (!count($this->performOnCollections)) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (in_array('*', $this->performOnCollections)) { |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return in_array($collectionName, $this->performOnCollections); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Mark this conversion as one that should be queued. |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function queued() |
146
|
|
|
{ |
147
|
|
|
$this->performOnQueue = true; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Mark this conversion as one that should not be queued. |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function nonQueued() |
158
|
|
|
{ |
159
|
|
|
$this->performOnQueue = false; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* Determine if the conversion should be queued. |
166
|
|
|
*/ |
167
|
|
|
public function shouldBeQueued() : bool |
168
|
|
|
{ |
169
|
|
|
return $this->performOnQueue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/* |
173
|
|
|
* Get the extension that the result of this conversion must have. |
174
|
|
|
*/ |
175
|
|
|
public function getResultExtension(string $originalFileExtension = '') : string |
176
|
|
|
{ |
177
|
|
|
return array_reduce($this->getManipulations(), function ($carry, array $manipulation) { |
178
|
|
|
|
179
|
|
|
if (isset($manipulation['fm'])) { |
180
|
|
|
$keepExtensions = ['jpg', 'jpeg', 'png', 'gif']; |
181
|
|
|
|
182
|
|
|
return ($manipulation['fm'] === 'src' && in_array($carry, $keepExtensions)) ? $carry : $manipulation['fm']; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $carry; |
186
|
|
|
|
187
|
|
|
}, $originalFileExtension); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/* |
191
|
|
|
* Determine if the given manipulations contain a format manipulation. |
192
|
|
|
*/ |
193
|
|
|
protected function containsFormatManipulation(array $manipulations) : bool |
194
|
|
|
{ |
195
|
|
|
foreach ($manipulations as $manipulation) { |
196
|
|
|
if (array_key_exists('fm', $manipulation)) { |
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set the target width. |
206
|
|
|
* Matches with Glide's 'w'-parameter. |
207
|
|
|
* |
208
|
|
|
* @param int $width |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
|
|
* |
212
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversionParameter |
213
|
|
|
*/ |
214
|
|
|
public function setWidth(int $width) |
215
|
|
|
{ |
216
|
|
|
if ($width < 1) { |
217
|
|
|
throw InvalidConversionParameter::invalidWidth(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->setManipulationParameter('w', $width); |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set the target height. |
227
|
|
|
* Matches with Glide's 'h'-parameter. |
228
|
|
|
* |
229
|
|
|
* @param int $height |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
* |
233
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversionParameter |
234
|
|
|
*/ |
235
|
|
|
public function setHeight(int $height) |
236
|
|
|
{ |
237
|
|
|
if ($height < 1) { |
238
|
|
|
throw InvalidConversionParameter::invalidHeight(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->setManipulationParameter('h', $height); |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set the target format. |
248
|
|
|
* Matches with Glide's 'fm'-parameter. |
249
|
|
|
* |
250
|
|
|
* @param string $format |
251
|
|
|
* |
252
|
|
|
* @return $this |
253
|
|
|
* |
254
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversionParameter |
255
|
|
|
*/ |
256
|
|
|
public function setFormat(string $format) |
257
|
|
|
{ |
258
|
|
|
$validFormats = ['jpg', 'png', 'gif', 'src']; |
259
|
|
|
|
260
|
|
|
if (!in_array($format, $validFormats)) { |
261
|
|
|
throw InvalidConversionParameter::invalidFormat($format, $validFormats); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$this->setManipulationParameter('fm', $format); |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set the target fit. |
271
|
|
|
* Matches with Glide's 'fit'-parameter. |
272
|
|
|
* |
273
|
|
|
* @param string $fit |
274
|
|
|
* |
275
|
|
|
* @return $this |
276
|
|
|
* |
277
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversionParameter |
278
|
|
|
*/ |
279
|
|
|
public function setFit(string $fit) |
280
|
|
|
{ |
281
|
|
|
$validFits = [ |
282
|
|
|
'contain', |
283
|
|
|
'max', |
284
|
|
|
'fill', |
285
|
|
|
'stretch', |
286
|
|
|
'crop', |
287
|
|
|
'crop-top-left', |
288
|
|
|
'crop-top', |
289
|
|
|
'crop-top-right', |
290
|
|
|
'crop-left', |
291
|
|
|
'crop-center', |
292
|
|
|
'crop-right', |
293
|
|
|
'crop-bottom-left', |
294
|
|
|
'crop-bottom', |
295
|
|
|
'crop-bottom-right', |
296
|
|
|
]; |
297
|
|
|
|
298
|
|
|
if (!in_array($fit, $validFits)) { |
299
|
|
|
throw InvalidConversionParameter::invalidFit($fit, $validFits); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$this->setManipulationParameter('fit', $fit); |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Crops the image to specific dimensions prior to any other resize operations. |
309
|
|
|
* |
310
|
|
|
* @param int $width |
311
|
|
|
* @param int $height |
312
|
|
|
* @param int $x |
313
|
|
|
* @param int $y |
314
|
|
|
* |
315
|
|
|
* @return $this |
316
|
|
|
* |
317
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversionParameter |
318
|
|
|
*/ |
319
|
|
|
public function setCrop(int $width, int $height, int $x, int $y) |
320
|
|
|
{ |
321
|
|
|
foreach (compact('width', 'height') as $name => $value) { |
322
|
|
|
if ($value < 1) { |
323
|
|
|
throw InvalidConversionParameter::shouldBeGreaterThanOne($name, $value); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$this->setManipulationParameter('crop', implode(',', [$width, $height, $x, $y])); |
328
|
|
|
|
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set the manipulation parameter. |
334
|
|
|
* |
335
|
|
|
* @param string $name |
336
|
|
|
* @param string $value |
337
|
|
|
* |
338
|
|
|
* @return $this |
339
|
|
|
*/ |
340
|
|
|
public function setManipulationParameter(string $name, string $value) |
341
|
|
|
{ |
342
|
|
|
$manipulation = array_pop($this->manipulations) ?: []; |
343
|
|
|
|
344
|
|
|
$this->manipulations[] = array_merge($manipulation, [$name => $value]); |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|