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

TransparentColorDecoder

This commit is contained in:
Oliver Vogel
2021-11-28 09:13:50 +00:00
parent b649a6751e
commit 66794138e2
7 changed files with 98 additions and 10 deletions

View File

@@ -199,7 +199,7 @@ abstract class AbstractImage
);
}
public function pad(int $width, int $height, string $position = 'center', $backgroundColor = 'fff'): ImageInterface
public function pad(int $width, int $height, string $position = 'center', $backgroundColor = 'transparent'): ImageInterface
{
// original
$imagesize = $this->getSize();
@@ -212,7 +212,7 @@ abstract class AbstractImage
);
}
public function padDown(int $width, int $height, string $position = 'center', $backgroundColor = 'fff'): ImageInterface
public function padDown(int $width, int $height, string $position = 'center', $backgroundColor = 'transparent'): ImageInterface
{
// original
$imagesize = $this->getSize();

View File

@@ -0,0 +1,21 @@
<?php
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class TransparentColorDecoder extends ArrayColorDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input) || strtolower($input) !== 'transparent') {
$this->fail();
}
return parent::decode([0, 0, 0, 0]);
}
}

View File

@@ -11,10 +11,12 @@ class InputHandler extends AbstractInputHandler
{
return new Decoders\ArrayColorDecoder(
new Decoders\HexColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
new Decoders\TransparentColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
)
)
)
)

View File

@@ -0,0 +1,21 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class TransparentColorDecoder extends ArrayColorDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input) || strtolower($input) !== 'transparent') {
$this->fail();
}
return parent::decode([0, 0, 0, 0]);
}
}

View File

@@ -11,10 +11,12 @@ class InputHandler extends AbstractInputHandler
{
return new Decoders\ArrayColorDecoder(
new Decoders\HexColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
new Decoders\TransparentColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
)
)
)
)

View File

@@ -0,0 +1,21 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Drivers\Gd\Decoders\TransparentColorDecoder;
use Intervention\Image\Tests\TestCase;
class TransparentColorDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new TransparentColorDecoder();
$color = $decoder->decode('transparent');
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals(0, $color->red());
$this->assertEquals(0, $color->green());
$this->assertEquals(0, $color->blue());
$this->assertEquals(0, $color->alpha());
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Decoders\TransparentColorDecoder;
use Intervention\Image\Tests\TestCase;
class TransparentColorDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new TransparentColorDecoder();
$color = $decoder->decode('transparent');
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals(0, $color->red());
$this->assertEquals(0, $color->green());
$this->assertEquals(0, $color->blue());
$this->assertEquals(0, $color->alpha());
}
}