From 55299b2840acfbbb9dfe0d5a47f48bc9d1fd5a8b Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 6 Jan 2024 17:25:15 +0100 Subject: [PATCH] Add BlendTransparencyModifier --- .../Modifiers/BlendTransparencyModifier.php | 31 ++++++++++++++++++ .../Modifiers/BlendTransparencyModifier.php | 32 +++++++++++++++++++ src/Image.php | 11 +++++++ src/Interfaces/ImageInterface.php | 8 +++++ src/Modifiers/BlendTransparencyModifier.php | 10 ++++++ 5 files changed, 92 insertions(+) create mode 100644 src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php create mode 100644 src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php create mode 100644 src/Modifiers/BlendTransparencyModifier.php diff --git a/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php new file mode 100644 index 00000000..0beed8af --- /dev/null +++ b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php @@ -0,0 +1,31 @@ +driver()->handleInput( + $this->color ? $this->color : $image->blendingColor() + ); + + foreach ($image as $frame) { + // create new canvas with blending color as background + $modified = Cloner::cloneBlended( + $frame->native(), + background: $color + ); + + // set new gd image + $frame->setNative($modified); + } + + return $image; + } +} diff --git a/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php new file mode 100644 index 00000000..373635e4 --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php @@ -0,0 +1,32 @@ +driver()->handleInput( + $this->color ? $this->color : $image->blendingColor() + ); + + // get imagickpixel from color + $pixel = $this->driver() + ->colorProcessor($image->colorspace()) + ->colorToNative($color); + + // merge transparent areas with the background color + foreach ($image as $frame) { + $frame->native()->setImageBackgroundColor($pixel); + $frame->native()->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); + $frame->native()->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); + } + + return $image; + } +} diff --git a/src/Image.php b/src/Image.php index d44491aa..4577d700 100644 --- a/src/Image.php +++ b/src/Image.php @@ -45,6 +45,7 @@ use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\ProfileInterface; use Intervention\Image\Interfaces\ResolutionInterface; use Intervention\Image\Interfaces\SizeInterface; +use Intervention\Image\Modifiers\BlendTransparencyModifier; use Intervention\Image\Modifiers\BlurModifier; use Intervention\Image\Modifiers\BrightnessModifier; use Intervention\Image\Modifiers\ColorizeModifier; @@ -402,6 +403,16 @@ final class Image implements ImageInterface return $this; } + /** + * {@inheritdoc} + * + * @see ImageInterface::blendTransparency() + */ + public function blendTransparency(mixed $color = null): ImageInterface + { + return $this->modify(new BlendTransparencyModifier($color)); + } + /** * {@inheritdoc} * diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 84dc34d1..95c9e3ef 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -202,6 +202,14 @@ interface ImageInterface extends IteratorAggregate, Countable */ public function setBlendingColor(mixed $color): ImageInterface; + /** + * Replace transparent areas of the image with given color + * + * @param mixed $color + * @return ImageInterface + */ + public function blendTransparency(mixed $color = null): ImageInterface; + /** * Retrieve ICC color profile of image * diff --git a/src/Modifiers/BlendTransparencyModifier.php b/src/Modifiers/BlendTransparencyModifier.php new file mode 100644 index 00000000..5f7a782e --- /dev/null +++ b/src/Modifiers/BlendTransparencyModifier.php @@ -0,0 +1,10 @@ +