1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage\Processor\Image; |
18
|
|
|
|
19
|
|
|
use Intervention\Image\Image; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Image\Exception\UnsupportedOperationException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Operations |
25
|
|
|
*/ |
26
|
|
|
class Operations |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \Intervention\Image\Image |
30
|
|
|
*/ |
31
|
|
|
protected Image $image; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Intervention\Image\Image $image Image |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Image $image) |
37
|
|
|
{ |
38
|
|
|
$this->image = $image; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name Name |
43
|
|
|
* @param array<string, mixed> $arguments Arguments |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function __call(string $name, array $arguments): mixed |
47
|
|
|
{ |
48
|
|
|
throw UnsupportedOperationException::withName($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Crops the image |
53
|
|
|
* |
54
|
|
|
* @link http://image.intervention.io/api/fit |
55
|
|
|
* @param array<string, mixed> $arguments Arguments |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function fit(array $arguments): void |
59
|
|
|
{ |
60
|
|
|
if (!isset($arguments['width'])) { |
61
|
|
|
throw new InvalidArgumentException('Missing width'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$preventUpscale = $arguments['preventUpscale'] ?? false; |
65
|
|
|
$height = $arguments['height'] ?? null; |
66
|
|
|
|
67
|
|
|
$this->image->fit( |
68
|
|
|
(int)$arguments['width'], |
69
|
|
|
(int)$height, |
70
|
|
|
static function ($constraint) use ($preventUpscale) { |
71
|
|
|
if ($preventUpscale) { |
72
|
|
|
$constraint->upsize(); |
73
|
|
|
} |
74
|
|
|
}, |
75
|
|
|
$arguments['position'] ?? 'center', |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Crops the image |
81
|
|
|
* |
82
|
|
|
* @link http://image.intervention.io/api/crop |
83
|
|
|
* @param array<string, mixed> $arguments Arguments |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function crop(array $arguments): void |
87
|
|
|
{ |
88
|
|
|
if (!isset($arguments['height'], $arguments['width'])) { |
89
|
|
|
throw new InvalidArgumentException('Missing width or height'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$arguments = array_merge(['x' => null, 'y' => null], $arguments); |
93
|
|
|
$height = $arguments['height'] ? (int)$arguments['height'] : null; |
94
|
|
|
$width = $arguments['width'] ? (int)$arguments['width'] : null; |
95
|
|
|
$x = $arguments['x'] ? (int)$arguments['x'] : null; |
96
|
|
|
$y = $arguments['y'] ? (int)$arguments['y'] : null; |
97
|
|
|
|
98
|
|
|
$this->image->crop($width, $height, $x, $y); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Flips the image horizontal |
103
|
|
|
* |
104
|
|
|
* @link http://image.intervention.io/api/flip |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function flipHorizontal(): void |
108
|
|
|
{ |
109
|
|
|
$this->flip(['direction' => 'h']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Flips the image vertical |
114
|
|
|
* |
115
|
|
|
* @link http://image.intervention.io/api/flip |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function flipVertical(): void |
119
|
|
|
{ |
120
|
|
|
$this->flip(['direction' => 'v']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Flips the image |
125
|
|
|
* |
126
|
|
|
* @link http://image.intervention.io/api/flip |
127
|
|
|
* @param array<string, mixed> $arguments Arguments |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function flip(array $arguments): void |
131
|
|
|
{ |
132
|
|
|
if (!isset($arguments['direction'])) { |
133
|
|
|
throw new InvalidArgumentException('Direction missing'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($arguments['direction'] !== 'v' && $arguments['direction'] !== 'h') { |
137
|
|
|
throw new InvalidArgumentException( |
138
|
|
|
'Invalid argument, you must provide h or v' |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->image->flip($arguments['direction']); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Resizes the image |
147
|
|
|
* |
148
|
|
|
* @link http://image.intervention.io/api/resize |
149
|
|
|
* @param array<string, mixed> $arguments Arguments |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function resize(array $arguments): void |
153
|
|
|
{ |
154
|
|
|
if (!isset($arguments['height'], $arguments['width'])) { |
155
|
|
|
throw new InvalidArgumentException( |
156
|
|
|
'Missing height or width' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$aspectRatio = $arguments['aspectRatio'] ?? true; |
161
|
|
|
$preventUpscale = $arguments['preventUpscale'] ?? false; |
162
|
|
|
|
163
|
|
|
$this->image->resize( |
164
|
|
|
$arguments['width'], |
165
|
|
|
$arguments['height'], |
166
|
|
|
static function ($constraint) use ($aspectRatio, $preventUpscale) { |
167
|
|
|
if ($aspectRatio) { |
168
|
|
|
$constraint->aspectRatio(); |
169
|
|
|
} |
170
|
|
|
if ($preventUpscale) { |
171
|
|
|
$constraint->upsize(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @link http://image.intervention.io/api/widen |
179
|
|
|
* @param array<string, mixed> $arguments Arguments |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function widen(array $arguments): void |
183
|
|
|
{ |
184
|
|
|
if (!isset($arguments['width'])) { |
185
|
|
|
throw new InvalidArgumentException( |
186
|
|
|
'Missing width' |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$preventUpscale = $arguments['preventUpscale'] ?? false; |
191
|
|
|
|
192
|
|
|
$this->image->widen((int)$arguments['width'], function ($constraint) use ($preventUpscale) { |
193
|
|
|
if ($preventUpscale) { |
194
|
|
|
$constraint->upsize(); |
195
|
|
|
} |
196
|
|
|
}); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @link http://image.intervention.io/api/heighten |
201
|
|
|
* @param array<string, mixed> $arguments Arguments |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function heighten(array $arguments): void |
205
|
|
|
{ |
206
|
|
|
if (!isset($arguments['height'])) { |
207
|
|
|
throw new InvalidArgumentException( |
208
|
|
|
'Missing height' |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$preventUpscale = $arguments['preventUpscale'] ?? false; |
213
|
|
|
|
214
|
|
|
$this->image->heighten((int)$arguments['height'], function ($constraint) use ($preventUpscale) { |
215
|
|
|
if ($preventUpscale) { |
216
|
|
|
$constraint->upsize(); |
217
|
|
|
} |
218
|
|
|
}); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @link http://image.intervention.io/api/rotate |
223
|
|
|
* @param array<string, mixed> $arguments Arguments |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
public function rotate(array $arguments): void |
227
|
|
|
{ |
228
|
|
|
if (!isset($arguments['angle'])) { |
229
|
|
|
throw new InvalidArgumentException( |
230
|
|
|
'Missing angle' |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$this->image->rotate((int)$arguments['angle']); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @link http://image.intervention.io/api/rotate |
239
|
|
|
* @param array<string, mixed> $arguments Arguments |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
|
|
public function sharpen(array $arguments): void |
243
|
|
|
{ |
244
|
|
|
if (!isset($arguments['amount'])) { |
245
|
|
|
throw new InvalidArgumentException( |
246
|
|
|
'Missing amount' |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->image->sharpen((int)$arguments['amount']); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Allows the declaration of a callable that gets the image manager instance |
255
|
|
|
* and the arguments passed to it. |
256
|
|
|
* |
257
|
|
|
* @param array<string, mixed> $arguments Arguments |
258
|
|
|
* @return void |
259
|
|
|
*/ |
260
|
|
|
public function callback(array $arguments): void |
261
|
|
|
{ |
262
|
|
|
if (!isset($arguments['callback'])) { |
263
|
|
|
throw new InvalidArgumentException( |
264
|
|
|
'Missing callback argument' |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if (!is_callable($arguments['callback'])) { |
269
|
|
|
throw new InvalidArgumentException( |
270
|
|
|
'Provided value for callback is not a callable' |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$arguments['callable']($this->image, $arguments); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|