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