mirror of
https://github.com/Intervention/image.git
synced 2025-08-13 01:14:04 +02:00
Added tests
This commit is contained in:
@@ -4,6 +4,12 @@ namespace Intervention\Image\Drivers\Abstract;
|
|||||||
|
|
||||||
abstract class AbstractColor
|
abstract class AbstractColor
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Format color to hexadecimal color code
|
||||||
|
*
|
||||||
|
* @param string $prefix
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function toHex(string $prefix = ''): string
|
public function toHex(string $prefix = ''): string
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
@@ -14,4 +20,14 @@ abstract class AbstractColor
|
|||||||
$this->blue()
|
$this->blue()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if color is greyscale
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isGreyscale(): bool
|
||||||
|
{
|
||||||
|
return ($this->red() === $this->green()) && ($this->green() === $this->blue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,7 +33,9 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
|||||||
$this->fail();
|
$this->fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
$gd = $this->gdImageToTruecolor($gd);
|
if (! imageistruecolor($gd)) {
|
||||||
|
imagepalettetotruecolor($gd);
|
||||||
|
}
|
||||||
|
|
||||||
return new Image(new Collection([new Frame($gd)]));
|
return new Image(new Collection([new Frame($gd)]));
|
||||||
}
|
}
|
||||||
@@ -58,32 +60,4 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
|||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Transform GD image into truecolor version
|
|
||||||
*
|
|
||||||
* @param GdImage $gd
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function gdImageToTruecolor(GdImage $gd): GdImage
|
|
||||||
{
|
|
||||||
$width = imagesx($gd);
|
|
||||||
$height = imagesy($gd);
|
|
||||||
|
|
||||||
// new canvas
|
|
||||||
$canvas = imagecreatetruecolor($width, $height);
|
|
||||||
|
|
||||||
// fill with transparent color
|
|
||||||
imagealphablending($canvas, false);
|
|
||||||
$transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
|
|
||||||
imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent);
|
|
||||||
imagecolortransparent($canvas, $transparent);
|
|
||||||
imagealphablending($canvas, true);
|
|
||||||
|
|
||||||
// copy original
|
|
||||||
imagecopy($canvas, $gd, 0, 0, 0, 0, $width, $height);
|
|
||||||
imagedestroy($gd);
|
|
||||||
|
|
||||||
return $canvas;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
21
tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php
Normal file
21
tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers;
|
||||||
|
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
use Intervention\Image\Drivers\Gd\Image;
|
||||||
|
use Intervention\Image\Drivers\Gd\Modifiers\GreyscaleModifier;
|
||||||
|
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
|
||||||
|
|
||||||
|
class GreyscaleModifierTest extends TestCase
|
||||||
|
{
|
||||||
|
use CanCreateGdTestImage;
|
||||||
|
|
||||||
|
public function testColorChange(): void
|
||||||
|
{
|
||||||
|
$image = $this->createTestImage('trim.png');
|
||||||
|
$this->assertFalse($image->pickColor(0, 0)->isGreyscale());
|
||||||
|
$image->modify(new GreyscaleModifier());
|
||||||
|
$this->assertTrue($image->pickColor(0, 0)->isGreyscale());
|
||||||
|
}
|
||||||
|
}
|
24
tests/Drivers/Gd/Modifiers/ResizeModifierTest.php
Normal file
24
tests/Drivers/Gd/Modifiers/ResizeModifierTest.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers;
|
||||||
|
|
||||||
|
use Intervention\Image\Drivers\Gd\Image;
|
||||||
|
use Intervention\Image\Drivers\Gd\Modifiers\ResizeModifier;
|
||||||
|
use Intervention\Image\Geometry\Size;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
|
||||||
|
|
||||||
|
class ResizeModifierTest extends TestCase
|
||||||
|
{
|
||||||
|
use CanCreateGdTestImage;
|
||||||
|
|
||||||
|
public function testColorChange(): void
|
||||||
|
{
|
||||||
|
$image = $this->createTestImage('trim.png');
|
||||||
|
$this->assertEquals(50, $image->width());
|
||||||
|
$this->assertEquals(50, $image->height());
|
||||||
|
$image->modify(new ResizeModifier(new Size(30, 20)));
|
||||||
|
$this->assertEquals(30, $image->width());
|
||||||
|
$this->assertEquals(20, $image->height());
|
||||||
|
}
|
||||||
|
}
|
21
tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php
Normal file
21
tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers;
|
||||||
|
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Image;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Modifiers\GreyscaleModifier;
|
||||||
|
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
class GreyscaleModifierTest extends TestCase
|
||||||
|
{
|
||||||
|
use CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
public function testColorChange(): void
|
||||||
|
{
|
||||||
|
$image = $this->createTestImage('trim.png');
|
||||||
|
$this->assertFalse($image->pickColor(0, 0)->isGreyscale());
|
||||||
|
$image->modify(new GreyscaleModifier());
|
||||||
|
$this->assertTrue($image->pickColor(0, 0)->isGreyscale());
|
||||||
|
}
|
||||||
|
}
|
24
tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php
Normal file
24
tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers;
|
||||||
|
|
||||||
|
use Intervention\Image\Drivers\Imagick\Image;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Modifiers\ResizeModifier;
|
||||||
|
use Intervention\Image\Geometry\Size;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
class ResizeModifierTest extends TestCase
|
||||||
|
{
|
||||||
|
use CanCreateImagickTestImage;
|
||||||
|
|
||||||
|
public function testColorChange(): void
|
||||||
|
{
|
||||||
|
$image = $this->createTestImage('trim.png');
|
||||||
|
$this->assertEquals(50, $image->width());
|
||||||
|
$this->assertEquals(50, $image->height());
|
||||||
|
$image->modify(new ResizeModifier(new Size(30, 20)));
|
||||||
|
$this->assertEquals(30, $image->width());
|
||||||
|
$this->assertEquals(20, $image->height());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user