|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\MediaLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use League\Glide\Responses\LaravelResponseFactory; |
|
11
|
|
|
use League\Glide\ServerFactory; |
|
12
|
|
|
use League\Glide\Signatures\SignatureFactory; |
|
13
|
|
|
use League\Glide\Urls\UrlBuilderFactory; |
|
14
|
|
|
|
|
15
|
|
|
class Glide implements ImageServiceInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use ImageServiceDefaults; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Config |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $config; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Application |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Request |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $request; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \League\Glide\Server |
|
36
|
|
|
*/ |
|
37
|
|
|
private $server; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \League\Glide\Urls\UrlBuilder |
|
41
|
|
|
*/ |
|
42
|
|
|
private $urlBuilder; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Config $config |
|
46
|
|
|
* @param Application $app |
|
47
|
|
|
* @param Request $request |
|
48
|
|
|
*/ |
|
49
|
11 |
|
public function __construct(Config $config, Application $app, Request $request) |
|
50
|
|
|
{ |
|
51
|
11 |
|
$this->config = $config; |
|
52
|
11 |
|
$this->app = $app; |
|
53
|
11 |
|
$this->request = $request; |
|
54
|
|
|
|
|
55
|
11 |
|
$baseUrlHost = $this->config->get( |
|
56
|
11 |
|
'twill.glide.base_url', |
|
57
|
11 |
|
$this->request->getScheme() . '://' . str_replace( |
|
58
|
11 |
|
['http://', 'https://'], |
|
59
|
11 |
|
'', |
|
60
|
11 |
|
$this->config->get('app.url') |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
11 |
|
$baseUrl = join('/', [ |
|
65
|
11 |
|
rtrim($baseUrlHost, '/'), |
|
66
|
11 |
|
ltrim($this->config->get('twill.glide.base_path'), '/'), |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
11 |
|
$this->server = ServerFactory::create([ |
|
70
|
11 |
|
'response' => new LaravelResponseFactory($this->request), |
|
71
|
11 |
|
'source' => $this->config->get('twill.glide.source'), |
|
72
|
11 |
|
'cache' => $this->config->get('twill.glide.cache'), |
|
73
|
11 |
|
'cache_path_prefix' => $this->config->get('twill.glide.cache_path_prefix'), |
|
74
|
11 |
|
'base_url' => $baseUrl, |
|
75
|
11 |
|
'presets' => $this->config->get('twill.glide.presets', []), |
|
76
|
11 |
|
'driver' => $this->config->get('twill.glide.driver') |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
11 |
|
$this->urlBuilder = UrlBuilderFactory::create( |
|
80
|
11 |
|
$baseUrl, |
|
81
|
11 |
|
$this->config->get('twill.glide.use_signed_urls') ? $this->config->get('twill.glide.sign_key') : null |
|
82
|
|
|
); |
|
83
|
11 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $path |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function render($path) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->config->get('twill.glide.use_signed_urls')) { |
|
92
|
|
|
SignatureFactory::create($this->config->get('twill.glide.sign_key'))->validateRequest($this->config->get('twill.glide.base_path') . '/' . $path, $this->request->all()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->server->getImageResponse($path, $this->request->all()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $id |
|
100
|
|
|
* @param array $params |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
6 |
|
public function getUrl($id, array $params = []) |
|
104
|
|
|
{ |
|
105
|
6 |
|
$defaultParams = config('twill.glide.default_params'); |
|
106
|
6 |
|
|
|
107
|
|
|
return $this->getOriginalMediaUrl($id) ?? |
|
108
|
6 |
|
$this->urlBuilder->getUrl($id, array_replace($defaultParams, $params)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
6 |
|
* @param string $id |
|
113
|
|
|
* @param array $cropParams |
|
114
|
|
|
* @param array $params |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getUrlWithCrop($id, array $cropParams, array $params = []) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getUrl($id, $this->getCrop($cropParams) + $params); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $id |
|
124
|
|
|
* @param array $cropParams |
|
125
|
|
|
* @param mixed $width |
|
126
|
|
|
* @param mixed $height |
|
127
|
|
|
* @param array $params |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = []) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $id |
|
137
|
|
|
* @param array $params |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getLQIPUrl($id, array $params = []) |
|
141
|
|
|
{ |
|
142
|
|
|
$defaultParams = config('twill.glide.lqip_default_params'); |
|
143
|
|
|
|
|
144
|
|
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
|
145
|
|
|
|
|
146
|
|
|
$params = Arr::except($params, $this->cropParamsKeys); |
|
147
|
|
|
|
|
148
|
|
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $id |
|
153
|
|
|
* @param array $params |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getSocialUrl($id, array $params = []) |
|
157
|
|
|
{ |
|
158
|
|
|
$defaultParams = config('twill.glide.social_default_params'); |
|
159
|
|
|
|
|
160
|
|
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
|
161
|
|
|
|
|
162
|
|
|
$params = Arr::except($params, $this->cropParamsKeys); |
|
163
|
|
|
|
|
164
|
|
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $id |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getCmsUrl($id, array $params = []) |
|
172
|
|
|
{ |
|
173
|
|
|
$defaultParams = config('twill.glide.cms_default_params'); |
|
174
|
|
|
|
|
175
|
3 |
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
|
176
|
|
|
|
|
177
|
3 |
|
$params = Arr::except($params, $this->cropParamsKeys); |
|
178
|
|
|
|
|
179
|
3 |
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
|
180
|
|
|
} |
|
181
|
3 |
|
|
|
182
|
|
|
/** |
|
183
|
3 |
|
* @param string $id, string $preset |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getPresetUrl($id, $preset) |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->getRawUrl($id) . '?p=' . $preset; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $id |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getRawUrl($id) |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->getOriginalMediaUrl($id) ?? $this->urlBuilder->getUrl($id); |
|
198
|
|
|
} |
|
199
|
3 |
|
|
|
200
|
|
|
/** |
|
201
|
3 |
|
* @param string $id |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getDimensions($id) |
|
205
|
|
|
{ |
|
206
|
|
|
$url = $this->urlBuilder->getUrl($id); |
|
207
|
|
|
|
|
208
|
|
|
try { |
|
209
|
|
|
list($w, $h) = getimagesize($url); |
|
210
|
|
|
|
|
211
|
|
|
return [ |
|
212
|
|
|
'width' => $w, |
|
213
|
|
|
'height' => $h, |
|
214
|
|
|
]; |
|
215
|
|
|
} catch (\Exception $e) { |
|
216
|
|
|
return [ |
|
217
|
|
|
'width' => 0, |
|
218
|
|
|
'height' => 0, |
|
219
|
|
|
]; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param array $crop_params |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function getCrop($crop_params) |
|
228
|
|
|
{ |
|
229
|
|
|
if (!empty($crop_params)) { |
|
230
|
|
|
return ['crop' => |
|
231
|
|
|
$crop_params['crop_w'] . ',' . |
|
232
|
|
|
$crop_params['crop_h'] . ',' . |
|
233
|
|
|
$crop_params['crop_x'] . ',' . |
|
234
|
|
|
$crop_params['crop_y'], |
|
235
|
|
|
]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return []; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param array $crop_params |
|
243
|
|
|
* @param int $width |
|
244
|
|
|
* @param int $height |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getFocalPointCrop($crop_params, $width, $height) |
|
248
|
|
|
{ |
|
249
|
|
|
if (!empty($crop_params)) { |
|
250
|
|
|
// determine center coordinates of user crop and express it in term of original image width and height percentage |
|
251
|
|
|
$fpX = 100 * ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width; |
|
252
|
|
|
$fpY = 100 * ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height; |
|
253
|
|
|
|
|
254
|
|
|
// determine focal zoom |
|
255
|
|
|
if ($crop_params['crop_w'] > $crop_params['crop_h']) { |
|
256
|
|
|
$fpZ = $width / ($crop_params['crop_w'] ?? $width); |
|
257
|
|
|
} else { |
|
258
|
|
|
$fpZ = $height / ($crop_params['crop_h'] ?? $height); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$fpX = number_format($fpX, 0, ".", ""); |
|
262
|
|
|
$fpY = number_format($fpY, 0, ".", ""); |
|
263
|
|
|
$fpZ = number_format($fpZ, 4, ".", ""); |
|
264
|
|
|
|
|
265
|
|
|
$params = ['fit' => 'crop-' . $fpX . '-' . $fpY . '-' . $fpZ]; |
|
266
|
|
|
|
|
267
|
|
|
return $params; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return []; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param string $id |
|
275
|
|
|
* @return string |
|
276
|
|
|
*/ |
|
277
|
|
|
private function getOriginalMediaUrl($id) |
|
278
|
|
|
{ |
|
279
|
|
|
$libraryDisk = $this->config->get('twill.media_library.disk'); |
|
280
|
|
|
$endpointType = $this->config->get('twill.media_library.endpoint_type'); |
|
281
|
|
|
$localMediaLibraryUrl = $this->config->get("filesystems.disks.$libraryDisk.url"); |
|
282
|
|
|
$originalMediaForExtensions = $this->config->get('twill.glide.original_media_for_extensions'); |
|
283
|
|
|
$addParamsToSvgs = $this->config->get('twill.glide.add_params_to_svgs', false); |
|
284
|
|
|
|
|
285
|
|
|
if ((Str::endsWith($id, '.svg') && $addParamsToSvgs) || !Str::endsWith($id, $originalMediaForExtensions)) { |
|
286
|
|
|
return null; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** @var string $endpoint */ |
|
290
|
|
|
$endpoint; |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
switch ($endpointType) { |
|
293
|
|
|
case 'local': |
|
294
|
|
|
$endpoint = $localMediaLibraryUrl; |
|
295
|
|
|
break; |
|
296
|
|
|
case 's3': |
|
297
|
|
|
$endpoint = s3Endpoint($libraryDisk); |
|
298
|
|
|
break; |
|
299
|
|
|
case 'azure': |
|
300
|
|
|
$endpoint = azureEndpoint($libraryDisk); |
|
301
|
|
|
break; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
return $endpoint . '/' . $id; |
|
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|