1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 17:41:58 +02:00

Add BlendTransparencyModifier

This commit is contained in:
Oliver Vogel
2024-01-06 17:25:15 +01:00
parent 99150c4e1d
commit 55299b2840
5 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Interfaces\ImageInterface;
class BlendTransparencyModifier extends DriverSpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
// decode blending color
$color = $this->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;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Imagick;
use Intervention\Image\Drivers\DriverSpecializedModifier;
use Intervention\Image\Interfaces\ImageInterface;
class BlendTransparencyModifier extends DriverSpecializedModifier
{
public function apply(ImageInterface $image): ImageInterface
{
// decode blending color
$color = $this->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;
}
}

View File

@@ -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}
*

View File

@@ -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
*

View File

@@ -0,0 +1,10 @@
<?php
namespace Intervention\Image\Modifiers;
class BlendTransparencyModifier extends AbstractModifier
{
public function __construct(public mixed $color = null)
{
}
}