|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
|
4
|
|
|
|
|
5
|
|
|
class Manipulations |
|
6
|
|
|
{ |
|
7
|
|
|
const CROP_TOP_LEFT = 'crop-top-left'; |
|
8
|
|
|
const CROP_TOP = 'crop-top'; |
|
9
|
|
|
const CROP_TOP_RIGHT = 'crop-top-right'; |
|
10
|
|
|
const CROP_LEFT = 'crop-left'; |
|
11
|
|
|
const CROP_CENTER = 'crop-center'; |
|
12
|
|
|
const CROP_RIGHT = 'crop-right'; |
|
13
|
|
|
const CROP_BOTTOM_LEFT = 'crop-bottom-left'; |
|
14
|
|
|
const CROP_BOTTOM = 'crop-bottom'; |
|
15
|
|
|
const CROP_BOTTOM_RIGHT = 'crop-bottom-right'; |
|
16
|
|
|
|
|
17
|
|
|
const ORIENTATION_AUTO = 'auto'; |
|
18
|
|
|
const ORIENTATION_90 = 90; |
|
19
|
|
|
const ORIENTATION_180 = 180; |
|
20
|
|
|
const ORIENTATION_270 = 270; |
|
21
|
|
|
|
|
22
|
|
|
const FIT_CONTAIN = 'contain'; |
|
23
|
|
|
const FIT_MAX = 'max'; |
|
24
|
|
|
const FIT_FILL = 'fill'; |
|
25
|
|
|
const FIT_STRETCH = 'stretch'; |
|
26
|
|
|
const FIT_CROP = 'crop'; |
|
27
|
|
|
|
|
28
|
|
|
const BORDER_OVERLAY = 'overlay'; |
|
29
|
|
|
const BORDER_SHRINK = 'shrink'; |
|
30
|
|
|
const BORDER_EXPAND = 'expand'; |
|
31
|
|
|
|
|
32
|
|
|
const FORMAT_JPG = 'jpg'; |
|
33
|
|
|
const FORMAT_PJPG = 'pjpg'; |
|
34
|
|
|
const FORMAT_PNG = 'png'; |
|
35
|
|
|
const FORMAT_GIF = 'gif'; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \Spatie\Image\ManipulationSequence */ |
|
38
|
|
|
protected $manipulationSequence; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(array $manipulations = []) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$this->manipulationSequence = new ManipulationSequence(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $orientation |
|
47
|
|
|
* |
|
48
|
|
|
* @return static |
|
49
|
|
|
*/ |
|
50
|
|
|
public function orientation(string $orientation) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->addManipulation($orientation); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $cropMethod |
|
57
|
|
|
* @param int $width |
|
58
|
|
|
* @param int $height |
|
59
|
|
|
* |
|
60
|
|
|
* @return static |
|
61
|
|
|
*/ |
|
62
|
|
|
public function crop(string $cropMethod, int $width, int $height) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this |
|
65
|
|
|
->addManipulation($cropMethod, 'crop') |
|
66
|
|
|
->addManipulation($width, 'width') |
|
67
|
|
|
->addManipulation($height, 'height'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param int $width |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function width(int $width) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->addManipulation($width); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param int $height |
|
82
|
|
|
* |
|
83
|
|
|
* @return static |
|
84
|
|
|
*/ |
|
85
|
|
|
public function height(int $height) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->addManipulation($height); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $fitMethod |
|
92
|
|
|
* @param int $width |
|
93
|
|
|
* @param int $height |
|
94
|
|
|
* |
|
95
|
|
|
* @return static |
|
96
|
|
|
*/ |
|
97
|
|
|
public function fit(string $fitMethod, int $width, int $height) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this |
|
100
|
|
|
->addManipulation($fitMethod, 'fit') |
|
101
|
|
|
->addManipulation($width, 'width') |
|
102
|
|
|
->addManipulation($height, 'height'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param int $ratio |
|
107
|
|
|
* |
|
108
|
|
|
* @return static |
|
109
|
|
|
*/ |
|
110
|
|
|
public function devicePixelRatio(int $ratio) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->addManipulation($ratio); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param int $brightness |
|
117
|
|
|
* |
|
118
|
|
|
* @return static |
|
119
|
|
|
*/ |
|
120
|
|
|
public function brightness(int $brightness) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->addManipulation($brightness); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param float $gamma |
|
127
|
|
|
* |
|
128
|
|
|
* @return static |
|
129
|
|
|
*/ |
|
130
|
|
|
public function gamma(float $gamma) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->addManipulation($gamma); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param int $contrast |
|
137
|
|
|
* |
|
138
|
|
|
* @return static |
|
139
|
|
|
*/ |
|
140
|
|
|
public function contrast(int $contrast) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->addManipulation($contrast); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param int $sharpen |
|
147
|
|
|
* |
|
148
|
|
|
* @return static |
|
149
|
|
|
*/ |
|
150
|
|
|
public function sharpen(int $sharpen) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->addManipulation($sharpen); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param int $blur |
|
157
|
|
|
* |
|
158
|
|
|
* @return static |
|
159
|
|
|
*/ |
|
160
|
|
|
public function blur(int $blur) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->addManipulation($blur); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param int $pixelate |
|
167
|
|
|
* |
|
168
|
|
|
* @return static |
|
169
|
|
|
*/ |
|
170
|
|
|
public function pixelate(int $pixelate) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->addManipulation($pixelate); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return static |
|
177
|
|
|
*/ |
|
178
|
|
|
public function greyscale() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->filter('greyscale'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return static |
|
185
|
|
|
*/ |
|
186
|
|
|
public function sepia() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->filter('sepia'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $colorName |
|
193
|
|
|
* |
|
194
|
|
|
* @return static |
|
195
|
|
|
*/ |
|
196
|
|
|
public function background(string $colorName) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->addManipulation($colorName); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param int $width |
|
203
|
|
|
* @param string $color |
|
204
|
|
|
* @param string $borderType |
|
205
|
|
|
* |
|
206
|
|
|
* @return static |
|
207
|
|
|
*/ |
|
208
|
|
|
public function border(int $width, string $color, string $borderType = 'overlay') |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->addManipulation(["{$width},{$color},{$borderType}"], 'border'); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param int $quality |
|
215
|
|
|
* |
|
216
|
|
|
* @return static |
|
217
|
|
|
*/ |
|
218
|
|
|
public function quality(int $quality) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->addManipulation($quality); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $format |
|
225
|
|
|
* |
|
226
|
|
|
* @return static |
|
227
|
|
|
*/ |
|
228
|
|
|
public function format(string $format) |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->addManipulation($format); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $filterName |
|
235
|
|
|
* |
|
236
|
|
|
* @return static |
|
237
|
|
|
*/ |
|
238
|
|
|
protected function filter(string $filterName) |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->addManipulation($filterName); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return static |
|
245
|
|
|
*/ |
|
246
|
|
|
public function apply() |
|
247
|
|
|
{ |
|
248
|
|
|
$this->manipulationSequence->startNewGroup(); |
|
249
|
|
|
|
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function removeManipulation(string $name) |
|
254
|
|
|
{ |
|
255
|
|
|
$this->manipulationSequence->removeManipulation($name); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
public function hasManipulation(string $manipulationName): bool |
|
260
|
|
|
{ |
|
261
|
|
|
return !is_null($this->getManipulation($manipulationName)); |
|
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $manipulationName |
|
266
|
|
|
* @return string|null |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getManipulationArgument(string $manipulationName) |
|
269
|
|
|
{ |
|
270
|
|
|
foreach ($this->manipulationSequence->getGroups() as $manipulationSet) { |
|
271
|
|
|
if (array_key_exists($manipulationName, $manipulationSet)) { |
|
272
|
|
|
return $manipulationSet[$manipulationName]; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return null; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
protected function addManipulation(string $manipulationArgument, string $manipulationName = null) |
|
280
|
|
|
{ |
|
281
|
|
|
$manipulationName = $manipulationName ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; |
|
282
|
|
|
|
|
283
|
|
|
$this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument); |
|
284
|
|
|
|
|
285
|
|
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function mergeManipulations(Manipulations $manipulations) |
|
289
|
|
|
{ |
|
290
|
|
|
$this->manipulationSequence->merge($manipulations->manipulationSequence); |
|
291
|
|
|
|
|
292
|
|
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function getManipulationSequence(): ManipulationSequence |
|
296
|
|
|
{ |
|
297
|
|
|
return $this->manipulationSequence; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.