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 InvalidArgumentException; |
20
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Variant; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Image Manipulation |
24
|
|
|
*/ |
25
|
|
|
class ImageVariant extends Variant |
26
|
|
|
{ |
27
|
|
|
protected string $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<int, string> |
31
|
|
|
*/ |
32
|
|
|
protected array $operations; |
33
|
|
|
|
34
|
|
|
protected string $path = ''; |
35
|
|
|
protected bool $optimize = false; |
36
|
|
|
protected string $url = ''; |
37
|
|
|
|
38
|
|
|
public const FLIP_HORIZONTAL = 'h'; |
39
|
|
|
public const FLIP_VERTICAL = 'v'; |
40
|
3 |
|
|
41
|
|
|
/** |
42
|
3 |
|
* @param string $name Name |
43
|
3 |
|
* @return self |
44
|
|
|
*/ |
45
|
3 |
|
public static function create(string $name): self |
46
|
|
|
{ |
47
|
|
|
$self = new self(); |
48
|
|
|
$self->name = $name; |
49
|
|
|
|
50
|
|
|
return $self; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
/** |
54
|
|
|
* Try to apply image optimizations if available on the system |
55
|
3 |
|
* |
56
|
|
|
* @return $this |
57
|
3 |
|
*/ |
58
|
|
|
public function optimize(): self |
59
|
|
|
{ |
60
|
|
|
$this->optimize = true; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $height Width |
67
|
|
|
* @param int|null $width Height |
68
|
|
|
* @param int|null $x X |
69
|
|
|
* @param int|null $y Y |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function crop(int $height, ?int $width = null, ?int $x = null, ?int $y = null): self |
73
|
|
|
{ |
74
|
|
|
$this->operations['crop'] = [ |
75
|
|
|
'width' => $width, |
76
|
|
|
'height' => $height, |
77
|
|
|
'x' => $x, |
78
|
|
|
'y' => $y |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $amount Angle |
86
|
2 |
|
* @return $this |
87
|
|
|
*/ |
88
|
2 |
|
public function sharpen(int $amount): self |
89
|
2 |
|
{ |
90
|
2 |
|
$this->operations['sharpen'] = [ |
91
|
2 |
|
'amount' => $amount, |
92
|
2 |
|
]; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $angle Angle |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function rotate(int $angle): self |
102
|
|
|
{ |
103
|
3 |
|
$this->operations['rotate'] = [ |
104
|
|
|
'angle' => $angle, |
105
|
3 |
|
]; |
106
|
3 |
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
3 |
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $height Height |
112
|
|
|
* @param boolean $preventUpscale Prevent Upscaling |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function heighten(int $height, bool $preventUpscale = false): self |
116
|
|
|
{ |
117
|
1 |
|
$this->operations['heighten'] = [ |
118
|
|
|
'height' => $height, |
119
|
1 |
|
'preventUpscale' => $preventUpscale |
120
|
1 |
|
]; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int $width Width |
127
|
|
|
* @param boolean $preventUpscale Prevent Upscaling |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function widen(int $width, bool $preventUpscale = false): self |
131
|
|
|
{ |
132
|
1 |
|
$this->operations['widen'] = [ |
133
|
|
|
'width' => $width, |
134
|
1 |
|
'preventUpscale' => $preventUpscale |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
1 |
|
* @param int $width Width |
142
|
1 |
|
* @param int $height Height |
143
|
|
|
* @param bool $aspectRatio Keeps the aspect ratio |
144
|
|
|
* @param bool $preventUpscale Prevents upscaling |
145
|
1 |
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function resize(int $width, int $height, bool $aspectRatio = true, bool $preventUpscale = false): self |
148
|
|
|
{ |
149
|
|
|
$this->operations['resize'] = [ |
150
|
|
|
'width' => $width, |
151
|
2 |
|
'height' => $height, |
152
|
|
|
'aspectRatio' => $aspectRatio, |
153
|
|
|
'preventUpscale' => $preventUpscale |
154
|
2 |
|
]; |
155
|
2 |
|
|
156
|
2 |
|
return $this; |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Flips the image horizontal |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function flipHorizontal(): self |
165
|
|
|
{ |
166
|
|
|
$this->operations['flipHorizontal'] = [ |
167
|
|
|
'direction' => self::FLIP_HORIZONTAL |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Flips the image vertical |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function flipVertical(): self |
179
|
|
|
{ |
180
|
|
|
$this->operations['flipVertical'] = [ |
181
|
|
|
'direction' => self::FLIP_VERTICAL |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Flips the image |
189
|
|
|
* |
190
|
|
|
* @param string $direction Direction, h or v |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function flip(string $direction): self |
194
|
|
|
{ |
195
|
|
|
if ($direction !== 'h' && $direction !== 'v') { |
196
|
|
|
throw new InvalidArgumentException(sprintf( |
197
|
|
|
'`%s` is invalid, provide `h` or `v`', |
198
|
|
|
$direction |
199
|
|
|
)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->operations['flip'] = [ |
203
|
|
|
'direction' => $direction |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Allows the declaration of a callable that gets the image manager instance |
211
|
|
|
* and the arguments passed to it. |
212
|
|
|
* |
213
|
|
|
* @param callable $callback callback |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function callback(callable $callback): self |
217
|
|
|
{ |
218
|
|
|
$this->operations['callback'] = [ |
219
|
|
|
'callback' => $callback |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @link http://image.intervention.io/api/fit |
227
|
|
|
* @param int $width Width |
228
|
|
|
* @param int|null $height Height |
229
|
|
|
* @param callable|null $callback Callback |
230
|
|
|
* @param bool $preventUpscale Prevent Upscaling |
231
|
|
|
* @param string $position Position |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function fit( |
235
|
|
|
int $width, |
236
|
|
|
?int $height = null, |
237
|
|
|
?callable $callback = null, |
238
|
|
|
bool $preventUpscale = false, |
239
|
|
|
string $position = 'center' |
240
|
|
|
): self { |
241
|
|
|
$this->operations['fit'] = [ |
242
|
|
|
'width' => $width, |
243
|
|
|
'height' => $height, |
244
|
|
|
'callback' => $callback, |
245
|
|
|
'preventUpscale' => $preventUpscale, |
246
|
|
|
'position' => $position |
247
|
|
|
]; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return array<string, mixed> |
254
|
|
|
*/ |
255
|
|
|
public function toArray(): array |
256
|
|
|
{ |
257
|
|
|
return [ |
258
|
|
|
'operations' => $this->operations, |
259
|
|
|
'path' => $this->path, |
260
|
|
|
'url' => $this->url, |
261
|
|
|
'optimize' => $this->optimize, |
262
|
|
|
]; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|