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