mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 17:19:50 +02:00
Add BlendTransparencyModifier
This commit is contained in:
31
src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php
Normal file
31
src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php
Normal 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;
|
||||
}
|
||||
}
|
32
src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php
Normal file
32
src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php
Normal 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;
|
||||
}
|
||||
}
|
@@ -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}
|
||||
*
|
||||
|
@@ -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
|
||||
*
|
||||
|
10
src/Modifiers/BlendTransparencyModifier.php
Normal file
10
src/Modifiers/BlendTransparencyModifier.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Modifiers;
|
||||
|
||||
class BlendTransparencyModifier extends AbstractModifier
|
||||
{
|
||||
public function __construct(public mixed $color = null)
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user