Skip to content

Commit

Permalink
Add tests, improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 10, 2024
1 parent 3ad4261 commit d9a7ea5
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Config.php
Expand Up @@ -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',
) {
}

Expand Down
10 changes: 9 additions & 1 deletion src/Drivers/Gd/Encoders/JpegEncoder.php
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Gd/Modifiers/BlendTransparencyModifier.php
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/Drivers/Gd/Modifiers/ContainModifier.php
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/Drivers/Imagick/Encoders/JpegEncoder.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Imagick/Modifiers/BlendTransparencyModifier.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/ConfigTest.php
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions tests/Unit/Drivers/Gd/ImageTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
14 changes: 6 additions & 8 deletions tests/Unit/Drivers/Imagick/ImageTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
52 changes: 42 additions & 10 deletions tests/Unit/ImageManagerTestGd.php
Expand Up @@ -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) {
Expand All @@ -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'), [
Expand All @@ -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));
}
}

0 comments on commit d9a7ea5

Please sign in to comment.