Skip to content

Commit 519f3e1

Browse files
committed
Handle non true color images with GD2
1 parent f2446b6 commit 519f3e1

File tree

1 file changed

+47
-40
lines changed
  • lib/internal/Magento/Framework/Image/Adapter

1 file changed

+47
-40
lines changed

lib/internal/Magento/Framework/Image/Adapter/Gd2.php

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
465465
} elseif ($this->getWatermarkPosition() == self::POSITION_CENTER) {
466466
$positionX = $this->_imageSrcWidth / 2 - imagesx($watermark) / 2;
467467
$positionY = $this->_imageSrcHeight / 2 - imagesy($watermark) / 2;
468-
$this->imagecopymergeWithAlphaFix(
468+
$this->copyImageWithAlphaPercentage(
469469
$this->_imageHandler,
470470
$watermark,
471471
$positionX,
@@ -478,7 +478,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
478478
);
479479
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_RIGHT) {
480480
$positionX = $this->_imageSrcWidth - imagesx($watermark);
481-
$this->imagecopymergeWithAlphaFix(
481+
$this->copyImageWithAlphaPercentage(
482482
$this->_imageHandler,
483483
$watermark,
484484
$positionX,
@@ -490,7 +490,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
490490
$this->getWatermarkImageOpacity()
491491
);
492492
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_LEFT) {
493-
$this->imagecopymergeWithAlphaFix(
493+
$this->copyImageWithAlphaPercentage(
494494
$this->_imageHandler,
495495
$watermark,
496496
$positionX,
@@ -504,7 +504,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
504504
} elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT) {
505505
$positionX = $this->_imageSrcWidth - imagesx($watermark);
506506
$positionY = $this->_imageSrcHeight - imagesy($watermark);
507-
$this->imagecopymergeWithAlphaFix(
507+
$this->copyImageWithAlphaPercentage(
508508
$this->_imageHandler,
509509
$watermark,
510510
$positionX,
@@ -517,7 +517,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
517517
);
518518
} elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT) {
519519
$positionY = $this->_imageSrcHeight - imagesy($watermark);
520-
$this->imagecopymergeWithAlphaFix(
520+
$this->copyImageWithAlphaPercentage(
521521
$this->_imageHandler,
522522
$watermark,
523523
$positionX,
@@ -531,7 +531,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
531531
}
532532

533533
if ($tile === false && $merged === false) {
534-
$this->imagecopymergeWithAlphaFix(
534+
$this->copyImageWithAlphaPercentage(
535535
$this->_imageHandler,
536536
$watermark,
537537
$positionX,
@@ -547,7 +547,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
547547
$offsetY = $positionY;
548548
while ($offsetY <= $this->_imageSrcHeight + imagesy($watermark)) {
549549
while ($offsetX <= $this->_imageSrcWidth + imagesx($watermark)) {
550-
$this->imagecopymergeWithAlphaFix(
550+
$this->copyImageWithAlphaPercentage(
551551
$this->_imageHandler,
552552
$watermark,
553553
$offsetX,
@@ -780,64 +780,71 @@ protected function _createEmptyImage($width, $height)
780780
}
781781

782782
/**
783-
* Fix an issue with the usage of imagecopymerge where the alpha channel is lost
783+
* Copy source image onto destination image with given alpha percentage
784784
*
785-
* @param resource $dst_im
786-
* @param resource $src_im
787-
* @param int $dst_x
788-
* @param int $dst_y
789-
* @param int $src_x
790-
* @param int $src_y
791-
* @param int $src_w
792-
* @param int $src_h
793-
* @param int $pct
785+
* @internal The arguments and functionality is the same as imagecopymerge
786+
* but with proper handling of alpha transparency
787+
*
788+
* @param resource $destinationImage
789+
* @param resource $sourceImage
790+
* @param int $destinationX
791+
* @param int $destinationY
792+
* @param int $sourceX
793+
* @param int $sourceY
794+
* @param int $sourceWidth
795+
* @param int $sourceHeight
796+
* @param int $alphaPercentage
794797
*
795798
* @return bool
796799
*/
797-
private function imagecopymergeWithAlphaFix(
798-
$dst_im,
799-
$src_im,
800-
$dst_x,
801-
$dst_y,
802-
$src_x,
803-
$src_y,
804-
$src_w,
805-
$src_h,
806-
$pct
800+
private function copyImageWithAlphaPercentage(
801+
$destinationImage,
802+
$sourceImage,
803+
$destinationX,
804+
$destinationY,
805+
$sourceX,
806+
$sourceY,
807+
$sourceWidth,
808+
$sourceHeight,
809+
$alphaPercentage
807810
) {
808-
if ($pct >= 100) {
809-
return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
811+
if (imageistruecolor($destinationImage) === false || imageistruecolor($sourceImage) === false) {
812+
return imagecopymerge($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight, $alphaPercentage);
813+
}
814+
815+
if ($alphaPercentage >= 100) {
816+
return imagecopy($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
810817
}
811818

812-
if ($pct < 0) {
819+
if ($alphaPercentage < 0) {
813820
return false;
814821
}
815822

816-
$sizeX = imagesx($src_im);
817-
$sizeY = imagesy($src_im);
818-
if (false === $sizeX || false === $sizeY) {
823+
$sizeX = imagesx($sourceImage);
824+
$sizeY = imagesy($sourceImage);
825+
if ($sizeX === false || $sizeY === false || $sizeX === 0 || $sizeY === 0) {
819826
return false;
820827
}
821828

822-
$tmpImg = imagecreatetruecolor($src_w, $src_h);
823-
if (false === $tmpImg) {
829+
$tmpImg = imagecreatetruecolor($sourceWidth, $sourceHeight);
830+
if ($tmpImg === false) {
824831
return false;
825832
}
826833

827-
if (false === imagealphablending($tmpImg, false)) {
834+
if (imagealphablending($tmpImg, false) === false) {
828835
return false;
829836
}
830837

831-
if (false === imagecopy($tmpImg, $src_im, 0, 0, 0, 0, $sizeX, $sizeY)) {
838+
if (imagecopy($tmpImg, $sourceImage, 0, 0, 0, 0, $sizeX, $sizeY) === false) {
832839
return false;
833840
}
834841

835-
$transparancy = 127 - (($pct*127)/100);
836-
if (false === imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparancy)) {
842+
$transparency = 127 - (($alphaPercentage*127)/100);
843+
if (imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparency) === false) {
837844
return false;
838845
}
839846

840-
$result = imagecopy($dst_im, $tmpImg, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
847+
$result = imagecopy($destinationImage, $tmpImg, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
841848
imagedestroy($tmpImg);
842849

843850
return $result;

0 commit comments

Comments
 (0)