-
Notifications
You must be signed in to change notification settings - Fork 554
/
Imgix.php
123 lines (99 loc) · 3.83 KB
/
Imgix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace A17\Twill\Services\MediaLibrary;
use Imgix\ShardStrategy;
use Imgix\UrlBuilder;
class Imgix implements ImageServiceInterface
{
use ImageServiceDefaults;
private $urlBuilder;
public function __construct()
{
$urlBuilder = new UrlBuilder(config('twill.imgix.source_host'), config('twill.imgix.use_https'), '', ShardStrategy::CRC, false);
if (config('twill.imgix.use_signed_urls')) {
$urlBuilder->setSignKey(config('twill.imgix.sign_key'));
}
$this->urlBuilder = $urlBuilder;
}
public function getUrl($id, array $params = [])
{
$defaultParams = config('twill.imgix.default_params');
return $this->urlBuilder->createURL($id, ends_with($id, '.svg') ? [] : array_replace($defaultParams, $params));
}
public function getUrlWithCrop($id, array $cropParams, array $params = [])
{
return $this->getUrl($id, $this->getCrop($cropParams) + $params);
}
public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = [])
{
return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params);
}
public function getLQIPUrl($id, array $params = [])
{
$defaultParams = config('twill.imgix.lqip_default_params');
return $this->getUrl($id, array_replace($defaultParams, $params));
}
public function getSocialUrl($id, array $params = [])
{
$defaultParams = config('twill.imgix.social_default_params');
return $this->getUrl($id, array_replace($defaultParams, $params));
}
public function getCmsUrl($id, array $params = [])
{
$defaultParams = config('twill.imgix.cms_default_params');
return $this->getUrl($id, array_replace($defaultParams, $params));
}
public function getRawUrl($id)
{
return $this->urlBuilder->createURL($id);
}
public function getDimensions($id)
{
$url = $this->urlBuilder->createURL($id, ['fm' => 'json']);
try {
$imageMetadata = json_decode(file_get_contents($url), true);
return [
'width' => $imageMetadata['PixelWidth'],
'height' => $imageMetadata['PixelHeight'],
];
} catch (\Exception $e) {
try {
list($width, $height) = getimagesize($url);
return [
'width' => $width,
'height' => $height,
];
} catch (\Exception $e) {
return [
'width' => 0,
'height' => 0,
];
}
}
}
protected function getCrop($crop_params)
{
if (!empty($crop_params)) {
return ['rect' => $crop_params['crop_x'] . ',' . $crop_params['crop_y'] . ',' . $crop_params['crop_w'] . ',' . $crop_params['crop_h']];
}
return [];
}
protected function getFocalPointCrop($crop_params, $width, $height)
{
if (!empty($crop_params)) {
// determine center coordinates of user crop and express it in term of original image width and height percentage
$fpX = ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width;
$fpY = ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height;
// determine focal zoom
if ($crop_params['crop_w'] > $crop_params['crop_h']) {
$fpZ = $width / $crop_params['crop_w'];
} else {
$fpZ = $height / $crop_params['crop_h'];
}
$params = ['fp-x' => $fpX, 'fp-y' => $fpY, 'fp-z' => $fpZ];
return array_map(function ($param) {
return number_format($param, 4, ".", "");
}, $params) + ['crop' => 'focalpoint', 'fit' => 'crop'];
}
return [];
}
}