Passed
Push — 1.2 ( 0ce0c4...ac7648 )
by Quentin
22:05 queued 06:18
created

Glide::getPresetUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
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
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\Media...ary\League\Glide\Server was not found. Did you mean League\Glide\Server? If so, make sure to prefix the type with \.
Loading history...
36
     */
37
    private $server;
38
39
    /**
40
     * @var UrlBuilder
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\MediaLibrary\UrlBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    private $urlBuilder;
43
44
    /**
45
     * @param Config $config
46
     * @param Application $app
47
     * @param Request $request
48
     */
49
    public function __construct(Config $config, Application $app, Request $request)
50
    {
51
        $this->config = $config;
52
        $this->app = $app;
53
        $this->request = $request;
54
55
        $baseUrl = join('/', [
56
            rtrim($this->config->get('twill.glide.base_url'), '/'),
57
            ltrim($this->config->get('twill.glide.base_path'), '/'),
58
        ]);
59
60
        $this->server = ServerFactory::create([
0 ignored issues
show
Documentation Bug introduced by
It seems like League\Glide\ServerFacto...de.presets', array()))) of type League\Glide\Server is incompatible with the declared type A17\Twill\Services\Media...ary\League\Glide\Server of property $server.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
            'response' => new LaravelResponseFactory($this->request),
62
            'source' => $this->config->get('twill.glide.source'),
63
            'cache' => $this->config->get('twill.glide.cache'),
64
            'cache_path_prefix' => $this->config->get('twill.glide.cache_path_prefix'),
65
            'base_url' => $baseUrl,
66
            'presets' => $this->config->get('twill.glide.presets', [])
67
        ]);
68
69
        $this->urlBuilder = UrlBuilderFactory::create(
0 ignored issues
show
Documentation Bug introduced by
It seems like League\Glide\Urls\UrlBui...lide.sign_key') : null) of type League\Glide\Urls\UrlBuilder is incompatible with the declared type A17\Twill\Services\MediaLibrary\UrlBuilder of property $urlBuilder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
            $baseUrl,
71
            $this->config->get('twill.glide.use_signed_urls') ? $this->config->get('twill.glide.sign_key') : null
72
        );
73
    }
74
75
    /**
76
     * @param string $path
77
     * @return StreamedResponse
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\MediaLibrary\StreamedResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
     */
79
    public function render($path)
80
    {
81
        if ($this->config->get('twill.glide.use_signed_urls')) {
82
            SignatureFactory::create($this->config->get('twill.glide.sign_key'))->validateRequest($this->config->get('twill.glide.base_path') . '/' . $path, $this->request->all());
83
        }
84
85
        return $this->server->getImageResponse($path, $this->request->all());
86
    }
87
88
    /**
89
     * @param string $id
90
     * @param array $params
91
     * @return string
92
     */
93
    public function getUrl($id, array $params = [])
94
    {
95
        $defaultParams = config('twill.glide.default_params');
96
        return $this->urlBuilder->getUrl($id, Str::endsWith($id, '.svg') ? [] : array_replace($defaultParams, $params));
97
    }
98
99
    /**
100
     * @param string $id
101
     * @param array $cropParams
102
     * @param array $params
103
     * @return string
104
     */
105
    public function getUrlWithCrop($id, array $cropParams, array $params = [])
106
    {
107
        return $this->getUrl($id, $this->getCrop($cropParams) + $params);
108
    }
109
110
    /**
111
     * @param string $id
112
     * @param array $cropParams
113
     * @param mixed $width
114
     * @param mixed $height
115
     * @param array $params
116
     * @return string
117
     */
118
    public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = [])
119
    {
120
        return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params);
121
    }
122
123
    /**
124
     * @param string $id
125
     * @param array $params
126
     * @return string
127
     */
128
    public function getLQIPUrl($id, array $params = [])
129
    {
130
        $defaultParams = config('twill.glide.lqip_default_params');
131
132
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
133
134
        $params = Arr::except($params, $this->cropParamsKeys);
135
136
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
137
    }
138
139
    /**
140
     * @param string $id
141
     * @param array $params
142
     * @return string
143
     */
144
    public function getSocialUrl($id, array $params = [])
145
    {
146
        $defaultParams = config('twill.glide.social_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
     * @return string
158
     */
159
    public function getCmsUrl($id, array $params = [])
160
    {
161
        $defaultParams = config('twill.glide.cms_default_params');
162
163
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
164
165
        $params = Arr::except($params, $this->cropParamsKeys);
166
167
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
168
    }
169
170
    /**
171
     * @param string $id, string $preset
172
     * @return string
173
     */
174
    public function getPresetUrl($id, $preset)
175
    {
176
        return $this->getRawUrl($id) . '?p=' . $preset;
0 ignored issues
show
Bug introduced by
Are you sure $this->getRawUrl($id) of type array|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        return /** @scrutinizer ignore-type */ $this->getRawUrl($id) . '?p=' . $preset;
Loading history...
177
    }
178
179
    /**
180
     * @param string $id
181
     * @return array|null
182
     */
183
    public function getRawUrl($id)
184
    {
185
        return $this->urlBuilder->getUrL($id);
186
    }
187
188
    /**
189
     * @param string $id
190
     * @return array
191
     */
192
    public function getDimensions($id)
193
    {
194
        $url = $this->urlBuilder->getUrL($id);
195
196
        try {
197
            list($w, $h) = getimagesize($url);
198
199
            return [
200
                'width' => $w,
201
                'height' => $h,
202
            ];
203
        } catch (\Exception $e) {
204
            return [
205
                'width' => 0,
206
                'height' => 0,
207
            ];
208
        }
209
    }
210
211
    /**
212
     * @param array $crop_params
213
     * @return array
214
     */
215
    protected function getCrop($crop_params)
216
    {
217
        if (!empty($crop_params)) {
218
            return ['crop' =>
219
                $crop_params['crop_w'] . ',' .
220
                $crop_params['crop_h'] . ',' .
221
                $crop_params['crop_x'] . ',' .
222
                $crop_params['crop_y'],
223
            ];
224
        }
225
226
        return [];
227
    }
228
229
    /**
230
     * @param array $crop_params
231
     * @param int $width
232
     * @param int $height
233
     * @return array
234
     */
235
    protected function getFocalPointCrop($crop_params, $width, $height)
236
    {
237
        if (!empty($crop_params)) {
238
            // determine center coordinates of user crop and express it in term of original image width and height percentage
239
            $fpX = 100 * ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width;
240
            $fpY = 100 * ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height;
241
242
            // determine focal zoom
243
            if ($crop_params['crop_w'] > $crop_params['crop_h']) {
244
                $fpZ = $width / ($crop_params['crop_w'] ?? $width);
245
            } else {
246
                $fpZ = $height / ($crop_params['crop_h'] ?? $height);
247
            }
248
249
            $fpX = number_format($fpX, 0, ".", "");
250
            $fpY = number_format($fpY, 0, ".", "");
251
            $fpZ = number_format($fpZ, 4, ".", "");
252
253
            $params = ['fit' => 'crop-' . $fpX . '-' . $fpY . '-' . $fpZ];
254
255
            return $params;
256
        }
257
258
        return [];
259
    }
260
}
261