From e86e8d8aae8e0189f6f147c4dbea37262536129f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Fri, 5 Jan 2024 17:12:09 +0100 Subject: [PATCH] Fix bug Given rotation background color was ignored when encoding from image with transparency to image with no alpha channel. --- src/Drivers/Gd/Modifiers/RotateModifier.php | 24 ++++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Drivers/Gd/Modifiers/RotateModifier.php b/src/Drivers/Gd/Modifiers/RotateModifier.php index d59b74e8..2815631b 100644 --- a/src/Drivers/Gd/Modifiers/RotateModifier.php +++ b/src/Drivers/Gd/Modifiers/RotateModifier.php @@ -8,9 +8,9 @@ use Intervention\Image\Drivers\Gd\SpecializedModifier; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Modifiers\FillModifier; /** * @method mixed rotationAngle() @@ -23,7 +23,7 @@ public function apply(ImageInterface $image): ImageInterface $background = $this->driver()->handleInput($this->background); foreach ($image as $frame) { - $this->modifyFrame($frame, $background); + $this->modifyFrame($frame, $background, $image->colorspace()); } return $image; @@ -35,10 +35,14 @@ public function apply(ImageInterface $image): ImageInterface * * @param FrameInterface $frame * @param ColorInterface $background + * @param ColorspaceInterface $colorspace * @return void */ - protected function modifyFrame(FrameInterface $frame, ColorInterface $background): void - { + protected function modifyFrame( + FrameInterface $frame, + ColorInterface $background, + ColorspaceInterface $colorspace + ): void { // get transparent color from frame core $transparent = match ($transparent = imagecolortransparent($frame->native())) { -1 => imagecolorallocatealpha( @@ -74,12 +78,16 @@ protected function modifyFrame(FrameInterface $frame, ColorInterface $background ->rotate($this->rotationAngle() * -1); // create new gd image - $modified = $this->driver()->createImage( + $modified = imagecreatetruecolor( imagesx($rotated), imagesy($rotated) - )->modify(new FillModifier($background)) - ->core() - ->native(); + ); + + // fill new gd with background color + $transColor = $this->driver()->colorProcessor($colorspace)->colorToNative($background); + imagealphablending($modified, true); + imagefill($modified, 0, 0, $transColor); + imagecolortransparent($modified, $transColor); // retain resolution $this->copyResolution($frame->native(), $modified);