diff --git a/src/Config.php b/src/Config.php index 654af169..26475381 100644 --- a/src/Config.php +++ b/src/Config.php @@ -24,7 +24,7 @@ class Config implements ConfigInterface public function __construct( protected bool $autoOrientation = true, protected bool $decodeAnimation = true, - protected mixed $blendingColor = 'ffffff00', + protected mixed $blendingColor = 'ffffff', ) { } diff --git a/src/Drivers/Gd/Encoders/JpegEncoder.php b/src/Drivers/Gd/Encoders/JpegEncoder.php index dd0a4e71..2fa03d76 100644 --- a/src/Drivers/Gd/Encoders/JpegEncoder.php +++ b/src/Drivers/Gd/Encoders/JpegEncoder.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Gd\Encoders; +use Intervention\Image\Config; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Encoders\JpegEncoder as GenericJpegEncoder; use Intervention\Image\EncodedImage; @@ -14,7 +15,14 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface { public function encode(ImageInterface $image): EncodedImage { - $output = Cloner::cloneBlended($image->core()->native(), background: $image->blendingColor()); + $blendingColor = $this->driver()->handleInput( + $this->driver()->config()->option(Config::BLENDING_COLOR) + ); + + $output = Cloner::cloneBlended( + $image->core()->native(), + background: $blendingColor + ); $data = $this->buffered(function () use ($output) { imageinterlace($output, $this->progressive); diff --git a/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php index 33e3abac..eda71b3c 100644 --- a/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php +++ b/src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; +use Intervention\Image\Config; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; @@ -15,7 +16,7 @@ public function apply(ImageInterface $image): ImageInterface { // decode blending color $color = $this->driver()->handleInput( - $this->color ? $this->color : $image->blendingColor() + $this->color ? $this->color : $this->driver()->config()->option(Config::BLENDING_COLOR) ); foreach ($image as $frame) { diff --git a/src/Drivers/Gd/Modifiers/ContainModifier.php b/src/Drivers/Gd/Modifiers/ContainModifier.php index 148d2c9e..54d363ba 100644 --- a/src/Drivers/Gd/Modifiers/ContainModifier.php +++ b/src/Drivers/Gd/Modifiers/ContainModifier.php @@ -7,6 +7,7 @@ use Intervention\Image\Colors\Rgb\Channels\Blue; use Intervention\Image\Colors\Rgb\Channels\Green; use Intervention\Image\Colors\Rgb\Channels\Red; +use Intervention\Image\Config; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Interfaces\ColorInterface; @@ -23,7 +24,9 @@ public function apply(ImageInterface $image): ImageInterface $crop = $this->getCropSize($image); $resize = $this->getResizeSize($image); $background = $this->driver()->handleInput($this->background); - $blendingColor = $image->blendingColor(); + $blendingColor = $this->driver()->handleInput( + $this->driver()->config()->option(Config::BLENDING_COLOR) + ); foreach ($image as $frame) { $this->modify($frame, $crop, $resize, $background, $blendingColor); diff --git a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php index 8d7aa948..6c647e78 100644 --- a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php +++ b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; +use Intervention\Image\Config; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\InputException; use Intervention\Image\Interfaces\ImageInterface; @@ -31,9 +32,13 @@ public function apply(ImageInterface $image): ImageInterface $this->driver()->handleInput($this->background) ); + $blendingColor = $this->driver()->handleInput( + $this->driver()->config()->option(Config::BLENDING_COLOR) + ); + foreach ($image as $frame) { // create new image for color quantization - $reduced = Cloner::cloneEmpty($frame->native(), background: $image->blendingColor()); + $reduced = Cloner::cloneEmpty($frame->native(), background: $blendingColor); // fill with background imagefill($reduced, 0, 0, $background); diff --git a/src/Drivers/Imagick/Encoders/JpegEncoder.php b/src/Drivers/Imagick/Encoders/JpegEncoder.php index b70f0e36..f99627ed 100644 --- a/src/Drivers/Imagick/Encoders/JpegEncoder.php +++ b/src/Drivers/Imagick/Encoders/JpegEncoder.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Imagick\Encoders; use Imagick; +use Intervention\Image\Config; use Intervention\Image\EncodedImage; use Intervention\Image\Encoders\JpegEncoder as GenericJpegEncoder; use Intervention\Image\Interfaces\ImageInterface; @@ -16,11 +17,14 @@ public function encode(ImageInterface $image): EncodedImage { $format = 'jpeg'; $compression = Imagick::COMPRESSION_JPEG; + $blendingColor = $this->driver()->handleInput( + $this->driver()->config()->option(Config::BLENDING_COLOR) + ); // resolve blending color because jpeg has no transparency $background = $this->driver() ->colorProcessor($image->colorspace()) - ->colorToNative($image->blendingColor()); + ->colorToNative($blendingColor); // set alpha value to 1 because Imagick renders // possible full transparent colors as black diff --git a/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php index f6586793..f1ce4dc6 100644 --- a/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php +++ b/src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Imagick; +use Intervention\Image\Config; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\BlendTransparencyModifier as GenericBlendTransparencyModifier; @@ -15,7 +16,7 @@ public function apply(ImageInterface $image): ImageInterface { // decode blending color $color = $this->driver()->handleInput( - $this->color ? $this->color : $image->blendingColor() + $this->color ? $this->color : $this->driver()->config()->option(Config::BLENDING_COLOR) ); // get imagickpixel from color diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 700a6e2e..2bc8bd35 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -18,7 +18,7 @@ public function testConstructor(): void $this->assertTrue($config->option('autoOrientation')); $this->assertTrue($config->option('decodeAnimation')); - $this->assertEquals('ffffff00', $config->option('blendingColor')); + $this->assertEquals('ffffff', $config->option('blendingColor')); $config = new Config( autoOrientation: false, @@ -37,7 +37,7 @@ public function testGetSetOptions(): void $config = new Config(); $this->assertTrue($config->option('autoOrientation')); $this->assertTrue($config->option('decodeAnimation')); - $this->assertEquals('ffffff00', $config->option('blendingColor')); + $this->assertEquals('ffffff', $config->option('blendingColor')); $result = $config->setOptions( autoOrientation: false, diff --git a/tests/Unit/Drivers/Gd/ImageTest.php b/tests/Unit/Drivers/Gd/ImageTest.php index 68530ac6..89c0f550 100644 --- a/tests/Unit/Drivers/Gd/ImageTest.php +++ b/tests/Unit/Drivers/Gd/ImageTest.php @@ -7,7 +7,6 @@ use Intervention\Image\Analyzers\WidthAnalyzer; use Intervention\Image\Collection; use Intervention\Image\Colors\Hsl\Colorspace; -use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Drivers\Gd\Core; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Drivers\Gd\Frame; @@ -280,17 +279,16 @@ public function testText(): void $this->assertInstanceOf(Image::class, $this->image->text('test', 0, 0, new Font())); } - public function testSetGetBlendingColor(): void + public function testBlendTransparencyDefault(): void { $image = $this->readTestImage('gradient.gif'); - $this->assertInstanceOf(ColorInterface::class, $image->blendingColor()); - $this->assertColor(255, 255, 255, 0, $image->blendingColor()); - $result = $image->setBlendingColor(new Color(1, 2, 3, 4)); - $this->assertColor(1, 2, 3, 4, $result->blendingColor()); - $this->assertColor(1, 2, 3, 4, $image->blendingColor()); + $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); + $result = $image->blendTransparency(); + $this->assertColor(255, 255, 255, 255, $image->pickColor(1, 0)); + $this->assertColor(255, 255, 255, 255, $result->pickColor(1, 0)); } - public function testBlendTransparency(): void + public function testBlendTransparencyArgument(): void { $image = $this->readTestImage('gradient.gif'); $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); diff --git a/tests/Unit/Drivers/Imagick/ImageTest.php b/tests/Unit/Drivers/Imagick/ImageTest.php index 777fbc3e..9d05227f 100644 --- a/tests/Unit/Drivers/Imagick/ImageTest.php +++ b/tests/Unit/Drivers/Imagick/ImageTest.php @@ -8,7 +8,6 @@ use Intervention\Image\Analyzers\WidthAnalyzer; use Intervention\Image\Collection; use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace; -use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Drivers\Imagick\Core; use Intervention\Image\Drivers\Imagick\Driver; @@ -263,17 +262,16 @@ public function testSharpen(): void $this->assertInstanceOf(Image::class, $this->image->sharpen(12)); } - public function testSetGetBlendingColor(): void + public function testBlendTransparencyDefault(): void { $image = $this->readTestImage('gradient.gif'); - $this->assertInstanceOf(ColorInterface::class, $image->blendingColor()); - $this->assertColor(255, 255, 255, 0, $image->blendingColor()); - $result = $image->setBlendingColor(new Color(1, 2, 3, 4)); - $this->assertColor(1, 2, 3, 4, $result->blendingColor()); - $this->assertColor(1, 2, 3, 4, $image->blendingColor()); + $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); + $result = $image->blendTransparency(); + $this->assertColor(255, 255, 255, 255, $image->pickColor(1, 0)); + $this->assertColor(255, 255, 255, 255, $result->pickColor(1, 0)); } - public function testBlendTransparency(): void + public function testBlendTransparencyArgument(): void { $image = $this->readTestImage('gradient.gif'); $this->assertColor(0, 0, 0, 0, $image->pickColor(1, 0)); diff --git a/tests/Unit/ImageManagerTestGd.php b/tests/Unit/ImageManagerTestGd.php index 84007c0e..78352096 100644 --- a/tests/Unit/ImageManagerTestGd.php +++ b/tests/Unit/ImageManagerTestGd.php @@ -48,14 +48,14 @@ public function testDriverStatic(): void $this->assertInstanceOf(ImageManager::class, $manager); } - public function testCreateGd(): void + public function testCreate(): void { $manager = new ImageManager(Driver::class); $image = $manager->create(5, 4); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testAnimateGd(): void + public function testAnimate(): void { $manager = new ImageManager(Driver::class); $image = $manager->animate(function ($animation) { @@ -64,42 +64,42 @@ public function testAnimateGd(): void $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGd(): void + public function testRead(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif')); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithDecoderClassname(): void + public function testReadWithDecoderClassname(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), FilePathImageDecoder::class); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithDecoderInstance(): void + public function testReadWithDecoderInstance(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), new FilePathImageDecoder()); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithDecoderClassnameArray(): void + public function testReadWithDecoderClassnameArray(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [FilePathImageDecoder::class]); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithDecoderInstanceArray(): void + public function testReadWithDecoderInstanceArray(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [new FilePathImageDecoder()]); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithDecoderInstanceArrayMultiple(): void + public function testReadWithDecoderInstanceArrayMultiple(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [ @@ -109,17 +109,49 @@ public function testReadGdWithDecoderInstanceArrayMultiple(): void $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadGdWithRotationAdjustment(): void + public function testReadWithRotationAdjustment(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('orientation.jpg')); $this->assertColor(1, 0, 254, 255, $image->pickColor(3, 3)); } - public function testReadImagickWithoutRotationAdjustment(): void + public function testReadWithoutRotationAdjustment(): void { $manager = new ImageManager(Driver::class, autoOrientation: false); $image = $manager->read($this->getTestResourcePath('orientation.jpg')); $this->assertColor(250, 2, 3, 255, $image->pickColor(3, 3)); } + + public function testReadAnimation(): void + { + $manager = new ImageManager(Driver::class); + $image = $manager->read($this->getTestResourcePath('animation.gif')); + $this->assertTrue($image->isAnimated()); + } + + public function testReadAnimationDiscarded(): void + { + $manager = new ImageManager(Driver::class, decodeAnimation: false); + $image = $manager->read($this->getTestResourcePath('animation.gif')); + $this->assertFalse($image->isAnimated()); + } + + public function testApplyBlendingColorDefault(): void + { + $manager = new ImageManager(Driver::class); + $image = $manager->read($this->getTestResourcePath('blocks.png')); + $result = $image->blendTransparency(); + $this->assertColor(255, 255, 255, 255, $image->pickColor(530, 0)); + $this->assertColor(255, 255, 255, 255, $result->pickColor(530, 0)); + } + + public function testApplyBlendingColorConfigured(): void + { + $manager = new ImageManager(Driver::class, blendingColor: 'ff5500'); + $image = $manager->read($this->getTestResourcePath('blocks.png')); + $result = $image->blendTransparency(); + $this->assertColor(255, 85, 0, 255, $image->pickColor(530, 0)); + $this->assertColor(255, 85, 0, 255, $result->pickColor(530, 0)); + } } diff --git a/tests/Unit/ImageManagerTestImagick.php b/tests/Unit/ImageManagerTestImagick.php index 60773397..422229b1 100644 --- a/tests/Unit/ImageManagerTestImagick.php +++ b/tests/Unit/ImageManagerTestImagick.php @@ -48,14 +48,14 @@ public function testDriverStatic(): void $this->assertInstanceOf(ImageManager::class, $manager); } - public function testCreateImagick(): void + public function testCreate(): void { $manager = new ImageManager(Driver::class); $image = $manager->create(5, 4); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testAnimateImagick(): void + public function testAnimate(): void { $manager = new ImageManager(Driver::class); $image = $manager->animate(function ($animation) { @@ -64,42 +64,42 @@ public function testAnimateImagick(): void $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagick(): void + public function testRead(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif')); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithDecoderClassname(): void + public function testReadWithDecoderClassname(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), FilePathImageDecoder::class); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithDecoderInstance(): void + public function testReadWithDecoderInstance(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), new FilePathImageDecoder()); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithDecoderClassnameArray(): void + public function testReadWithDecoderClassnameArray(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [FilePathImageDecoder::class]); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithDecoderInstanceArray(): void + public function testReadWithDecoderInstanceArray(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [new FilePathImageDecoder()]); $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithDecoderInstanceArrayMultiple(): void + public function testReadWithDecoderInstanceArrayMultiple(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('red.gif'), [ @@ -109,17 +109,49 @@ public function testReadImagickWithDecoderInstanceArrayMultiple(): void $this->assertInstanceOf(ImageInterface::class, $image); } - public function testReadImagickWithRotationAdjustment(): void + public function testReadWithRotationAdjustment(): void { $manager = new ImageManager(Driver::class); $image = $manager->read($this->getTestResourcePath('orientation.jpg')); $this->assertColor(1, 0, 254, 255, $image->pickColor(3, 3)); } - public function testReadImagickWithoutRotationAdjustment(): void + public function testReadWithoutRotationAdjustment(): void { $manager = new ImageManager(Driver::class, autoOrientation: false); $image = $manager->read($this->getTestResourcePath('orientation.jpg')); $this->assertColor(250, 2, 3, 255, $image->pickColor(3, 3)); } + + public function testReadAnimation(): void + { + $manager = new ImageManager(Driver::class); + $image = $manager->read($this->getTestResourcePath('animation.gif')); + $this->assertTrue($image->isAnimated()); + } + + public function testReadAnimationDiscarded(): void + { + $manager = new ImageManager(Driver::class, decodeAnimation: false); + $image = $manager->read($this->getTestResourcePath('animation.gif')); + $this->assertFalse($image->isAnimated()); + } + + public function testApplyBlendingColor(): void + { + $manager = new ImageManager(Driver::class); + $image = $manager->read($this->getTestResourcePath('blocks.png')); + $result = $image->blendTransparency(); + $this->assertColor(255, 255, 255, 255, $image->pickColor(530, 0)); + $this->assertColor(255, 255, 255, 255, $result->pickColor(530, 0)); + } + + public function testApplyBlendingColorConfigured(): void + { + $manager = new ImageManager(Driver::class, blendingColor: 'ff5500'); + $image = $manager->read($this->getTestResourcePath('blocks.png')); + $result = $image->blendTransparency(); + $this->assertColor(255, 85, 0, 255, $image->pickColor(530, 0)); + $this->assertColor(255, 85, 0, 255, $result->pickColor(530, 0)); + } }