Skip to content

Commit e8fd441

Browse files
committed
2 parents 08ee243 + 9f5bdf5 commit e8fd441

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

.idea/php.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
"imgix/imgix-php": "^3.3|^4.0",
2828
"ivopetkov/html5-dom-document-php": "2.*",
2929
"league/commonmark": "^2.0",
30+
"spatie/laravel-ignition": "^2.8",
3031
"spatie/schema-org": "^3.3",
3132
"storyblok/php-client": "^2.3",
3233
"storyblok/richtext-resolver": "^2.2"
3334
},
3435
"require-dev": {
3536
"mockery/mockery": "^1.2",
3637
"orchestra/testbench": "^8.0|^9.0",
37-
"phpunit/phpunit": "^10",
38-
"spatie/laravel-ignition": "^2.0"
38+
"phpunit/phpunit": "^10"
3939
},
4040
"autoload": {
4141
"psr-4": {

src/Support/ImageTransformers/Storyblok.php

+48-2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ public function rotate(int $amount): self
166166
return $this;
167167
}
168168

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+
169211
/**
170212
* Creates the Storyblok image service URL
171213
*
@@ -174,7 +216,7 @@ public function rotate(int $amount): self
174216
public function buildUrl(): string
175217
{
176218
if ($this->transformations === 'svg') {
177-
return $this->image->content()['filename'];
219+
return $this->assetDomain($this->image->content()['filename']);
178220
}
179221

180222
$transforms = '';
@@ -312,6 +354,10 @@ protected function assetDomain($options = null): string
312354
{
313355
$resource = str_replace(config('storyblok.asset_domain'), config('storyblok.image_service_domain'), $this->image->content()['filename']);
314356

315-
return $resource . '/m' . $options;
357+
if ($options) {
358+
return $resource . '/m' . $options;
359+
}
360+
361+
return $resource;
316362
}
317363
}

0 commit comments

Comments
 (0)