1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-20 12:41:23 +02:00

Rename Image::blendTransparency() to Image::background()

This commit is contained in:
Oliver Vogel
2025-06-20 17:09:19 +02:00
parent 2fb891da36
commit a20c9833da
16 changed files with 96 additions and 84 deletions

9
changelog.md Normal file
View File

@@ -0,0 +1,9 @@
## New Features
- ImageInterface::version()
## API Changes
- ImageInterface::blendTransparency() was renamed to ImageInterface::background()
- ImageInterface::setBlendingColor() was renamed to ImageInterface::setBackgroundColor()
- ImageInterface::blendingColor() was renamed to ImageInterface::backgroundColor()

View File

@@ -16,7 +16,7 @@ class Config
public function __construct(
public bool $autoOrientation = true,
public bool $decodeAnimation = true,
public mixed $blendingColor = 'ffffff',
public mixed $backgroundColor = 'ffffff',
public bool $strip = false,
) {
//

View File

@@ -19,13 +19,13 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$blendingColor = $this->driver()->handleInput(
$this->driver()->config()->blendingColor
$backgroundColor = $this->driver()->handleInput(
$this->driver()->config()->backgroundColor
);
$output = Cloner::cloneBlended(
$image->core()->native(),
background: $blendingColor
background: $backgroundColor
);
return $this->createEncodedImage(function ($pointer) use ($output): void {

View File

@@ -7,9 +7,9 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\BlendTransparencyModifier as GenericBlendTransparencyModifier;
use Intervention\Image\Modifiers\BackgroundModifier as GenericBackgroundModifier;
class BlendTransparencyModifier extends GenericBlendTransparencyModifier implements SpecializedInterface
class BackgroundModifier extends GenericBackgroundModifier implements SpecializedInterface
{
/**
* {@inheritdoc}
@@ -18,13 +18,13 @@ class BlendTransparencyModifier extends GenericBlendTransparencyModifier impleme
*/
public function apply(ImageInterface $image): ImageInterface
{
$blendingColor = $this->blendingColor($this->driver());
$backgroundColor = $this->backgroundColor($this->driver());
foreach ($image as $frame) {
// create new canvas with blending color as background
// create new canvas with background color as background
$modified = Cloner::cloneBlended(
$frame->native(),
background: $blendingColor
background: $backgroundColor
);
// set new gd image

View File

@@ -28,12 +28,12 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter
$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$background = $this->driver()->handleInput($this->background);
$blendingColor = $this->driver()->handleInput(
$this->driver()->config()->blendingColor
$backgroundColor = $this->driver()->handleInput(
$this->driver()->config()->backgroundColor
);
foreach ($image as $frame) {
$this->modify($frame, $crop, $resize, $background, $blendingColor);
$this->modify($frame, $crop, $resize, $background, $backgroundColor);
}
return $image;
@@ -47,7 +47,7 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter
SizeInterface $crop,
SizeInterface $resize,
ColorInterface $background,
ColorInterface $blendingColor
ColorInterface $backgroundColor
): void {
// create new gd image
$modified = Cloner::cloneEmpty($frame->native(), $resize, $background);
@@ -56,9 +56,9 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter
// even if background-color is set
$transparent = imagecolorallocatealpha(
$modified,
$blendingColor->channel(Red::class)->value(),
$blendingColor->channel(Green::class)->value(),
$blendingColor->channel(Blue::class)->value(),
$backgroundColor->channel(Red::class)->value(),
$backgroundColor->channel(Green::class)->value(),
$backgroundColor->channel(Blue::class)->value(),
127,
);
imagealphablending($modified, false); // do not blend / just overwrite
@@ -72,7 +72,7 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter
$transparent
);
// copy image from original with blending alpha
// copy image from original with background alpha
imagealphablending($modified, true);
imagecopyresampled(
$modified,

View File

@@ -36,13 +36,13 @@ class QuantizeColorsModifier extends GenericQuantizeColorsModifier implements Sp
$this->driver()->handleInput($this->background)
);
$blendingColor = $this->driver()->handleInput(
$this->driver()->config()->blendingColor
$backgroundColor = $this->driver()->handleInput(
$this->driver()->config()->backgroundColor
);
foreach ($image as $frame) {
// create new image for color quantization
$reduced = Cloner::cloneEmpty($frame->native(), background: $blendingColor);
$reduced = Cloner::cloneEmpty($frame->native(), background: $backgroundColor);
// fill with background
imagefill($reduced, 0, 0, $background);

View File

@@ -18,14 +18,14 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
{
$format = 'JPEG';
$compression = Imagick::COMPRESSION_JPEG;
$blendingColor = $this->driver()->handleInput(
$this->driver()->config()->blendingColor
$backgroundColor = $this->driver()->handleInput(
$this->driver()->config()->backgroundColor
);
// resolve blending color because jpeg has no transparency
// resolve background color because jpeg has no transparency
$background = $this->driver()
->colorProcessor($image->colorspace())
->colorToNative($blendingColor);
->colorToNative($backgroundColor);
// set alpha value to 1 because Imagick renders
// possible full transparent colors as black

View File

@@ -7,18 +7,18 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Imagick;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\BlendTransparencyModifier as GenericBlendTransparencyModifier;
use Intervention\Image\Modifiers\BackgroundModifier as GenericBackgroundModifier;
class BlendTransparencyModifier extends GenericBlendTransparencyModifier implements SpecializedInterface
class BackgroundModifier extends GenericBackgroundModifier implements SpecializedInterface
{
public function apply(ImageInterface $image): ImageInterface
{
$blendingColor = $this->blendingColor($this->driver());
$backgroundColor = $this->backgroundColor($this->driver());
// get imagickpixel from blending color
// get imagickpixel from background color
$pixel = $this->driver()
->colorProcessor($image->colorspace())
->colorToNative($blendingColor);
->colorToNative($backgroundColor);
// merge transparent areas with the background color
foreach ($image as $frame) {

View File

@@ -57,7 +57,7 @@ use Intervention\Image\Interfaces\ProfileInterface;
use Intervention\Image\Interfaces\ResolutionInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Modifiers\AlignRotationModifier;
use Intervention\Image\Modifiers\BlendTransparencyModifier;
use Intervention\Image\Modifiers\BackgroundModifier;
use Intervention\Image\Modifiers\BlurModifier;
use Intervention\Image\Modifiers\BrightnessModifier;
use Intervention\Image\Modifiers\ColorizeModifier;
@@ -406,24 +406,24 @@ final class Image implements ImageInterface
/**
* {@inheritdoc}
*
* @see ImageInterface::blendingColor()
* @see ImageInterface::backgroundColor()
*/
public function blendingColor(): ColorInterface
public function backgroundColor(): ColorInterface
{
return $this->driver()->handleInput(
$this->driver()->config()->blendingColor
$this->driver()->config()->backgroundColor
);
}
/**
* {@inheritdoc}
*
* @see ImageInterface::setBlendingColor()
* @see ImageInterface::setBackgroundColor()
*/
public function setBlendingColor(mixed $color): ImageInterface
public function setBackgroundColor(mixed $color): ImageInterface
{
$this->driver()->config()->setOptions(
blendingColor: $this->driver()->handleInput($color)
backgroundColor: $this->driver()->handleInput($color)
);
return $this;
@@ -432,11 +432,11 @@ final class Image implements ImageInterface
/**
* {@inheritdoc}
*
* @see ImageInterface::blendTransparency()
* @see ImageInterface::background()
*/
public function blendTransparency(mixed $color = null): ImageInterface
public function background(mixed $color = null): ImageInterface
{
return $this->modify(new BlendTransparencyModifier($color));
return $this->modify(new BackgroundModifier($color));
}
/**

View File

@@ -223,22 +223,25 @@ interface ImageInterface extends IteratorAggregate, Countable
*
* @throws RuntimeException
*/
public function blendingColor(): ColorInterface;
public function backgroundColor(): ColorInterface;
/**
* Set blending color will have no effect unless image is converted into a format
* which does not support transparency.
* Set the background color to be used with self::background()
*
* Settting the background color will have no effect unless image is
* converted into a format which does not support transparency or
* self::background() is used.
*
* @throws RuntimeException
*/
public function setBlendingColor(mixed $color): self;
public function setBackgroundColor(mixed $color): self;
/**
* Replace transparent areas of the image with given color
*
* @throws RuntimeException
*/
public function blendTransparency(mixed $color = null): self;
public function background(mixed $color = null): self;
/**
* Retrieve ICC color profile of image

View File

@@ -14,7 +14,7 @@ use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DriverInterface;
class BlendTransparencyModifier extends SpecializableModifier
class BackgroundModifier extends SpecializableModifier
{
/**
* Create new modifier object
@@ -27,17 +27,17 @@ class BlendTransparencyModifier extends SpecializableModifier
}
/**
* Decode blending color of current modifier with given driver. Possible
* Decode background color of current modifier with given driver. Possible
* (semi-)transparent alpha channel values are made opaque.
*
* @throws RuntimeException
* @throws ColorException
*/
protected function blendingColor(DriverInterface $driver): ColorInterface
protected function backgroundColor(DriverInterface $driver): ColorInterface
{
// decode blending color
// decode background color
$color = $driver->handleInput(
$this->color ?: $driver->config()->blendingColor
$this->color ?: $driver->config()->backgroundColor
);
// replace alpha channel value with opaque value

View File

@@ -18,12 +18,12 @@ final class ConfigTest extends BaseTestCase
$this->assertTrue($config->autoOrientation);
$this->assertTrue($config->decodeAnimation);
$this->assertEquals('ffffff', $config->blendingColor);
$this->assertEquals('ffffff', $config->backgroundColor);
$config = new Config(
autoOrientation: false,
decodeAnimation: false,
blendingColor: 'f00',
backgroundColor: 'f00',
strip: true,
);
$this->assertInstanceOf(Config::class, $config);
@@ -31,7 +31,7 @@ final class ConfigTest extends BaseTestCase
$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('f00', $config->blendingColor);
$this->assertEquals('f00', $config->backgroundColor);
}
public function testGetSetOptions(): void
@@ -40,35 +40,35 @@ final class ConfigTest extends BaseTestCase
$this->assertTrue($config->autoOrientation);
$this->assertTrue($config->decodeAnimation);
$this->assertFalse($config->strip);
$this->assertEquals('ffffff', $config->blendingColor);
$this->assertEquals('ffffff', $config->backgroundColor);
$result = $config->setOptions(
autoOrientation: false,
decodeAnimation: false,
blendingColor: 'f00',
backgroundColor: 'f00',
strip: true,
);
$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertEquals('f00', $config->blendingColor);
$this->assertEquals('f00', $config->backgroundColor);
$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('f00', $result->blendingColor);
$this->assertEquals('f00', $result->backgroundColor);
$result = $config->setOptions(blendingColor: '000');
$result = $config->setOptions(backgroundColor: '000');
$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('000', $config->blendingColor);
$this->assertEquals('000', $config->backgroundColor);
$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('000', $result->blendingColor);
$this->assertEquals('000', $result->backgroundColor);
}
public function testSetOptionsWithArray(): void
@@ -77,17 +77,17 @@ final class ConfigTest extends BaseTestCase
$result = $config->setOptions([
'autoOrientation' => false,
'decodeAnimation' => false,
'blendingColor' => 'f00',
'backgroundColor' => 'f00',
'strip' => true,
]);
$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('f00', $config->blendingColor);
$this->assertEquals('f00', $config->backgroundColor);
$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('f00', $result->blendingColor);
$this->assertEquals('f00', $result->backgroundColor);
}
}

View File

@@ -283,29 +283,29 @@ final class ImageTest extends GdTestCase
$this->assertInstanceOf(Image::class, $this->image->text('test', 0, 0, new Font()));
}
public function testBlendTransparencyDefault(): void
public function testBackgroundDefault(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 255, 255, 255, $image->pickColor(1, 0));
$this->assertColor(255, 255, 255, 255, $result->pickColor(1, 0));
}
public function testBlendTransparencyArgument(): void
public function testBackgroundArgument(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency('ff5500');
$result = $image->background('ff5500');
$this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
}
public function testBlendTransparencyIgnoreTransparencyInBlendingColor(): void
public function testBackgroundIgnoreTransparencyInBackgroundColor(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency('ff550055');
$result = $image->background('ff550055');
$this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
}

View File

@@ -277,29 +277,29 @@ final class ImageTest extends ImagickTestCase
$this->assertInstanceOf(Image::class, $this->image->sharpen(12));
}
public function testBlendTransparencyDefault(): void
public function testBackgroundDefault(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 255, 255, 255, $image->pickColor(1, 0));
$this->assertColor(255, 255, 255, 255, $result->pickColor(1, 0));
}
public function testBlendTransparencyArgument(): void
public function testBackgroundArgument(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency('ff5500');
$result = $image->background('ff5500');
$this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
}
public function testBlendTransparencyIgnoreTransparencyInBlendingColor(): void
public function testBackgroundIgnoreTransparencyInBackgroundColor(): void
{
$image = $this->readTestImage('gradient.gif');
$this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0));
$result = $image->blendTransparency('ff550055');
$result = $image->background('ff550055');
$this->assertColor(255, 85, 0, 255, $image->pickColor(1, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(1, 0));
}

View File

@@ -137,20 +137,20 @@ final class ImageManagerTestGd extends BaseTestCase
$this->assertFalse($image->isAnimated());
}
public function testApplyBlendingColorDefault(): void
public function testApplyBackgroundColorDefault(): void
{
$manager = new ImageManager(Driver::class);
$image = $manager->read($this->getTestResourcePath('blocks.png'));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 255, 255, 255, $image->pickColor(530, 0));
$this->assertColor(255, 255, 255, 255, $result->pickColor(530, 0));
}
public function testApplyBlendingColorConfigured(): void
public function testApplyBackgroundColorConfigured(): void
{
$manager = new ImageManager(Driver::class, blendingColor: 'ff5500');
$manager = new ImageManager(Driver::class, backgroundColor: 'ff5500');
$image = $manager->read($this->getTestResourcePath('blocks.png'));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 85, 0, 255, $image->pickColor(530, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(530, 0));
}

View File

@@ -137,20 +137,20 @@ final class ImageManagerTestImagick extends BaseTestCase
$this->assertFalse($image->isAnimated());
}
public function testApplyBlendingColor(): void
public function testApplyBackgroundColor(): void
{
$manager = new ImageManager(Driver::class);
$image = $manager->read($this->getTestResourcePath('blocks.png'));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 255, 255, 255, $image->pickColor(530, 0));
$this->assertColor(255, 255, 255, 255, $result->pickColor(530, 0));
}
public function testApplyBlendingColorConfigured(): void
public function testApplyBackgroundColorConfigured(): void
{
$manager = new ImageManager(Driver::class, blendingColor: 'ff5500');
$manager = new ImageManager(Driver::class, backgroundColor: 'ff5500');
$image = $manager->read($this->getTestResourcePath('blocks.png'));
$result = $image->blendTransparency();
$result = $image->background();
$this->assertColor(255, 85, 0, 255, $image->pickColor(530, 0));
$this->assertColor(255, 85, 0, 255, $result->pickColor(530, 0));
}