From 585766b4af48ad91b084cf453e445d8c11717824 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 8 Nov 2021 19:52:45 +0100 Subject: [PATCH] FitModifier --- src/Drivers/Abstract/AbstractImage.php | 9 +++++++ src/Drivers/Gd/Modifiers/FitDownModifier.php | 22 ++++++++++++++++ src/Drivers/Gd/Modifiers/FitModifier.php | 27 +++++++++----------- src/Geometry/Resizer.php | 2 +- 4 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 src/Drivers/Gd/Modifiers/FitDownModifier.php diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index f734c03b..a6a52719 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -175,6 +175,15 @@ abstract class AbstractImage ); } + public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface + { + $size = new Size($width, $height); + + return $this->modify( + $this->resolveDriverClass('Modifiers\FitDownModifier', $size, $position) + ); + } + public function place($element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0): ImageInterface { return $this->modify( diff --git a/src/Drivers/Gd/Modifiers/FitDownModifier.php b/src/Drivers/Gd/Modifiers/FitDownModifier.php new file mode 100644 index 00000000..143b4bef --- /dev/null +++ b/src/Drivers/Gd/Modifiers/FitDownModifier.php @@ -0,0 +1,22 @@ +resizeGeometrically($this->getCropSize($image)) + ->toWidth($this->target->getWidth()) + ->toHeight($this->target->getHeight()) + ->scaleDown(); + } +} diff --git a/src/Drivers/Gd/Modifiers/FitModifier.php b/src/Drivers/Gd/Modifiers/FitModifier.php index 2f43650c..ba1702ec 100644 --- a/src/Drivers/Gd/Modifiers/FitModifier.php +++ b/src/Drivers/Gd/Modifiers/FitModifier.php @@ -10,18 +10,6 @@ use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Traits\CanResizeGeometrically; -/* - -# contain -1. Scale (keep aspect ratio) Original to fit Target -2. Scale (keep aspect ratio) Up/Down to fit Target (obsolete) - -# cover -1. Scale (keep aspect ratio) Target to fit Original -2. Scale (keep aspect ratio) Up/Down to fit Target - - */ - class FitModifier extends ResizeModifier implements ModifierInterface { use CanResizeGeometrically; @@ -49,13 +37,22 @@ class FitModifier extends ResizeModifier implements ModifierInterface protected function getCropSize(ImageInterface $image): SizeInterface { + $imagesize = $image->getSize(); + + // auto height $size = $this->resizeGeometrically($this->target) - ->toWidth($image->width()) - ->toHeight($image->height()) + ->toWidth($imagesize->getWidth()) ->scale(); + if (!$size->fitsInto($imagesize)) { + // auto width + $size = $this->resizeGeometrically($this->target) + ->toHeight($imagesize->getHeight()) + ->scale(); + } + return $size->alignPivotTo( - $image->getSize()->alignPivot($this->position), + $imagesize->alignPivot($this->position), $this->position ); } diff --git a/src/Geometry/Resizer.php b/src/Geometry/Resizer.php index 92c43668..b9b79923 100644 --- a/src/Geometry/Resizer.php +++ b/src/Geometry/Resizer.php @@ -94,7 +94,7 @@ class Resizer public function toWidth(int $width): self { - return $this->height($width); + return $this->width($width); } public function toHeight(int $height): self