Passed
Push — master ( 398928...1438ee )
by Quentin
60:26 queued 54:50
created

Glide::getUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
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
        $addParamsToSvgs = config('twill.glide.add_params_to_svgs', false);
107
108 6
        if (!$addParamsToSvgs && Str::endsWith($id, '.svg')) {
109
            return $this->urlBuilder->getUrl($id);
110
        }
111
112 6
        return $this->urlBuilder->getUrl($id, array_replace($defaultParams, $params));
113
    }
114
115
    /**
116
     * @param string $id
117
     * @param array $cropParams
118
     * @param array $params
119
     * @return string
120
     */
121
    public function getUrlWithCrop($id, array $cropParams, array $params = [])
122
    {
123
        return $this->getUrl($id, $this->getCrop($cropParams) + $params);
124
    }
125
126
    /**
127
     * @param string $id
128
     * @param array $cropParams
129
     * @param mixed $width
130
     * @param mixed $height
131
     * @param array $params
132
     * @return string
133
     */
134
    public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = [])
135
    {
136
        return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params);
137
    }
138
139
    /**
140
     * @param string $id
141
     * @param array $params
142
     * @return string
143
     */
144
    public function getLQIPUrl($id, array $params = [])
145
    {
146
        $defaultParams = config('twill.glide.lqip_default_params');
147
148
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
149
150
        $params = Arr::except($params, $this->cropParamsKeys);
151
152
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
153
    }
154
155
    /**
156
     * @param string $id
157
     * @param array $params
158
     * @return string
159
     */
160
    public function getSocialUrl($id, array $params = [])
161
    {
162
        $defaultParams = config('twill.glide.social_default_params');
163
164
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
165
166
        $params = Arr::except($params, $this->cropParamsKeys);
167
168
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
169
    }
170
171
    /**
172
     * @param string $id
173
     * @return string
174
     */
175 3
    public function getCmsUrl($id, array $params = [])
176
    {
177 3
        $defaultParams = config('twill.glide.cms_default_params');
178
179 3
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
180
181 3
        $params = Arr::except($params, $this->cropParamsKeys);
182
183 3
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
184
    }
185
186
    /**
187
     * @param string $id, string $preset
188
     * @return string
189
     */
190
    public function getPresetUrl($id, $preset)
191
    {
192
        return $this->getRawUrl($id) . '?p=' . $preset;
193
    }
194
195
    /**
196
     * @param string $id
197
     * @return string
198
     */
199 3
    public function getRawUrl($id)
200
    {
201 3
        return $this->urlBuilder->getUrL($id);
202
    }
203
204
    /**
205
     * @param string $id
206
     * @return array
207
     */
208
    public function getDimensions($id)
209
    {
210
        $url = $this->urlBuilder->getUrL($id);
211
212
        try {
213
            list($w, $h) = getimagesize($url);
214
215
            return [
216
                'width' => $w,
217
                'height' => $h,
218
            ];
219
        } catch (\Exception $e) {
220
            return [
221
                'width' => 0,
222
                'height' => 0,
223
            ];
224
        }
225
    }
226
227
    /**
228
     * @param array $crop_params
229
     * @return array
230
     */
231
    protected function getCrop($crop_params)
232
    {
233
        if (!empty($crop_params)) {
234
            return ['crop' =>
235
                $crop_params['crop_w'] . ',' .
236
                $crop_params['crop_h'] . ',' .
237
                $crop_params['crop_x'] . ',' .
238
                $crop_params['crop_y'],
239
            ];
240
        }
241
242
        return [];
243
    }
244
245
    /**
246
     * @param array $crop_params
247
     * @param int $width
248
     * @param int $height
249
     * @return array
250
     */
251
    protected function getFocalPointCrop($crop_params, $width, $height)
252
    {
253
        if (!empty($crop_params)) {
254
            // determine center coordinates of user crop and express it in term of original image width and height percentage
255
            $fpX = 100 * ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width;
256
            $fpY = 100 * ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height;
257
258
            // determine focal zoom
259
            if ($crop_params['crop_w'] > $crop_params['crop_h']) {
260
                $fpZ = $width / ($crop_params['crop_w'] ?? $width);
261
            } else {
262
                $fpZ = $height / ($crop_params['crop_h'] ?? $height);
263
            }
264
265
            $fpX = number_format($fpX, 0, ".", "");
266
            $fpY = number_format($fpY, 0, ".", "");
267
            $fpZ = number_format($fpZ, 4, ".", "");
268
269
            $params = ['fit' => 'crop-' . $fpX . '-' . $fpY . '-' . $fpZ];
270
271
            return $params;
272
        }
273
274
        return [];
275
    }
276
}
277