Skip to content

Commit d98604f

Browse files
author
Oleksandr Iegorov
committed
MC-30296: Product image is not visible if a watermark image size is larger
1 parent 5206031 commit d98604f

File tree

1 file changed

+56
-0
lines changed
  • lib/internal/Magento/Framework/Image/Adapter

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
442442
$this->_getCallback('create', $watermarkFileType, 'Unsupported watermark image format.'),
443443
$imagePath
444444
);
445+
//$watermark = $this->prepareWatermark($watermark, $watermarkFileType);
445446

446447
$merged = false;
447448

@@ -599,6 +600,49 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
599600
$this->refreshImageDimensions();
600601
}
601602

603+
/**
604+
* @param resource $watermark
605+
* @param string $watermarkFileType
606+
* @return resource
607+
*/
608+
private function prepareWatermark($watermark, $watermarkFileType)
609+
{
610+
if (imagesx($watermark) > $this->_imageSrcWidth
611+
|| imagesy($watermark) > $this->_imageSrcHeight) {
612+
$widthRatio = imagesx($watermark)/$this->_imageSrcWidth;
613+
$heightRatio = imagesy($watermark)/$this->_imageSrcHeight;
614+
$newWidth = ($widthRatio > $heightRatio)
615+
? round(imagesx($watermark)/$widthRatio) : round(imagesx($watermark)/$heightRatio);
616+
$newHeight = ($widthRatio > $heightRatio)
617+
? round(imagesy($watermark)/$widthRatio) : round(imagesy($watermark)/$heightRatio);
618+
$isAlpha = false;
619+
$isTrueColor = false;
620+
$this->_getTransparency($watermark, $watermarkFileType, $isAlpha, $isTrueColor);
621+
if ($isTrueColor) {
622+
$newImage = imagecreatetruecolor($newWidth, $newHeight);
623+
} else {
624+
$newImage = imagecreate($newWidth, $newHeight);
625+
}
626+
if ($isAlpha) {
627+
$this->_saveAlpha($newImage);
628+
}
629+
imagecopyresampled(
630+
$newImage,
631+
$watermark,
632+
0,
633+
0,
634+
0,
635+
0,
636+
$newWidth,
637+
$newHeight,
638+
imagesx($watermark),
639+
imagesy($watermark)
640+
);
641+
$watermark = $newImage;
642+
}
643+
return $watermark;
644+
}
645+
602646
/**
603647
* Crop image
604648
*
@@ -859,6 +903,10 @@ private function imagecopymergeWithAlphaFix(
859903
return false;
860904
}
861905

906+
if (false === imagesavealpha($tmpImg, true)) {
907+
return false;
908+
}
909+
862910
if (false === imagecopy($tmpImg, $src_im, 0, 0, 0, 0, $sizeX, $sizeY)) {
863911
return false;
864912
}
@@ -868,6 +916,14 @@ private function imagecopymergeWithAlphaFix(
868916
return false;
869917
}
870918

919+
if (false === imagealphablending($dst_im, true)) {
920+
return false;
921+
}
922+
923+
if (false === imagesavealpha($dst_im, true)) {
924+
return false;
925+
}
926+
871927
$result = imagecopy($dst_im, $tmpImg, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
872928
imagedestroy($tmpImg);
873929

0 commit comments

Comments
 (0)