@@ -166,6 +166,48 @@ public function rotate(int $amount): self
166
166
return $ this ;
167
167
}
168
168
169
+ /**
170
+ * Zooms and crops an image based on focal point. If there is no focal point it will use the center of the image
171
+ *
172
+ * @param int $zoomLevel
173
+ * @param int $width
174
+ * @param int $height
175
+ * @return string
176
+ */
177
+ public function zoomCrop (int $ zoomLevel , int $ width , int $ height ): string
178
+ {
179
+ if ($ this ->transformations === 'svg ' ) {
180
+ return $ this ->assetDomain ($ this ->image ->content ()['filename ' ]);
181
+ }
182
+
183
+ if ($ this ->width () >= $ this ->height ()) {
184
+ $ cropBuffer = $ this ->width () * 100 / $ zoomLevel ;
185
+ } else {
186
+ $ cropBuffer = $ this ->height () * 100 / $ zoomLevel ;
187
+ }
188
+
189
+ if ($ this ->image ->focus ) {
190
+ $ focalPointCoords = explode ('x ' , explode (': ' , $ this ->image ->focus )[0 ]);
191
+ $ focalPoint = [$ focalPointCoords [0 ], $ focalPointCoords [1 ]];
192
+ } else {
193
+ $ focalPoint = [$ this ->width () / 2 , $ this ->height () / 2 ];
194
+ }
195
+
196
+ $ cropLeft = max (round ($ focalPoint [0 ] - $ cropBuffer / 2 ), 0 );
197
+ $ cropTop = max (round ($ focalPoint [1 ] - $ cropBuffer / 2 ), 0 );
198
+ $ cropRight = min (round ($ cropLeft + $ cropBuffer ), $ this ->width ());
199
+ $ cropBottom = min (round ($ cropTop + $ cropBuffer ), $ this ->height ());
200
+
201
+ $ croppedUrl = '/ ' . $ cropLeft . "x " . $ cropTop . ": " . $ cropRight . "x " . $ cropBottom . "/ " . $ width . "x " . $ height ;
202
+
203
+
204
+ if ($ this ->hasFilters ()) {
205
+ $ croppedUrl .= $ this ->applyFilters ();
206
+ }
207
+
208
+ return $ this ->assetDomain ($ croppedUrl );
209
+ }
210
+
169
211
/**
170
212
* Creates the Storyblok image service URL
171
213
*
@@ -174,7 +216,7 @@ public function rotate(int $amount): self
174
216
public function buildUrl (): string
175
217
{
176
218
if ($ this ->transformations === 'svg ' ) {
177
- return $ this ->image ->content ()['filename ' ];
219
+ return $ this ->assetDomain ( $ this -> image ->content ()['filename ' ]) ;
178
220
}
179
221
180
222
$ transforms = '' ;
@@ -312,6 +354,10 @@ protected function assetDomain($options = null): string
312
354
{
313
355
$ resource = str_replace (config ('storyblok.asset_domain ' ), config ('storyblok.image_service_domain ' ), $ this ->image ->content ()['filename ' ]);
314
356
315
- return $ resource . '/m ' . $ options ;
357
+ if ($ options ) {
358
+ return $ resource . '/m ' . $ options ;
359
+ }
360
+
361
+ return $ resource ;
316
362
}
317
363
}
0 commit comments