From 92f24eccffdab3beafc0957a14f2603a5a0d0669 Mon Sep 17 00:00:00 2001 From: Elze Kool Date: Tue, 26 Sep 2017 14:47:19 +0200 Subject: [PATCH 1/6] Handle transparncy correctly for watermark The watermark functionality uses imagecopymerge for copying the watermark into the image, this function loses the alpha information. Therefor use imagecopy for 100% opacity and use imagefilter in other cases. Resolves: #10661 --- .../Magento/Framework/Image/Adapter/Gd2.php | 80 +++++++++++++++++-- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php index 444ab7113d429..de48e234a58d2 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php +++ b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php @@ -404,7 +404,7 @@ public function rotate($angle) */ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = 30, $tile = false) { - list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = $this->_getImageOptions($imagePath); + list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType,) = $this->_getImageOptions($imagePath); $this->_getFileAttributes(); $watermark = call_user_func( $this->_getCallback('create', $watermarkFileType, 'Unsupported watermark image format.'), @@ -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; - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -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); - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -490,7 +490,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = $this->getWatermarkImageOpacity() ); } elseif ($this->getWatermarkPosition() == self::POSITION_TOP_LEFT) { - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -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); - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -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); - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -531,7 +531,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = } if ($tile === false && $merged === false) { - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $positionX, @@ -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)) { - imagecopymerge( + $this->imagecopymergeWithAlphaFix( $this->_imageHandler, $watermark, $offsetX, @@ -778,4 +778,68 @@ protected function _createEmptyImage($width, $height) $this->imageDestroy(); $this->_imageHandler = $image; } + + /** + * Fix an issue with the usage of imagecopymerge where the alpha channel is lost + * + * @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 + * + * @return bool + */ + private function imagecopymergeWithAlphaFix( + $dst_im, + $src_im, + $dst_x, + $dst_y, + $src_x, + $src_y, + $src_w, + $src_h, + $pct + ) { + if ($pct >= 100) { + return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); + } + + if ($pct < 0) { + return false; + } + + $sizeX = imagesx($src_im); + $sizeY = imagesy($src_im); + if (false === $sizeX || false === $sizeY) { + return false; + } + + $tmpImg = imagecreatetruecolor($src_w, $src_h); + if (false === $tmpImg) { + return false; + } + + if (false === imagealphablending($tmpImg, false)) { + return false; + } + + if (false === imagecopy($tmpImg, $src_im, 0, 0, 0, 0, $sizeX, $sizeY)) { + return false; + } + + $transparancy = 127 - (($pct*127)/100); + if (false === imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparancy)) { + return false; + } + + $result = imagecopy($dst_im, $tmpImg, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); + imagedestroy($tmpImg); + + return $result; + } } From f2446b637679ed7d39dd73b54c13f2bd0d3e45a6 Mon Sep 17 00:00:00 2001 From: Elze Kool Date: Sun, 19 Nov 2017 10:52:08 +0100 Subject: [PATCH 2/6] Fix handling of watermark with alpha for Imagick When adding a watermark that already has an alpha channel this was reset because the overal opacity was set for the image. Instead we should multiply the original alpha with the requested opacity --- .../Framework/Image/Adapter/ImageMagick.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php b/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php index 50b9a5a013273..29f3c85dc1205 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php +++ b/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php @@ -269,15 +269,17 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = ); } - if (method_exists($watermark, 'setImageOpacity')) { - // available from imagick 6.3.1 - $watermark->setImageOpacity($opacity); - } else { - // go to each pixel and make it transparent - $watermark->paintTransparentImage($watermark->getImagePixelColor(0, 0), 1, 65530); - $watermark->evaluateImage(\Imagick::EVALUATE_SUBTRACT, 1 - $opacity, \Imagick::CHANNEL_ALPHA); + if (method_exists($watermark, 'getImageAlphaChannel')) { + // available from imagick 6.4.0 + if ($watermark->getImageAlphaChannel() == 0) { + $watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE); + } } + $compositeChannels = \Imagick::CHANNEL_ALL; + $watermark->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity, \Imagick::CHANNEL_OPACITY); + $compositeChannels &= ~(\Imagick::CHANNEL_OPACITY); + switch ($this->getWatermarkPosition()) { case self::POSITION_STRETCH: $watermark->sampleImage($this->_imageSrcWidth, $this->_imageSrcHeight); @@ -309,14 +311,14 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = $offsetY = $positionY; while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) { while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) { - $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY); + $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY, $compositeChannels); $offsetX += $watermark->getImageWidth(); } $offsetX = $positionX; $offsetY += $watermark->getImageHeight(); } } else { - $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $positionX, $positionY); + $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $positionX, $positionY, $compositeChannels); } } catch (\ImagickException $e) { throw new \Exception('Unable to create watermark.', $e->getCode(), $e); From 519f3e1c608e0ee0105f8cec0244b822c3d17cee Mon Sep 17 00:00:00 2001 From: Elze Kool Date: Sun, 19 Nov 2017 10:54:57 +0100 Subject: [PATCH 3/6] Handle non true color images with GD2 --- .../Magento/Framework/Image/Adapter/Gd2.php | 87 ++++++++++--------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php index de48e234a58d2..6817c01b19cbf 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php +++ b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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; From 00a5c3a2f53c5c880210157fa44648688c212070 Mon Sep 17 00:00:00 2001 From: Elze Kool Date: Sun, 19 Nov 2017 11:03:45 +0100 Subject: [PATCH 4/6] Add integration test for watermark with alpha Added integration tests for handling alpha transparency in watermarks. Renamed the current test for watermarks as it only tests if the watermark is correctly places --- .../Framework/Image/Adapter/InterfaceTest.php | 97 +++++++++++++++++- .../Image/_files/watermark_alpha.png | Bin 0 -> 202 bytes .../_files/watermark_alpha_base_image.jpg | Bin 0 -> 1252 bytes 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha.png create mode 100644 dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha_base_image.jpg diff --git a/dev/tests/integration/testsuite/Magento/Framework/Image/Adapter/InterfaceTest.php b/dev/tests/integration/testsuite/Magento/Framework/Image/Adapter/InterfaceTest.php index 420803d9530bd..bc8281d55ac4f 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Image/Adapter/InterfaceTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Image/Adapter/InterfaceTest.php @@ -337,6 +337,97 @@ public function rotateDataProvider() ); } + /** + * Test if alpha transparency is correctly handled + * + * @param string $image + * @param string $watermark + * @param int $alphaPercentage + * @param array $comparePoint1 + * @param array $comparePoint2 + * @param string $adapterType + * + * @dataProvider imageWatermarkWithAlphaTransparencyDataProvider + * @depends testOpen + * @depends testImageSize + */ + public function testWatermarkWithAlphaTransparency( + $image, + $watermark, + $alphaPercentage, + $comparePoint1, + $comparePoint2, + $adapterType + ) { + $imageAdapter = $this->_getAdapter($adapterType); + $imageAdapter->open($image); + + $watermarkAdapter = $this->_getAdapter($adapterType); + $watermarkAdapter->open($watermark); + + list($comparePoint1X, $comparePoint1Y) = $comparePoint1; + list($comparePoint2X, $comparePoint2Y) = $comparePoint2; + + $imageAdapter + ->setWatermarkImageOpacity($alphaPercentage) + ->setWatermarkPosition(\Magento\Framework\Image\Adapter\AbstractAdapter::POSITION_TOP_LEFT) + ->watermark($watermark); + + $comparePoint1Color = $imageAdapter->getColorAt($comparePoint1X, $comparePoint1Y); + unset($comparePoint1Color['alpha']); + + $comparePoint2Color = $imageAdapter->getColorAt($comparePoint2X, $comparePoint2Y); + unset($comparePoint2Color['alpha']); + + $result = $this->_compareColors($comparePoint1Color, $comparePoint2Color); + $message = sprintf( + '%s should be different to %s due to alpha transparency', + join(',', $comparePoint1Color), + join(',', $comparePoint2Color) + ); + $this->assertFalse($result, $message); + } + + public function imageWatermarkWithAlphaTransparencyDataProvider() + { + return $this->_prepareData( + [ + // Watermark with alpha channel, 25% + [ + $this->_getFixture('watermark_alpha_base_image.jpg'), + $this->_getFixture('watermark_alpha.png'), + 25, + [ 23, 3 ], + [ 23, 30 ] + ], + // Watermark with alpha channel, 50% + [ + $this->_getFixture('watermark_alpha_base_image.jpg'), + $this->_getFixture('watermark_alpha.png'), + 50, + [ 23, 3 ], + [ 23, 30 ] + ], + // Watermark with no alpha channel, 50% + [ + $this->_getFixture('watermark_alpha_base_image.jpg'), + $this->_getFixture('watermark.png'), + 50, + [ 3, 3 ], + [ 23,3 ] + ], + // Watermark with no alpha channel, 100% + [ + $this->_getFixture('watermark_alpha_base_image.jpg'), + $this->_getFixture('watermark.png'), + 100, + [ 3, 3 ], + [ 3, 60 ] + ], + ] + ); + } + /** * Checks if watermark exists on the right position * @@ -350,10 +441,10 @@ public function rotateDataProvider() * @param int $colorY * @param string $adapterType * - * @dataProvider imageWatermarkDataProvider + * @dataProvider imageWatermarkPositionDataProvider * @depends testOpen */ - public function testWatermark( + public function testWatermarkPosition( $image, $watermark, $width, @@ -387,7 +478,7 @@ public function testWatermark( $this->assertFalse($result, $message); } - public function imageWatermarkDataProvider() + public function imageWatermarkPositionDataProvider() { return $this->_prepareData( [ diff --git a/dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha.png b/dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha.png new file mode 100644 index 0000000000000000000000000000000000000000..af05a14a99fdd34e8f5edb8740fec06d5d3ccd8e GIT binary patch literal 202 zcmeAS@N?(olHy`uVBq!ia0vp^ra-L5!3HE#mRxoPQk(@Ik;M!Qd`Cc-ajG_-G*Ga{ z)5S3)qV??+N8W}29+r!mD>>IZ;B(A)*dwZU)WvhN{cn*sD!z*o(hYBaX|T8OYF!)x zC0v<~f9c2zEb#4rG3|h{?V@O=_dve49hdvcf+;{E!bpqj-Alo{ixw<%FNyy2M`r1v jKrJYt)L2~QvYr23A7_64ts-NfI~Y7&{an^LB{Ts5PkTog literal 0 HcmV?d00001 diff --git a/dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha_base_image.jpg b/dev/tests/integration/testsuite/Magento/Framework/Image/_files/watermark_alpha_base_image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..361ff523d2130ccfccd86a935f2f72053420e766 GIT binary patch literal 1252 zcmex=5A1R0qH8UG()kY`|EWCgkw?FU=d^$QZ#gA6AnydS1J@SYMi)`L)mHLLD8U# zA5@H!ikdjN#Ka{erBv0_H8izMOwG(KEUlbfT;1F~JiUTLLc_u%BBPR1Qq$5iGP89XZ3R<7E#dCS&q+js2Tb?ESsqsNY) zIC<*Qo&Gyn;}30F zQ~&r*t#8`A{jHf1{~3<&_%&;lyyWMS{r&ngle#q4UEZRxsEdJA{Ny^wlCZ4555KO} zda$DEYZc?`A7NAb|5W#{Tyy#Qhqc=BKfcQchdMvD%bWi3>1UllSJ7K#ts;RAtR!NY zSztTP#_zG)x->?VKW@E)Z9UhejsF??3%{Dqs`@9oH2FV++~ZeSD{9iuUixRVFSSc! z(QdyBi@G!zNW{#O!L~%M{@#9V%j%|uudlvfU;i*F%>Ls$yJegG;ei>--}7@<{PF2$ zHoA(0XWkMCbY&nF7iEL(Fs=LW>)O=&iL=m&O7n5;31I*bWi&6d@8C@Z=nEn4v08E$z Aw*UYD literal 0 HcmV?d00001 From 503204c04627454ebe7e59e076089de640b8fe8b Mon Sep 17 00:00:00 2001 From: Elze Kool Date: Sun, 19 Nov 2017 21:20:44 +0100 Subject: [PATCH 5/6] Fix issues with static tests --- .../Magento/Framework/Image/Adapter/Gd2.php | 40 +++++++++++++++++-- .../Framework/Image/Adapter/ImageMagick.php | 16 +++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php index 6817c01b19cbf..a36e41a526466 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/Gd2.php +++ b/lib/internal/Magento/Framework/Image/Adapter/Gd2.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\Image\Adapter; +/** + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + */ class Gd2 extends \Magento\Framework\Image\Adapter\AbstractAdapter { /** @@ -796,6 +799,9 @@ protected function _createEmptyImage($width, $height) * @param int $alphaPercentage * * @return bool + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function copyImageWithAlphaPercentage( $destinationImage, @@ -809,11 +815,30 @@ private function copyImageWithAlphaPercentage( $alphaPercentage ) { if (imageistruecolor($destinationImage) === false || imageistruecolor($sourceImage) === false) { - return imagecopymerge($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight, $alphaPercentage); + return imagecopymerge( + $destinationImage, + $sourceImage, + $destinationX, + $destinationY, + $sourceX, + $sourceY, + $sourceWidth, + $sourceHeight, + $alphaPercentage + ); } if ($alphaPercentage >= 100) { - return imagecopy($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight); + return imagecopy( + $destinationImage, + $sourceImage, + $destinationX, + $destinationY, + $sourceX, + $sourceY, + $sourceWidth, + $sourceHeight + ); } if ($alphaPercentage < 0) { @@ -844,7 +869,16 @@ private function copyImageWithAlphaPercentage( return false; } - $result = imagecopy($destinationImage, $tmpImg, $destinationX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight); + $result = imagecopy( + $destinationImage, + $tmpImg, + $destinationX, + $destinationY, + $sourceX, + $sourceY, + $sourceWidth, + $sourceHeight + ); imagedestroy($tmpImg); return $result; diff --git a/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php b/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php index 29f3c85dc1205..5d9ef49c7d285 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php +++ b/lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php @@ -311,14 +311,26 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = $offsetY = $positionY; while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) { while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) { - $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY, $compositeChannels); + $this->_imageHandler->compositeImage( + $watermark, + \Imagick::COMPOSITE_OVER, + $offsetX, + $offsetY, + $compositeChannels + ); $offsetX += $watermark->getImageWidth(); } $offsetX = $positionX; $offsetY += $watermark->getImageHeight(); } } else { - $this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $positionX, $positionY, $compositeChannels); + $this->_imageHandler->compositeImage( + $watermark, + \Imagick::COMPOSITE_OVER, + $positionX, + $positionY, + $compositeChannels + ); } } catch (\ImagickException $e) { throw new \Exception('Unable to create watermark.', $e->getCode(), $e); From ca460ec88ac0c7bcebfaad1af7f733bfd243643b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mart=C3=ADnez?= Date: Mon, 29 Jan 2018 04:12:40 +0100 Subject: [PATCH 6/6] Adapt \Magento\Analytics\Block\Adminhtml\System\Config\CollectionTimeLabel::render method to use current locale to render time zone label, as done in \Magento\Framework\Locale\TranslatedLists::getOptionTimezones --- .../System/Config/CollectionTimeLabel.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Analytics/Block/Adminhtml/System/Config/CollectionTimeLabel.php b/app/code/Magento/Analytics/Block/Adminhtml/System/Config/CollectionTimeLabel.php index c4118792255cd..34f2b7d53d9be 100644 --- a/app/code/Magento/Analytics/Block/Adminhtml/System/Config/CollectionTimeLabel.php +++ b/app/code/Magento/Analytics/Block/Adminhtml/System/Config/CollectionTimeLabel.php @@ -5,13 +5,35 @@ */ namespace Magento\Analytics\Block\Adminhtml\System\Config; +use Magento\Framework\App\ObjectManager; + /** * Provides label with default Time Zone */ class CollectionTimeLabel extends \Magento\Config\Block\System\Config\Form\Field { /** - * Add default time zone to comment + * @var \Magento\Framework\Locale\ResolverInterface + */ + private $localeResolver; + + /** + * @param \Magento\Backend\Block\Template\Context $context + * @param array $data + * @param \Magento\Framework\Locale\ResolverInterface|null $localeResolver + */ + public function __construct( + \Magento\Backend\Block\Template\Context $context, + array $data = [], + \Magento\Framework\Locale\ResolverInterface $localeResolver = null + ) { + $this->localeResolver = $localeResolver ?: + ObjectManager::getInstance()->get(\Magento\Framework\Locale\ResolverInterface::class); + parent::__construct($context, $data); + } + + /** + * Add current time zone to comment, properly translated according to locale * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string @@ -19,7 +41,9 @@ class CollectionTimeLabel extends \Magento\Config\Block\System\Config\Form\Field public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $timeZoneCode = $this->_localeDate->getConfigTimezone(); - $getLongTimeZoneName = \IntlTimeZone::createTimeZone($timeZoneCode)->getDisplayName(); + $locale = $this->localeResolver->getLocale(); + $getLongTimeZoneName = \IntlTimeZone::createTimeZone($timeZoneCode) + ->getDisplayName(false, \IntlTimeZone::DISPLAY_LONG, $locale); $element->setData( 'comment', sprintf("%s (%s)", $getLongTimeZoneName, $timeZoneCode)