From b649a6751eccd2bf26a2ecc856b9927fa7c22632 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 28 Nov 2021 09:45:34 +0100 Subject: [PATCH] Resize Modifiers --- src/Drivers/Abstract/AbstractImage.php | 4 +- src/Drivers/Gd/Modifiers/FitModifier.php | 20 ++---- src/Drivers/Gd/Modifiers/PadModifier.php | 83 ++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 src/Drivers/Gd/Modifiers/PadModifier.php diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index f9870ace..71b05e9b 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -208,7 +208,7 @@ abstract class AbstractImage $crop = $imagesize->contain($resize)->alignPivotTo($resize, $position); return $this->modify( - $this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor) + $this->resolveDriverClass('Modifiers\PadModifier', $crop, $resize, $backgroundColor) ); } @@ -223,7 +223,7 @@ abstract class AbstractImage return $this->modify( - $this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor) + $this->resolveDriverClass('Modifiers\PadModifier', $crop, $resize, $backgroundColor) ); } diff --git a/src/Drivers/Gd/Modifiers/FitModifier.php b/src/Drivers/Gd/Modifiers/FitModifier.php index ac6bef20..35d07bdd 100644 --- a/src/Drivers/Gd/Modifiers/FitModifier.php +++ b/src/Drivers/Gd/Modifiers/FitModifier.php @@ -11,17 +11,13 @@ use Intervention\Image\Traits\CanResizeGeometrically; class FitModifier implements ModifierInterface { - use CanHandleInput; - protected $crop; protected $resize; - protected $backgroundColor; - public function __construct(SizeInterface $crop, SizeInterface $resize, $backgroundColor = null) + public function __construct(SizeInterface $crop, SizeInterface $resize) { $this->crop = $crop; $this->resize = $resize; - $this->backgroundColor = $backgroundColor; } public function apply(ImageInterface $image): ImageInterface @@ -41,10 +37,6 @@ class FitModifier implements ModifierInterface $this->resize->getHeight() ); - $color = $this->handleInput($this->backgroundColor); - - imagefill($modified, 0, 0, $color->toInt()); - // get current image $current = $frame->getCore(); @@ -65,14 +57,14 @@ class FitModifier implements ModifierInterface imagecopyresampled( $modified, $current, + 0, + 0, $this->crop->getPivot()->getX(), $this->crop->getPivot()->getY(), - 0, - 0, + $this->resize->getWidth(), + $this->resize->getHeight(), $this->crop->getWidth(), - $this->crop->getHeight(), - $frame->getSize()->getWidth(), - $frame->getSize()->getHeight() + $this->crop->getHeight() ); imagedestroy($current); diff --git a/src/Drivers/Gd/Modifiers/PadModifier.php b/src/Drivers/Gd/Modifiers/PadModifier.php new file mode 100644 index 00000000..e0680b77 --- /dev/null +++ b/src/Drivers/Gd/Modifiers/PadModifier.php @@ -0,0 +1,83 @@ +crop = $crop; + $this->resize = $resize; + $this->backgroundColor = $backgroundColor; + } + + public function apply(ImageInterface $image): ImageInterface + { + foreach ($image as $frame) { + $this->modify($frame); + } + + return $image; + } + + protected function modify(FrameInterface $frame): void + { + // create new image + $modified = imagecreatetruecolor( + $this->resize->getWidth(), + $this->resize->getHeight() + ); + + $color = $this->handleInput($this->backgroundColor); + + imagefill($modified, 0, 0, $color->toInt()); + + // get current image + $current = $frame->getCore(); + + // preserve transparency + $transIndex = imagecolortransparent($current); + + if ($transIndex != -1) { + $rgba = imagecolorsforindex($modified, $transIndex); + $transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127); + imagefill($modified, 0, 0, $transColor); + imagecolortransparent($modified, $transColor); + } else { + imagealphablending($modified, false); + imagesavealpha($modified, true); + } + + // copy content from resource + imagecopyresampled( + $modified, + $current, + $this->crop->getPivot()->getX(), + $this->crop->getPivot()->getY(), + 0, + 0, + $this->crop->getWidth(), + $this->crop->getHeight(), + $frame->getSize()->getWidth(), + $frame->getSize()->getHeight() + ); + + imagedestroy($current); + + // set new content as recource + $frame->setCore($modified); + } +}