diff --git a/src/Drivers/Gd/Factory.php b/src/Drivers/Gd/Factory.php index c2aefa9a..50b8e4df 100644 --- a/src/Drivers/Gd/Factory.php +++ b/src/Drivers/Gd/Factory.php @@ -3,7 +3,6 @@ namespace Intervention\Image\Drivers\Gd; use Intervention\Image\Collection; -use Intervention\Image\Colors\Rgb\Channels\Alpha; use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\Drivers\Gd\Image; use Intervention\Image\Drivers\Gd\Traits\CanHandleColors; @@ -11,9 +10,6 @@ use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\FactoryInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Traits\CanHandleInput; -use Intervention\Image\Colors\Rgb\Channels\Blue; -use Intervention\Image\Colors\Rgb\Channels\Green; -use Intervention\Image\Colors\Rgb\Channels\Red; class Factory implements FactoryInterface { @@ -73,19 +69,7 @@ class Factory implements FactoryInterface $color = match (is_null($background)) { true => imagecolorallocatealpha($core, 255, 0, 255, 127), - default => imagecolorallocatealpha( - $core, - $background->channel(Red::class)->value(), - $background->channel(Green::class)->value(), - $background->channel(Blue::class)->value(), - $this->convertRange( - $background->channel(Alpha::class)->value(), - 0, - 255, - 127, - 0 - ) - ), + default => $this->allocateColor($core, $background), }; imagefill($core, 0, 0, $color); diff --git a/src/Drivers/Gd/Traits/CanHandleColors.php b/src/Drivers/Gd/Traits/CanHandleColors.php index 3853e558..66ac4a2e 100644 --- a/src/Drivers/Gd/Traits/CanHandleColors.php +++ b/src/Drivers/Gd/Traits/CanHandleColors.php @@ -2,10 +2,34 @@ namespace Intervention\Image\Drivers\Gd\Traits; +use GdImage; use Intervention\Image\Colors\Rgb\Color; +use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Colors\Rgb\Channels\Alpha; +use Intervention\Image\Colors\Rgb\Channels\Blue; +use Intervention\Image\Colors\Rgb\Channels\Green; +use Intervention\Image\Colors\Rgb\Channels\Red; trait CanHandleColors { + /** + * Allocate given color in given gd image and return color value/index + * + * @param GdImage $gd + * @param ColorInterface $color + * @return int + */ + protected function allocateColor(GdImage $gd, ColorInterface $color): int + { + return imagecolorallocatealpha( + $gd, + $color->channel(Red::class)->value(), + $color->channel(Green::class)->value(), + $color->channel(Blue::class)->value(), + $this->convertRange($color->channel(Alpha::class)->value(), 0, 255, 127, 0) + ); + } + /** * Transforms array result from imagecolorsforindex() to Color object *