1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-25 23:06:13 +02:00

Replace (semi-)transparent alpha values in BlendTransparencyModifier::class

This commit is contained in:
Oliver Vogel
2025-01-04 10:03:46 +01:00
parent 1c68e5fdf4
commit 4c48f7a143
5 changed files with 59 additions and 11 deletions

View File

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

View File

@@ -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) {

View File

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

View File

@@ -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());

View File

@@ -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());