From 27c47ae5025cee38a9e247261d8c32951e9b3afb Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Nov 2023 10:02:10 +0100 Subject: [PATCH] Add background parameter to FactoryInterface::newCore() --- src/Drivers/Gd/Factory.php | 7 +++++-- src/Drivers/Imagick/Factory.php | 13 +++++++++++-- src/Interfaces/FactoryInterface.php | 7 ++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Drivers/Gd/Factory.php b/src/Drivers/Gd/Factory.php index 0df41869..5385547c 100644 --- a/src/Drivers/Gd/Factory.php +++ b/src/Drivers/Gd/Factory.php @@ -5,6 +5,8 @@ namespace Intervention\Image\Drivers\Gd; use Intervention\Image\Collection; use Intervention\Image\Drivers\Gd\Frame; 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\ImageInterface; use Intervention\Image\Traits\CanHandleInput; @@ -12,6 +14,7 @@ use Intervention\Image\Traits\CanHandleInput; class Factory implements FactoryInterface { use CanHandleInput; + use CanHandleColors; /** * {@inheritdoc} @@ -60,10 +63,10 @@ class Factory implements FactoryInterface 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); - $color = imagecolorallocatealpha($core, 0, 0, 0, 127); + $color = $background ? $this->colorToInteger($background) : imagecolorallocatealpha($core, 0, 0, 0, 127); imagefill($core, 0, 0, $color); imagesavealpha($core, true); diff --git a/src/Drivers/Imagick/Factory.php b/src/Drivers/Imagick/Factory.php index e4161bab..910d3ff4 100644 --- a/src/Drivers/Imagick/Factory.php +++ b/src/Drivers/Imagick/Factory.php @@ -4,7 +4,10 @@ namespace Intervention\Image\Drivers\Imagick; use Imagick; use ImagickPixel; +use Intervention\Image\Colors\Rgb\Colorspace; 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\ImageInterface; use Intervention\Image\Traits\CanCheckType; @@ -14,6 +17,7 @@ class Factory implements FactoryInterface { use CanHandleInput; use CanCheckType; + use CanHandleColors; /** * {@inheritdoc} @@ -66,10 +70,15 @@ class Factory implements FactoryInterface * * @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->newImage($width, $height, new ImagickPixel('rgba(0, 0, 0, 0)'), 'png'); + $imagick->newImage($width, $height, $pixel, 'png'); $imagick->setType(Imagick::IMGTYPE_UNDEFINED); $imagick->setImageType(Imagick::IMGTYPE_UNDEFINED); $imagick->setColorspace(Imagick::COLORSPACE_RGB); diff --git a/src/Interfaces/FactoryInterface.php b/src/Interfaces/FactoryInterface.php index 8a491bb7..2862dd18 100644 --- a/src/Interfaces/FactoryInterface.php +++ b/src/Interfaces/FactoryInterface.php @@ -24,8 +24,9 @@ interface FactoryInterface /** * Create new driver specific core image object * - * @param int $width - * @param int $height + * @param int $width + * @param int $height + * @param null|ColorInterface $background */ - public function newCore(int $width, int $height); + public function newCore(int $width, int $height, ?ColorInterface $background = null); }