Skip to content

Commit

Permalink
Merge pull request #54 from RicLeP/zoom-crop
Browse files Browse the repository at this point in the history
Added new `zoomCrop` method to the Storyblok image transformer
  • Loading branch information
RicLeP authored Jul 1, 2024
2 parents 55dec02 + fcafdbd commit 9f5bdf5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
"imgix/imgix-php": "^3.3|^4.0",
"ivopetkov/html5-dom-document-php": "2.*",
"league/commonmark": "^2.0",
"spatie/laravel-ignition": "^2.8",
"spatie/schema-org": "^3.3",
"storyblok/php-client": "^2.3",
"storyblok/richtext-resolver": "^2.2"
},
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10",
"spatie/laravel-ignition": "^2.0"
"phpunit/phpunit": "^10"
},
"autoload": {
"psr-4": {
Expand Down
50 changes: 48 additions & 2 deletions src/Support/ImageTransformers/Storyblok.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ public function rotate(int $amount): self
return $this;
}

/**
* Zooms and crops an image based on focal point. If there is no focal point it will use the center of the image
*
* @param int $zoomLevel
* @param int $width
* @param int $height
* @return string
*/
public function zoomCrop(int $zoomLevel, int $width, int $height): string
{
if ($this->transformations === 'svg') {
return $this->assetDomain($this->image->content()['filename']);
}

if ($this->width() >= $this->height()) {
$cropBuffer = $this->width() * 100 / $zoomLevel;
} else {
$cropBuffer = $this->height() * 100 / $zoomLevel;
}

if ($this->image->focus) {
$focalPointCoords = explode('x', explode(':', $this->image->focus)[0]);
$focalPoint = [$focalPointCoords[0], $focalPointCoords[1]];
} else {
$focalPoint = [$this->width() / 2, $this->height() / 2];
}

$cropLeft = max(round($focalPoint[0] - $cropBuffer / 2), 0);
$cropTop = max(round($focalPoint[1] - $cropBuffer / 2), 0);
$cropRight = min(round($cropLeft + $cropBuffer), $this->width());
$cropBottom = min(round($cropTop + $cropBuffer), $this->height());

$croppedUrl = '/' . $cropLeft . "x" . $cropTop . ":" . $cropRight . "x" . $cropBottom . "/" . $width . "x" . $height;


if ($this->hasFilters()) {
$croppedUrl .= $this->applyFilters();
}

return $this->assetDomain($croppedUrl);
}

/**
* Creates the Storyblok image service URL
*
Expand All @@ -174,7 +216,7 @@ public function rotate(int $amount): self
public function buildUrl(): string
{
if ($this->transformations === 'svg') {
return $this->image->content()['filename'];
return $this->assetDomain($this->image->content()['filename']);
}

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

return $resource . '/m' . $options;
if ($options) {
return $resource . '/m' . $options;
}

return $resource;
}
}

0 comments on commit 9f5bdf5

Please sign in to comment.