diff --git a/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php index 555a163f..861d11cb 100644 --- a/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php +++ b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php @@ -18,16 +18,13 @@ class BlendTransparencyModifier extends GenericBlendTransparencyModifier impleme */ public function apply(ImageInterface $image): ImageInterface { - // decode blending color - $color = $this->driver()->handleInput( - $this->color ? $this->color : $this->driver()->config()->blendingColor - ); + $blendingColor = $this->blendingColor($this->driver()); foreach ($image as $frame) { // create new canvas with blending color as background $modified = Cloner::cloneBlended( $frame->native(), - background: $color + background: $blendingColor ); // set new gd image diff --git a/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php index 72c9e653..8fdc0b3b 100644 --- a/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php +++ b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php @@ -13,15 +13,12 @@ class BlendTransparencyModifier extends GenericBlendTransparencyModifier impleme { public function apply(ImageInterface $image): ImageInterface { - // decode blending color - $color = $this->driver()->handleInput( - $this->color ? $this->color : $this->driver()->config()->blendingColor - ); + $blendingColor = $this->blendingColor($this->driver()); - // get imagickpixel from color + // get imagickpixel from blending color $pixel = $this->driver() ->colorProcessor($image->colorspace()) - ->colorToNative($color); + ->colorToNative($blendingColor); // merge transparent areas with the background color foreach ($image as $frame) { diff --git a/src/Modifiers/BlendTransparencyModifier.php b/src/Modifiers/BlendTransparencyModifier.php index 0e8958fb..2e82759a 100644 --- a/src/Modifiers/BlendTransparencyModifier.php +++ b/src/Modifiers/BlendTransparencyModifier.php @@ -4,7 +4,15 @@ declare(strict_types=1); namespace Intervention\Image\Modifiers; +use Intervention\Image\Colors\Rgb\Channels\Blue; +use Intervention\Image\Colors\Rgb\Channels\Green; +use Intervention\Image\Colors\Rgb\Channels\Red; +use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Drivers\SpecializableModifier; +use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\RuntimeException; +use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Interfaces\DriverInterface; class BlendTransparencyModifier extends SpecializableModifier { @@ -16,5 +24,33 @@ class BlendTransparencyModifier extends SpecializableModifier */ public function __construct(public mixed $color = null) { + // + } + + /** + * Decode blending color of current modifier with given driver. Possible + * (semi-)transparent alpha channel values are made opaque. + * + * @throws RuntimeException + * @throws ColorException + * @return ColorInterface + */ + protected function blendingColor(DriverInterface $driver): ColorInterface + { + // decode blending color + $color = $driver->handleInput( + $this->color ?: $driver->config()->blendingColor + ); + + // replace alpha channel value with opaque value + if ($color->isTransparent()) { + return new Color( + $color->channel(Red::class)->value(), + $color->channel(Green::class)->value(), + $color->channel(Blue::class)->value(), + ); + } + + return $color; } } diff --git a/tests/Unit/Drivers/Gd/ImageTest.php b/tests/Unit/Drivers/Gd/ImageTest.php index 0da171b5..7bf676b9 100644 --- a/tests/Unit/Drivers/Gd/ImageTest.php +++ b/tests/Unit/Drivers/Gd/ImageTest.php @@ -301,6 +301,15 @@ final class ImageTest extends GdTestCase $this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0)); } + public function testBlendTransparencyIgnoreTransparencyInBlendingColor(): void + { + $image = $this->readTestImage('gradient.gif'); + $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); + $result = $image->blendTransparency('ff550055'); + $this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0)); + $this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0)); + } + public function testToJpeg(): void { $this->assertMediaType('image/jpeg', $this->image->toJpeg()); diff --git a/tests/Unit/Drivers/Imagick/ImageTest.php b/tests/Unit/Drivers/Imagick/ImageTest.php index 99ca2612..abb628c6 100644 --- a/tests/Unit/Drivers/Imagick/ImageTest.php +++ b/tests/Unit/Drivers/Imagick/ImageTest.php @@ -295,6 +295,15 @@ final class ImageTest extends ImagickTestCase $this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0)); } + public function testBlendTransparencyIgnoreTransparencyInBlendingColor(): void + { + $image = $this->readTestImage('gradient.gif'); + $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); + $result = $image->blendTransparency('ff550055'); + $this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0)); + $this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0)); + } + public function testToJpeg(): void { $this->assertMediaType('image/jpeg', $this->image->toJpeg());