|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use Spatie\Image\Exceptions\InvalidManipulation; |
|
7
|
|
|
|
|
8
|
|
|
class Manipulations |
|
9
|
|
|
{ |
|
10
|
|
|
const CROP_TOP_LEFT = 'crop-top-left'; |
|
11
|
|
|
const CROP_TOP = 'crop-top'; |
|
12
|
|
|
const CROP_TOP_RIGHT = 'crop-top-right'; |
|
13
|
|
|
const CROP_LEFT = 'crop-left'; |
|
14
|
|
|
const CROP_CENTER = 'crop-center'; |
|
15
|
|
|
const CROP_RIGHT = 'crop-right'; |
|
16
|
|
|
const CROP_BOTTOM_LEFT = 'crop-bottom-left'; |
|
17
|
|
|
const CROP_BOTTOM = 'crop-bottom'; |
|
18
|
|
|
const CROP_BOTTOM_RIGHT = 'crop-bottom-right'; |
|
19
|
|
|
|
|
20
|
|
|
const ORIENTATION_AUTO = 'auto'; |
|
21
|
|
|
const ORIENTATION_90 = 90; |
|
22
|
|
|
const ORIENTATION_180 = 180; |
|
23
|
|
|
const ORIENTATION_270 = 270; |
|
24
|
|
|
|
|
25
|
|
|
const FIT_CONTAIN = 'contain'; |
|
26
|
|
|
const FIT_MAX = 'max'; |
|
27
|
|
|
const FIT_FILL = 'fill'; |
|
28
|
|
|
const FIT_STRETCH = 'stretch'; |
|
29
|
|
|
const FIT_CROP = 'crop'; |
|
30
|
|
|
|
|
31
|
|
|
const BORDER_OVERLAY = 'overlay'; |
|
32
|
|
|
const BORDER_SHRINK = 'shrink'; |
|
33
|
|
|
const BORDER_EXPAND = 'expand'; |
|
34
|
|
|
|
|
35
|
|
|
const FORMAT_JPG = 'jpg'; |
|
36
|
|
|
const FORMAT_PJPG = 'pjpg'; |
|
37
|
|
|
const FORMAT_PNG = 'png'; |
|
38
|
|
|
const FORMAT_GIF = 'gif'; |
|
39
|
|
|
|
|
40
|
|
|
/** @var \Spatie\Image\ManipulationSequence */ |
|
41
|
|
|
protected $manipulationSequence; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(array $manipulations = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->manipulationSequence = new ManipulationSequence($manipulations); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $orientation |
|
50
|
|
|
* @return static |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Spatie\Image\Exceptions\InvalidManipulation |
|
53
|
|
|
*/ |
|
54
|
|
|
public function orientation(string $orientation) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$this->classHasConstantValue($orientation, 'orientation')) { |
|
57
|
|
|
throw InvalidManipulation::invalidParameter( |
|
58
|
|
|
'orientation', |
|
59
|
|
|
$orientation, |
|
60
|
|
|
$this->getConstantValues('orientation') |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->addManipulation($orientation); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $cropMethod |
|
69
|
|
|
* @param int $width |
|
70
|
|
|
* @param int $height |
|
71
|
|
|
* |
|
72
|
|
|
* @return static |
|
73
|
|
|
* @throws \Spatie\Image\Exceptions\InvalidManipulation |
|
74
|
|
|
*/ |
|
75
|
|
|
public function crop(string $cropMethod, int $width, int $height) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->classHasConstantValue($cropMethod, 'crop')) { |
|
78
|
|
|
throw InvalidManipulation::invalidParameter( |
|
79
|
|
|
'cropmethod', |
|
80
|
|
|
$cropMethod, |
|
81
|
|
|
$this->getConstantValues('crop') |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($width < 0) { |
|
86
|
|
|
throw InvalidManipulation::invalidWidth($width); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($height < 0) { |
|
90
|
|
|
throw InvalidManipulation::invalidWidth($height); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
return $this |
|
95
|
|
|
->addManipulation($cropMethod, 'crop') |
|
96
|
|
|
->addManipulation($width, 'width') |
|
97
|
|
|
->addManipulation($height, 'height'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int $width |
|
102
|
|
|
* @param int $height |
|
103
|
|
|
* @param int $focalX Crop center X in percent |
|
104
|
|
|
* @param int $focalY Crop center Y in percent |
|
105
|
|
|
* |
|
106
|
|
|
* @return static |
|
107
|
|
|
*/ |
|
108
|
|
|
public function focalCrop(int $width, int $height, $focalX, $focalY) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this |
|
111
|
|
|
->addManipulation("crop-{$focalX}-{$focalY}", 'crop') |
|
112
|
|
|
->addManipulation($width, 'width') |
|
113
|
|
|
->addManipulation($height, 'height'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param int $width |
|
118
|
|
|
* @param int $height |
|
119
|
|
|
* @param int $x |
|
120
|
|
|
* @param int $y |
|
121
|
|
|
* |
|
122
|
|
|
* @return static |
|
123
|
|
|
*/ |
|
124
|
|
|
public function manualCrop(int $width, int $height, int $x, int $y) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->addManipulation("{$width},{$height},{$x},{$y}"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param int $width |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
* @throws InvalidManipulation |
|
134
|
|
|
*/ |
|
135
|
|
|
public function width(int $width) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($width < 0) { |
|
138
|
|
|
throw InvalidManipulation::invalidWidth($width); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->addManipulation($width); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param int $height |
|
146
|
|
|
* |
|
147
|
|
|
* @return static |
|
148
|
|
|
*/ |
|
149
|
|
|
public function height(int $height) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->addManipulation($height); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $fitMethod |
|
156
|
|
|
* @param int $width |
|
157
|
|
|
* @param int $height |
|
158
|
|
|
* |
|
159
|
|
|
* @return static |
|
160
|
|
|
*/ |
|
161
|
|
|
public function fit(string $fitMethod, int $width, int $height) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this |
|
164
|
|
|
->addManipulation($fitMethod, 'fit') |
|
165
|
|
|
->addManipulation($width, 'width') |
|
166
|
|
|
->addManipulation($height, 'height'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param int $ratio |
|
171
|
|
|
* |
|
172
|
|
|
* @return static |
|
173
|
|
|
*/ |
|
174
|
|
|
public function devicePixelRatio(int $ratio) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->addManipulation($ratio); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param int $brightness |
|
181
|
|
|
* |
|
182
|
|
|
* @return static |
|
183
|
|
|
*/ |
|
184
|
|
|
public function brightness(int $brightness) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->addManipulation($brightness); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param float $gamma |
|
191
|
|
|
* |
|
192
|
|
|
* @return static |
|
193
|
|
|
*/ |
|
194
|
|
|
public function gamma(float $gamma) |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->addManipulation($gamma); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param int $contrast |
|
201
|
|
|
* |
|
202
|
|
|
* @return static |
|
203
|
|
|
*/ |
|
204
|
|
|
public function contrast(int $contrast) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->addManipulation($contrast); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param int $sharpen |
|
211
|
|
|
* |
|
212
|
|
|
* @return static |
|
213
|
|
|
*/ |
|
214
|
|
|
public function sharpen(int $sharpen) |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->addManipulation($sharpen); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param int $blur |
|
221
|
|
|
* |
|
222
|
|
|
* @return static |
|
223
|
|
|
*/ |
|
224
|
|
|
public function blur(int $blur) |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->addManipulation($blur); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param int $pixelate |
|
231
|
|
|
* |
|
232
|
|
|
* @return static |
|
233
|
|
|
*/ |
|
234
|
|
|
public function pixelate(int $pixelate) |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->addManipulation($pixelate); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return static |
|
241
|
|
|
*/ |
|
242
|
|
|
public function greyscale() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->filter('greyscale'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return static |
|
249
|
|
|
*/ |
|
250
|
|
|
public function sepia() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->filter('sepia'); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param string $colorName |
|
257
|
|
|
* |
|
258
|
|
|
* @return static |
|
259
|
|
|
*/ |
|
260
|
|
|
public function background(string $colorName) |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->addManipulation($colorName); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param int $width |
|
267
|
|
|
* @param string $color |
|
268
|
|
|
* @param string $borderType |
|
269
|
|
|
* |
|
270
|
|
|
* @return static |
|
271
|
|
|
*/ |
|
272
|
|
|
public function border(int $width, string $color, string $borderType = 'overlay') |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->addManipulation("{$width},{$color},{$borderType}", 'border'); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param int $quality |
|
279
|
|
|
* |
|
280
|
|
|
* @return static |
|
281
|
|
|
*/ |
|
282
|
|
|
public function quality(int $quality) |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->addManipulation($quality); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param string $format |
|
289
|
|
|
* |
|
290
|
|
|
* @return static |
|
291
|
|
|
*/ |
|
292
|
|
|
public function format(string $format) |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->addManipulation($format); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param string $filterName |
|
299
|
|
|
* |
|
300
|
|
|
* @return static |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function filter(string $filterName) |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->addManipulation($filterName); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return static |
|
309
|
|
|
*/ |
|
310
|
|
|
public function apply() |
|
311
|
|
|
{ |
|
312
|
|
|
$this->manipulationSequence->startNewGroup(); |
|
313
|
|
|
|
|
314
|
|
|
return $this; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function removeManipulation(string $name) |
|
318
|
|
|
{ |
|
319
|
|
|
$this->manipulationSequence->removeManipulation($name); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function hasManipulation(string $manipulationName): bool |
|
323
|
|
|
{ |
|
324
|
|
|
return !is_null($this->getManipulationArgument($manipulationName)); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @param string $manipulationName |
|
329
|
|
|
* |
|
330
|
|
|
* @return string|null |
|
331
|
|
|
*/ |
|
332
|
|
|
public function getManipulationArgument(string $manipulationName) |
|
333
|
|
|
{ |
|
334
|
|
|
foreach ($this->manipulationSequence->getGroups() as $manipulationSet) { |
|
335
|
|
|
if (array_key_exists($manipulationName, $manipulationSet)) { |
|
336
|
|
|
return $manipulationSet[$manipulationName]; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
protected function addManipulation(string $manipulationArgument, string $manipulationName = null) |
|
342
|
|
|
{ |
|
343
|
|
|
$manipulationName = $manipulationName ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; |
|
344
|
|
|
|
|
345
|
|
|
$this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument); |
|
346
|
|
|
|
|
347
|
|
|
return $this; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
public function mergeManipulations(Manipulations $manipulations) |
|
351
|
|
|
{ |
|
352
|
|
|
$this->manipulationSequence->merge($manipulations->manipulationSequence); |
|
353
|
|
|
|
|
354
|
|
|
return $this; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
public function getManipulationSequence(): ManipulationSequence |
|
358
|
|
|
{ |
|
359
|
|
|
return $this->manipulationSequence; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
protected function classHasConstantValue(string $value, string $constantNamePrefix): bool |
|
363
|
|
|
{ |
|
364
|
|
|
return in_array($value, $this->getConstantValues($constantNamePrefix)); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
protected function getConstantValues(string $namePrefix): array |
|
368
|
|
|
{ |
|
369
|
|
|
$allConstants = (new ReflectionClass(static::class))->getConstants(); |
|
370
|
|
|
|
|
371
|
|
|
return array_filter($allConstants, function($constantValue, $constantName) use ($namePrefix) { |
|
372
|
|
|
return strpos($constantName, strtoupper($namePrefix)) === 0; |
|
373
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|