1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-21 13:11:18 +02:00

Add background parameter to FactoryInterface::newCore()

This commit is contained in:
Oliver Vogel
2023-11-05 10:02:10 +01:00
parent b29f0d3329
commit 27c47ae502
3 changed files with 20 additions and 7 deletions

View File

@@ -5,6 +5,8 @@ namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Collection; use Intervention\Image\Collection;
use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Drivers\Gd\Image; use Intervention\Image\Drivers\Gd\Image;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FactoryInterface; use Intervention\Image\Interfaces\FactoryInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanHandleInput; use Intervention\Image\Traits\CanHandleInput;
@@ -12,6 +14,7 @@ use Intervention\Image\Traits\CanHandleInput;
class Factory implements FactoryInterface class Factory implements FactoryInterface
{ {
use CanHandleInput; use CanHandleInput;
use CanHandleColors;
/** /**
* {@inheritdoc} * {@inheritdoc}
@@ -60,10 +63,10 @@ class Factory implements FactoryInterface
return new Image($frames); return new Image($frames);
} }
public function newCore(int $width, int $height) public function newCore(int $width, int $height, ?ColorInterface $background = null)
{ {
$core = imagecreatetruecolor($width, $height); $core = imagecreatetruecolor($width, $height);
$color = imagecolorallocatealpha($core, 0, 0, 0, 127); $color = $background ? $this->colorToInteger($background) : imagecolorallocatealpha($core, 0, 0, 0, 127);
imagefill($core, 0, 0, $color); imagefill($core, 0, 0, $color);
imagesavealpha($core, true); imagesavealpha($core, true);

View File

@@ -4,7 +4,10 @@ namespace Intervention\Image\Drivers\Imagick;
use Imagick; use Imagick;
use ImagickPixel; use ImagickPixel;
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Drivers\Imagick\Image; use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FactoryInterface; use Intervention\Image\Interfaces\FactoryInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Traits\CanCheckType;
@@ -14,6 +17,7 @@ class Factory implements FactoryInterface
{ {
use CanHandleInput; use CanHandleInput;
use CanCheckType; use CanCheckType;
use CanHandleColors;
/** /**
* {@inheritdoc} * {@inheritdoc}
@@ -66,10 +70,15 @@ class Factory implements FactoryInterface
* *
* @see FactoryInterface::newCore() * @see FactoryInterface::newCore()
*/ */
public function newCore(int $width, int $height) public function newCore(int $width, int $height, ?ColorInterface $background = null)
{ {
$pixel = $background ? $this->colorToPixel(
$background,
new Colorspace()
) : new ImagickPixel('rgba(0, 0, 0, 0)');
$imagick = new Imagick(); $imagick = new Imagick();
$imagick->newImage($width, $height, new ImagickPixel('rgba(0, 0, 0, 0)'), 'png'); $imagick->newImage($width, $height, $pixel, 'png');
$imagick->setType(Imagick::IMGTYPE_UNDEFINED); $imagick->setType(Imagick::IMGTYPE_UNDEFINED);
$imagick->setImageType(Imagick::IMGTYPE_UNDEFINED); $imagick->setImageType(Imagick::IMGTYPE_UNDEFINED);
$imagick->setColorspace(Imagick::COLORSPACE_RGB); $imagick->setColorspace(Imagick::COLORSPACE_RGB);

View File

@@ -26,6 +26,7 @@ interface FactoryInterface
* *
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param null|ColorInterface $background
*/ */
public function newCore(int $width, int $height); public function newCore(int $width, int $height, ?ColorInterface $background = null);
} }