Skip to content

Commit

Permalink
Handle non true color images with GD2
Browse files Browse the repository at this point in the history
  • Loading branch information
elzekool committed Nov 19, 2017
1 parent f2446b6 commit 519f3e1
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions lib/internal/Magento/Framework/Image/Adapter/Gd2.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
} elseif ($this->getWatermarkPosition() == self::POSITION_CENTER) {
$positionX = $this->_imageSrcWidth / 2 - imagesx($watermark) / 2;
$positionY = $this->_imageSrcHeight / 2 - imagesy($watermark) / 2;
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -478,7 +478,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
);
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_RIGHT) {
$positionX = $this->_imageSrcWidth - imagesx($watermark);
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -490,7 +490,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
$this->getWatermarkImageOpacity()
);
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_LEFT) {
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -504,7 +504,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
} elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT) {
$positionX = $this->_imageSrcWidth - imagesx($watermark);
$positionY = $this->_imageSrcHeight - imagesy($watermark);
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -517,7 +517,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
);
} elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT) {
$positionY = $this->_imageSrcHeight - imagesy($watermark);
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -531,7 +531,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
}

if ($tile === false && $merged === false) {
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -547,7 +547,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
$offsetY = $positionY;
while ($offsetY <= $this->_imageSrcHeight + imagesy($watermark)) {
while ($offsetX <= $this->_imageSrcWidth + imagesx($watermark)) {
$this->imagecopymergeWithAlphaFix(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$offsetX,
Expand Down Expand Up @@ -780,64 +780,71 @@ protected function _createEmptyImage($width, $height)
}

/**
* Fix an issue with the usage of imagecopymerge where the alpha channel is lost
* Copy source image onto destination image with given alpha percentage
*
* @param resource $dst_im
* @param resource $src_im
* @param int $dst_x
* @param int $dst_y
* @param int $src_x
* @param int $src_y
* @param int $src_w
* @param int $src_h
* @param int $pct
* @internal The arguments and functionality is the same as imagecopymerge
* but with proper handling of alpha transparency
*
* @param resource $destinationImage
* @param resource $sourceImage
* @param int $destinationX
* @param int $destinationY
* @param int $sourceX
* @param int $sourceY
* @param int $sourceWidth
* @param int $sourceHeight
* @param int $alphaPercentage
*
* @return bool
*/
private function imagecopymergeWithAlphaFix(
$dst_im,
$src_im,
$dst_x,
$dst_y,
$src_x,
$src_y,
$src_w,
$src_h,
$pct
private function copyImageWithAlphaPercentage(
$destinationImage,
$sourceImage,
$destinationX,
$destinationY,
$sourceX,
$sourceY,
$sourceWidth,
$sourceHeight,
$alphaPercentage
) {
if ($pct >= 100) {
return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
if (imageistruecolor($destinationImage) === false || imageistruecolor($sourceImage) === false) {
return imagecopymerge($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight, $alphaPercentage);
}

if ($alphaPercentage >= 100) {
return imagecopy($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
}

if ($pct < 0) {
if ($alphaPercentage < 0) {
return false;
}

$sizeX = imagesx($src_im);
$sizeY = imagesy($src_im);
if (false === $sizeX || false === $sizeY) {
$sizeX = imagesx($sourceImage);
$sizeY = imagesy($sourceImage);
if ($sizeX === false || $sizeY === false || $sizeX === 0 || $sizeY === 0) {
return false;
}

$tmpImg = imagecreatetruecolor($src_w, $src_h);
if (false === $tmpImg) {
$tmpImg = imagecreatetruecolor($sourceWidth, $sourceHeight);
if ($tmpImg === false) {
return false;
}

if (false === imagealphablending($tmpImg, false)) {
if (imagealphablending($tmpImg, false) === false) {
return false;
}

if (false === imagecopy($tmpImg, $src_im, 0, 0, 0, 0, $sizeX, $sizeY)) {
if (imagecopy($tmpImg, $sourceImage, 0, 0, 0, 0, $sizeX, $sizeY) === false) {
return false;
}

$transparancy = 127 - (($pct*127)/100);
if (false === imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparancy)) {
$transparency = 127 - (($alphaPercentage*127)/100);
if (imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparency) === false) {
return false;
}

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

return $result;
Expand Down

0 comments on commit 519f3e1

Please sign in to comment.