Skip to content

Commit

Permalink
Replace (semi-)transparent alpha values in BlendTransparencyModifier:…
Browse files Browse the repository at this point in the history
…:class
  • Loading branch information
olivervogel committed Jan 4, 2025
1 parent 1c68e5f commit 4c48f7a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions src/Modifiers/BlendTransparencyModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

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
{
Expand All @@ -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;
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Drivers/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ public function testBlendTransparencyArgument(): void
$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());
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Drivers/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ public function testBlendTransparencyArgument(): void
$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());
Expand Down

0 comments on commit 4c48f7a

Please sign in to comment.