Skip to content

Commit 1ac3bce

Browse files
authored
[5.2] Check imagecolortransparent for validity (joomla#44418)
1 parent 4fc3029 commit 1ac3bce

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

libraries/src/Image/Image.php

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,30 @@ public function crop($width, $height, $left = null, $top = null, $createNew = tr
435435

436436
if ($this->isTransparent()) {
437437
// Get the transparent color values for the current image.
438-
$rgba = imagecolorsforindex($this->getHandle(), imagecolortransparent($this->getHandle()));
439-
$color = imagecolorallocatealpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
440-
441-
// Set the transparent color values for the new image.
442-
imagecolortransparent($handle, $color);
443-
imagefill($handle, 0, 0, $color);
438+
$ict = imagecolortransparent($this->getHandle());
439+
$ctot = imagecolorstotal($this->getHandle());
440+
// Sanitize imagecolortransparent & imagecolorstotal
441+
if ($ctot === 255 && $ict === 255) {
442+
$ict = 254;
443+
}
444+
if ($ctot === 0 && $ict === 0) {
445+
$ctot = 1;
446+
}
447+
if ($ict >= 0 && $ict < $ctot) {
448+
$rgba = imagecolorsforindex($this->getHandle(), $ict);
449+
if (!empty($rgba)) {
450+
$color = imagecolorallocatealpha(
451+
$handle,
452+
$rgba['red'],
453+
$rgba['green'],
454+
$rgba['blue'],
455+
$rgba['alpha']
456+
);
457+
// Set the transparent color values for the new image.
458+
imagecolortransparent($handle, $color);
459+
imagefill($handle, 0, 0, $color);
460+
}
461+
}
444462
}
445463

446464
if (!$this->generateBestQuality) {
@@ -714,12 +732,30 @@ public function resize($width, $height, $createNew = true, $scaleMethod = self::
714732

715733
if ($this->isTransparent()) {
716734
// Get the transparent color values for the current image.
717-
$rgba = imagecolorsforindex($this->getHandle(), imagecolortransparent($this->getHandle()));
718-
$color = imagecolorallocatealpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
719-
720-
// Set the transparent color values for the new image.
721-
imagecolortransparent($handle, $color);
722-
imagefill($handle, 0, 0, $color);
735+
$ict = imagecolortransparent($this->getHandle());
736+
$ctot = imagecolorstotal($this->getHandle());
737+
// Sanitize imagecolortransparent & imagecolorstotal
738+
if ($ctot === 255 && $ict === 255) {
739+
$ict = 254;
740+
}
741+
if ($ctot === 0 && $ict === 0) {
742+
$ctot = 1;
743+
}
744+
if ($ict >= 0 && $ict < $ctot) {
745+
$rgba = imagecolorsforindex($this->getHandle(), $ict);
746+
if (!empty($rgba)) {
747+
$color = imagecolorallocatealpha(
748+
$handle,
749+
$rgba['red'],
750+
$rgba['green'],
751+
$rgba['blue'],
752+
$rgba['alpha']
753+
);
754+
// Set the transparent color values for the new image.
755+
imagecolortransparent($handle, $color);
756+
imagefill($handle, 0, 0, $color);
757+
}
758+
}
723759
}
724760

725761
if (!$this->generateBestQuality) {

0 commit comments

Comments
 (0)