Skip to content

Commit

Permalink
feat(image resize): smart crop (ems-project#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 authored Oct 24, 2023
1 parent 8fd9964 commit 6c9c7a4
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 3 deletions.
17 changes: 14 additions & 3 deletions EMS/common-bundle/src/Storage/Processor/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use EMS\CommonBundle\Common\Standard\Type;
use EMS\CommonBundle\Helper\EmsFields;
use EMS\Helpers\Image\SmartCrop;
use EMS\Helpers\Standard\Color;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -140,11 +141,21 @@ private function fillBackgroundColor(\GdImage $temp): void

private function applyResizeAndBackground(\GdImage $image, int $width, int $height, int $originalWidth, int $originalHeight): \GdImage
{
$temp = $this->imageCreate($width, $height);
$resize = $this->config->getResize();
if ('smartCrop' === $resize) {
$smartCrop = new SmartCrop($image, $width, $height);
$res = $smartCrop->analyse();
if (null === $res['topCrop']) {
$resize = 'fillArea';
} else {
$smartCrop->crop($res['topCrop']['x'], $res['topCrop']['y'], $res['topCrop']['width'], $res['topCrop']['height']);

$this->fillBackgroundColor($temp);
return $smartCrop->get();
}
}

$resize = $this->config->getResize();
$temp = $this->imageCreate($width, $height);
$this->fillBackgroundColor($temp);
$gravity = $this->config->getGravity();

if ('fillArea' == $resize) {
Expand Down
Loading

0 comments on commit 6c9c7a4

Please sign in to comment.