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 array */ |
38
|
|
|
protected $manipulations = []; |
39
|
|
|
|
40
|
|
|
public function __construct(array $manipulations) |
41
|
|
|
{ |
42
|
|
|
$this->manipulations = $manipulations; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $orientation |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function orientation(string $orientation) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return $this->setManipulation(func_get_args()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $cropMethod |
56
|
|
|
* @param int $width |
57
|
|
|
* @param int $height |
58
|
|
|
* @return $this |
59
|
|
|
* |
60
|
|
|
* @internal param string $method |
61
|
|
|
*/ |
62
|
|
|
public function crop(string $cropMethod, int $width, int $height) |
63
|
|
|
{ |
64
|
|
|
return $this |
65
|
|
|
->setManipulation($cropMethod, 'crop') |
|
|
|
|
66
|
|
|
->setManipulation($width, 'width') |
|
|
|
|
67
|
|
|
->setManipulation($height, 'height'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $width |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function width(int $width) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return $this->setManipulation(func_get_args()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $height |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function height(int $height) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return $this->setManipulation(func_get_args()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function fit(string $fitMethod, int $width, int $height) |
91
|
|
|
{ |
92
|
|
|
return $this |
93
|
|
|
->setManipulation($fitMethod, 'fit') |
|
|
|
|
94
|
|
|
->setManipulation($width, 'width') |
|
|
|
|
95
|
|
|
->setManipulation($height, 'height'); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function devicePixelRatio(int $ratio) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
return $this->setManipulation(func_get_args()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function brightness(int $brightness) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
return $this->setManipulation(func_get_args()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function gamma(float $gamma) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return $this->setManipulation(func_get_args()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function contrast(int $contrast) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
return $this->setManipulation(func_get_args()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function sharpen(int $sharpen) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->setManipulation(func_get_args()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function blur(int $blur) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return $this->setManipulation(func_get_args()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function pixelate(int $pixelate) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
return $this->setManipulation(func_get_args()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function greyscale() |
134
|
|
|
{ |
135
|
|
|
return $this->filter('greyscale'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function sepia() |
139
|
|
|
{ |
140
|
|
|
return $this->filter('sepia'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function background(string $colorName) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return $this->setManipulation(func_get_args()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function border(int $width, string $color, string $borderType = 'overlay') |
149
|
|
|
{ |
150
|
|
|
return $this->setManipulation(["{$width},{$color},{$borderType}"], 'border'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function quality(int $quality) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return $this->setManipulation(func_get_args()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function format(string $format) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
return $this->setManipulation(func_get_args()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function filter(string $filterName) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
return $this->setManipulation(func_get_args()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function hasManipulation($manipulationName): bool |
169
|
|
|
{ |
170
|
|
|
return ! is_null($this->getManipulation($manipulationName)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getManipulation($manipulationName): bool |
174
|
|
|
{ |
175
|
|
|
foreach($this->manipulations as $manipulation) { |
176
|
|
|
if ($manipulation[0] === $manipulationName) { |
177
|
|
|
return $manipulation; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
protected function setManipulation(array $arguments, string $operation = null) |
186
|
|
|
{ |
187
|
|
|
$operation = $operation ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; |
188
|
|
|
|
189
|
|
|
$this->manipulations[] = array_merge([$operation], $arguments); |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function mergeManipulations(Manipulations $manipulations) |
195
|
|
|
{ |
196
|
|
|
$this->manipulations = array_merge($this->manipulations, $manipulations->toArray()); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function toArray(): array |
202
|
|
|
{ |
203
|
|
|
return $this->manipulations; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.