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
|
10 |
|
public function __construct(Config $config, Application $app, Request $request) |
50
|
|
|
{ |
51
|
10 |
|
$this->config = $config; |
52
|
10 |
|
$this->app = $app; |
53
|
10 |
|
$this->request = $request; |
54
|
|
|
|
55
|
10 |
|
$baseUrl = join('/', [ |
56
|
10 |
|
rtrim($this->config->get('twill.glide.base_url'), '/'), |
57
|
10 |
|
ltrim($this->config->get('twill.glide.base_path'), '/'), |
58
|
|
|
]); |
59
|
|
|
|
60
|
10 |
|
$this->server = ServerFactory::create([ |
61
|
10 |
|
'response' => new LaravelResponseFactory($this->request), |
62
|
10 |
|
'source' => $this->config->get('twill.glide.source'), |
63
|
10 |
|
'cache' => $this->config->get('twill.glide.cache'), |
64
|
10 |
|
'cache_path_prefix' => $this->config->get('twill.glide.cache_path_prefix'), |
65
|
10 |
|
'base_url' => $baseUrl, |
66
|
10 |
|
'presets' => $this->config->get('twill.glide.presets', []), |
67
|
10 |
|
'driver' => $this->config->get('twill.glide.driver') |
68
|
|
|
]); |
69
|
|
|
|
70
|
10 |
|
$this->urlBuilder = UrlBuilderFactory::create( |
71
|
10 |
|
$baseUrl, |
72
|
10 |
|
$this->config->get('twill.glide.use_signed_urls') ? $this->config->get('twill.glide.sign_key') : null |
73
|
|
|
); |
74
|
10 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $path |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function render($path) |
81
|
|
|
{ |
82
|
|
|
if ($this->config->get('twill.glide.use_signed_urls')) { |
83
|
|
|
SignatureFactory::create($this->config->get('twill.glide.sign_key'))->validateRequest($this->config->get('twill.glide.base_path') . '/' . $path, $this->request->all()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->server->getImageResponse($path, $this->request->all()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $id |
91
|
|
|
* @param array $params |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
6 |
|
public function getUrl($id, array $params = []) |
95
|
|
|
{ |
96
|
6 |
|
$defaultParams = config('twill.glide.default_params'); |
97
|
6 |
|
$addParamsToSvgs = config('twill.glide.add_params_to_svgs', false); |
98
|
|
|
|
99
|
6 |
|
if (!$addParamsToSvgs && Str::endsWith($id, '.svg')) { |
100
|
|
|
return $this->urlBuilder->getUrl($id); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
return $this->urlBuilder->getUrl($id, array_replace($defaultParams, $params)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $id |
108
|
|
|
* @param array $cropParams |
109
|
|
|
* @param array $params |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getUrlWithCrop($id, array $cropParams, array $params = []) |
113
|
|
|
{ |
114
|
|
|
return $this->getUrl($id, $this->getCrop($cropParams) + $params); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $id |
119
|
|
|
* @param array $cropParams |
120
|
|
|
* @param mixed $width |
121
|
|
|
* @param mixed $height |
122
|
|
|
* @param array $params |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = []) |
126
|
|
|
{ |
127
|
|
|
return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $id |
132
|
|
|
* @param array $params |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getLQIPUrl($id, array $params = []) |
136
|
|
|
{ |
137
|
|
|
$defaultParams = config('twill.glide.lqip_default_params'); |
138
|
|
|
|
139
|
|
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
140
|
|
|
|
141
|
|
|
$params = Arr::except($params, $this->cropParamsKeys); |
142
|
|
|
|
143
|
|
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $id |
148
|
|
|
* @param array $params |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getSocialUrl($id, array $params = []) |
152
|
|
|
{ |
153
|
|
|
$defaultParams = config('twill.glide.social_default_params'); |
154
|
|
|
|
155
|
|
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
156
|
|
|
|
157
|
|
|
$params = Arr::except($params, $this->cropParamsKeys); |
158
|
|
|
|
159
|
|
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $id |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
3 |
|
public function getCmsUrl($id, array $params = []) |
167
|
|
|
{ |
168
|
3 |
|
$defaultParams = config('twill.glide.cms_default_params'); |
169
|
|
|
|
170
|
3 |
|
$cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : []; |
171
|
|
|
|
172
|
3 |
|
$params = Arr::except($params, $this->cropParamsKeys); |
173
|
|
|
|
174
|
3 |
|
return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $id, string $preset |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getPresetUrl($id, $preset) |
182
|
|
|
{ |
183
|
|
|
return $this->getRawUrl($id) . '?p=' . $preset; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $id |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
3 |
|
public function getRawUrl($id) |
191
|
|
|
{ |
192
|
3 |
|
return $this->urlBuilder->getUrL($id); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $id |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
public function getDimensions($id) |
200
|
|
|
{ |
201
|
|
|
$url = $this->urlBuilder->getUrL($id); |
202
|
|
|
|
203
|
|
|
try { |
204
|
|
|
list($w, $h) = getimagesize($url); |
205
|
|
|
|
206
|
|
|
return [ |
207
|
|
|
'width' => $w, |
208
|
|
|
'height' => $h, |
209
|
|
|
]; |
210
|
|
|
} catch (\Exception $e) { |
211
|
|
|
return [ |
212
|
|
|
'width' => 0, |
213
|
|
|
'height' => 0, |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param array $crop_params |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
protected function getCrop($crop_params) |
223
|
|
|
{ |
224
|
|
|
if (!empty($crop_params)) { |
225
|
|
|
return ['crop' => |
226
|
|
|
$crop_params['crop_w'] . ',' . |
227
|
|
|
$crop_params['crop_h'] . ',' . |
228
|
|
|
$crop_params['crop_x'] . ',' . |
229
|
|
|
$crop_params['crop_y'], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return []; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $crop_params |
238
|
|
|
* @param int $width |
239
|
|
|
* @param int $height |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function getFocalPointCrop($crop_params, $width, $height) |
243
|
|
|
{ |
244
|
|
|
if (!empty($crop_params)) { |
245
|
|
|
// determine center coordinates of user crop and express it in term of original image width and height percentage |
246
|
|
|
$fpX = 100 * ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width; |
247
|
|
|
$fpY = 100 * ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height; |
248
|
|
|
|
249
|
|
|
// determine focal zoom |
250
|
|
|
if ($crop_params['crop_w'] > $crop_params['crop_h']) { |
251
|
|
|
$fpZ = $width / ($crop_params['crop_w'] ?? $width); |
252
|
|
|
} else { |
253
|
|
|
$fpZ = $height / ($crop_params['crop_h'] ?? $height); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$fpX = number_format($fpX, 0, ".", ""); |
257
|
|
|
$fpY = number_format($fpY, 0, ".", ""); |
258
|
|
|
$fpZ = number_format($fpZ, 4, ".", ""); |
259
|
|
|
|
260
|
|
|
$params = ['fit' => 'crop-' . $fpX . '-' . $fpY . '-' . $fpZ]; |
261
|
|
|
|
262
|
|
|
return $params; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return []; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|