From b9a3f8c4b2f72527c37bd6e4c1fff7c9b8bcf3b7 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 30 Oct 2023 10:58:28 +0100 Subject: [PATCH] Update color picking method of GD driver --- src/Drivers/Gd/Image.php | 8 +++++--- src/Drivers/Gd/Traits/CanHandleColors.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Gd/Image.php b/src/Drivers/Gd/Image.php index 2607dc89..55fe560c 100644 --- a/src/Drivers/Gd/Image.php +++ b/src/Drivers/Gd/Image.php @@ -92,9 +92,11 @@ class Image extends AbstractImage implements ImageInterface, IteratorAggregate public function pickColor(int $x, int $y, int $frame_key = 0): ColorInterface { - return $this->integerToColor( - imagecolorat($this->frame($frame_key)->core(), $x, $y) - ); + $gd = $this->frame($frame_key)->core(); + $index = imagecolorat($gd, $x, $y); + $colors = imagecolorsforindex($gd, $index); + + return $this->arrayToColor($colors); } /** diff --git a/src/Drivers/Gd/Traits/CanHandleColors.php b/src/Drivers/Gd/Traits/CanHandleColors.php index 1ed34b65..3853e558 100644 --- a/src/Drivers/Gd/Traits/CanHandleColors.php +++ b/src/Drivers/Gd/Traits/CanHandleColors.php @@ -6,6 +6,23 @@ use Intervention\Image\Colors\Rgb\Color; trait CanHandleColors { + /** + * Transforms array result from imagecolorsforindex() to Color object + * + * @param array $values + * @return Color + */ + protected function arrayToColor(array $values): Color + { + list($r, $g, $b, $a) = array_values($values); + + // convert gd apha integer to intervention alpha integer + // ([opaque]0-127[transparent]) to ([opaque]255-0[transparent]) + $a = (int) static::convertRange($a, 127, 0, 0, 255); + + return new Color($r, $g, $b, $a); + } + /** * Transforms GD Library integer color value to RGB color object *