Skip to content

Commit

Permalink
[5.2] Check imagecolortransparent for validity (#44418)
Browse files Browse the repository at this point in the history
  • Loading branch information
MacJoom authored Nov 14, 2024
1 parent 4fc3029 commit 1ac3bce
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions libraries/src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1ac3bce

Please sign in to comment.