1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\Storyblok\Support\ImageTransformers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
class Storyblok extends BaseTransformer |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Performs any actions needed once the object is created |
12
|
|
|
* and any preprocessing is completed |
13
|
|
|
* |
14
|
|
|
* @return $this |
15
|
|
|
*/ |
16
|
|
|
public function init() { |
17
|
|
|
$this->extractMetaDetails(); |
18
|
|
|
|
19
|
|
|
return $this; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Resizes the image and sets the focal point |
24
|
|
|
* |
25
|
|
|
* @param int $width |
26
|
|
|
* @param int $height |
27
|
|
|
* @param string $focus |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
public function resize($width = 0, $height = 0, $focus = null) |
31
|
|
|
{ |
32
|
|
|
$this->transformations = array_merge($this->transformations, [ |
33
|
|
|
'width' => $width, |
34
|
|
|
'height' => $height, |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
if ($focus) { |
38
|
|
|
$this->transformations = array_merge($this->transformations, [ |
39
|
|
|
'focus' => $focus, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Fits the image in the given width and height |
48
|
|
|
* |
49
|
|
|
* @param $width |
50
|
|
|
* @param $height |
51
|
|
|
* @param $fill |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function fitIn($width = 0, $height = 0, $fill = 'transparent') |
55
|
|
|
{ |
56
|
|
|
$this->transformations = array_merge($this->transformations, [ |
57
|
|
|
'width' => $width, |
58
|
|
|
'height' => $height, |
59
|
|
|
'fill' => $fill, |
60
|
|
|
'fit-in' => true, |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
// has to be an image that supports transparency |
64
|
|
|
if ($fill === 'transparent') { |
65
|
|
|
$this->format('webp'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the image format you want returned |
73
|
|
|
* |
74
|
|
|
* @param $format |
75
|
|
|
* @param $quality |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function format($format, $quality = null) |
79
|
|
|
{ |
80
|
|
|
$this->transformations = array_merge($this->transformations, [ |
81
|
|
|
'format' => $format, |
82
|
|
|
'mime' => $this->setMime($format), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
if ($quality) { |
86
|
|
|
$this->transformations = array_merge($this->transformations, [ |
87
|
|
|
'quality' => $quality, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Creates the Storyblok image service URL |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function buildUrl() { |
101
|
|
|
if ($this->transformations === 'svg') { |
|
|
|
|
102
|
|
|
return $this->image->content()['filename']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$transforms = ''; |
106
|
|
|
|
107
|
|
|
if (array_key_exists('fit-in', $this->transformations)) { |
108
|
|
|
$transforms .= '/fit-in'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (array_key_exists('width', $this->transformations)) { |
112
|
|
|
$transforms .= '/' . $this->transformations['width'] . 'x' . $this->transformations['height']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'smart') { |
116
|
|
|
$transforms .= '/smart'; |
117
|
|
|
} |
118
|
|
|
if ($this->hasFilters()) { |
119
|
|
|
$transforms .= $this->applyFilters(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->assetDomain($transforms); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Applies the filters to the image service URL |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
private function applyFilters() { |
131
|
|
|
$filters = '/filters'; |
132
|
|
|
|
133
|
|
|
if (array_key_exists('format', $this->transformations)) { |
134
|
|
|
$filters .= ':format(' . $this->transformations['format'] . ')'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (array_key_exists('quality', $this->transformations)) { |
138
|
|
|
$filters .= ':quality(' . $this->transformations['quality'] . ')'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (array_key_exists('fill', $this->transformations)) { |
142
|
|
|
$filters .= ':fill(' . $this->transformations['fill'] . ')'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point' && $this->image->content()['focus']) { |
146
|
|
|
$filters .= ':focal(' . $this->image->content()['focus'] . ')'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $filters; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Extracts meta details from the image. With Storyblok we can get a |
154
|
|
|
* few things from the URL |
155
|
|
|
* |
156
|
|
|
* @return void|null |
157
|
|
|
*/ |
158
|
|
|
protected function extractMetaDetails() |
159
|
|
|
{ |
160
|
|
|
$path = $this->image->content()['filename']; |
161
|
|
|
|
162
|
|
|
preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/mi', $path, $dimensions, PREG_SET_ORDER, 0); |
163
|
|
|
|
164
|
|
|
if ($dimensions) { |
165
|
|
|
if (Str::endsWith(strtolower($this->image->content()['filename']), '.svg')) { |
166
|
|
|
$this->meta = [ |
167
|
|
|
'height' => null, |
168
|
|
|
'width' => null, |
169
|
|
|
'extension' => 'svg', |
170
|
|
|
'mime' => 'image/svg+xml', |
171
|
|
|
]; |
172
|
|
|
} else { |
173
|
|
|
$this->meta = [ |
174
|
|
|
'height' => $dimensions[0]['height'], |
175
|
|
|
'width' => $dimensions[0]['width'], |
176
|
|
|
'extension' => strtolower($dimensions[0]['extension']), |
177
|
|
|
'mime' => $this->setMime(strtolower($dimensions[0]['extension'])), |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Checks if any filters were applied to the transformation |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
private function hasFilters() { |
189
|
|
|
return array_key_exists('format', $this->transformations) || array_key_exists('quality', $this->transformations) || array_key_exists('fill', $this->transformations) || (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Sets the asset domain |
194
|
|
|
* |
195
|
|
|
* @param $options |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
protected function assetDomain($options = null): string |
199
|
|
|
{ |
200
|
|
|
$resource = str_replace(config('storyblok.asset_domain'), config('storyblok.image_service_domain'), $this->image->content()['filename']); |
201
|
|
|
|
202
|
|
|
return $resource . '/m' . $options; |
203
|
|
|
} |
204
|
|
|
} |