From 1ac3bcee45642882124050f36d77588e05bb9322 Mon Sep 17 00:00:00 2001 From: Martin Carl Kopp <6154099+MacJoom@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:22:30 +0100 Subject: [PATCH] [5.2] Check imagecolortransparent for validity (#44418) --- libraries/src/Image/Image.php | 60 ++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/libraries/src/Image/Image.php b/libraries/src/Image/Image.php index 804be378ea8d7..5b5b249a4c0c1 100644 --- a/libraries/src/Image/Image.php +++ b/libraries/src/Image/Image.php @@ -435,12 +435,30 @@ public function crop($width, $height, $left = null, $top = null, $createNew = tr if ($this->isTransparent()) { // Get the transparent color values for the current image. - $rgba = imagecolorsforindex($this->getHandle(), imagecolortransparent($this->getHandle())); - $color = imagecolorallocatealpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); - - // Set the transparent color values for the new image. - imagecolortransparent($handle, $color); - imagefill($handle, 0, 0, $color); + $ict = imagecolortransparent($this->getHandle()); + $ctot = imagecolorstotal($this->getHandle()); + // Sanitize imagecolortransparent & imagecolorstotal + if ($ctot === 255 && $ict === 255) { + $ict = 254; + } + if ($ctot === 0 && $ict === 0) { + $ctot = 1; + } + if ($ict >= 0 && $ict < $ctot) { + $rgba = imagecolorsforindex($this->getHandle(), $ict); + if (!empty($rgba)) { + $color = imagecolorallocatealpha( + $handle, + $rgba['red'], + $rgba['green'], + $rgba['blue'], + $rgba['alpha'] + ); + // Set the transparent color values for the new image. + imagecolortransparent($handle, $color); + imagefill($handle, 0, 0, $color); + } + } } if (!$this->generateBestQuality) { @@ -714,12 +732,30 @@ public function resize($width, $height, $createNew = true, $scaleMethod = self:: if ($this->isTransparent()) { // Get the transparent color values for the current image. - $rgba = imagecolorsforindex($this->getHandle(), imagecolortransparent($this->getHandle())); - $color = imagecolorallocatealpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); - - // Set the transparent color values for the new image. - imagecolortransparent($handle, $color); - imagefill($handle, 0, 0, $color); + $ict = imagecolortransparent($this->getHandle()); + $ctot = imagecolorstotal($this->getHandle()); + // Sanitize imagecolortransparent & imagecolorstotal + if ($ctot === 255 && $ict === 255) { + $ict = 254; + } + if ($ctot === 0 && $ict === 0) { + $ctot = 1; + } + if ($ict >= 0 && $ict < $ctot) { + $rgba = imagecolorsforindex($this->getHandle(), $ict); + if (!empty($rgba)) { + $color = imagecolorallocatealpha( + $handle, + $rgba['red'], + $rgba['green'], + $rgba['blue'], + $rgba['alpha'] + ); + // Set the transparent color values for the new image. + imagecolortransparent($handle, $color); + imagefill($handle, 0, 0, $color); + } + } } if (!$this->generateBestQuality) {