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:
@@ -18,16 +18,13 @@ class BlendTransparencyModifier extends GenericBlendTransparencyModifier impleme
|
|||||||
*/
|
*/
|
||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
// decode blending color
|
$blendingColor = $this->blendingColor($this->driver());
|
||||||
$color = $this->driver()->handleInput(
|
|
||||||
$this->color ? $this->color : $this->driver()->config()->blendingColor
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
// create new canvas with blending color as background
|
// create new canvas with blending color as background
|
||||||
$modified = Cloner::cloneBlended(
|
$modified = Cloner::cloneBlended(
|
||||||
$frame->native(),
|
$frame->native(),
|
||||||
background: $color
|
background: $blendingColor
|
||||||
);
|
);
|
||||||
|
|
||||||
// set new gd image
|
// set new gd image
|
||||||
|
@@ -13,15 +13,12 @@ class BlendTransparencyModifier extends GenericBlendTransparencyModifier impleme
|
|||||||
{
|
{
|
||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
// decode blending color
|
$blendingColor = $this->blendingColor($this->driver());
|
||||||
$color = $this->driver()->handleInput(
|
|
||||||
$this->color ? $this->color : $this->driver()->config()->blendingColor
|
|
||||||
);
|
|
||||||
|
|
||||||
// get imagickpixel from color
|
// get imagickpixel from blending color
|
||||||
$pixel = $this->driver()
|
$pixel = $this->driver()
|
||||||
->colorProcessor($image->colorspace())
|
->colorProcessor($image->colorspace())
|
||||||
->colorToNative($color);
|
->colorToNative($blendingColor);
|
||||||
|
|
||||||
// merge transparent areas with the background color
|
// merge transparent areas with the background color
|
||||||
foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
|
@@ -4,7 +4,15 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image\Modifiers;
|
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\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
|
class BlendTransparencyModifier extends SpecializableModifier
|
||||||
{
|
{
|
||||||
@@ -16,5 +24,33 @@ class BlendTransparencyModifier extends SpecializableModifier
|
|||||||
*/
|
*/
|
||||||
public function __construct(public mixed $color = null)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -301,6 +301,15 @@ final class ImageTest extends GdTestCase
|
|||||||
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
|
$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
|
public function testToJpeg(): void
|
||||||
{
|
{
|
||||||
$this->assertMediaType('image/jpeg', $this->image->toJpeg());
|
$this->assertMediaType('image/jpeg', $this->image->toJpeg());
|
||||||
|
@@ -295,6 +295,15 @@ final class ImageTest extends ImagickTestCase
|
|||||||
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
|
$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
|
public function testToJpeg(): void
|
||||||
{
|
{
|
||||||
$this->assertMediaType('image/jpeg', $this->image->toJpeg());
|
$this->assertMediaType('image/jpeg', $this->image->toJpeg());
|
||||||
|
Reference in New Issue
Block a user