Skip to content

Commit

Permalink
🔃 [EngCom] Public Pull Requests - 2.2-develop
Browse files Browse the repository at this point in the history
Accepted Public Pull Requests:
 - #13408: Translate time zone label according to current locale in Stores > Configuration > Advanced Reporting (by @adrian-martinez-interactiv4)
 - #11060: Handle transparncy correctly for watermark (by @elzekool)


Fixed GitHub Issues:
 - #10661: Opacity png watermark became white box on product images (reported by @Koc) has been fixed in #11060 by @elzekool in 2.2-develop branch
   Related commits:
     1. 92f24ec
     2. 6581e80
     3. f2446b6
     4. 519f3e1
     5. 00a5c3a
     6. 503204c
  • Loading branch information
magento-engcom-team authored Jan 29, 2018
2 parents ae5e8df + 414bc5b commit 04d90c2
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,45 @@
*/
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
*/
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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,
Expand Down Expand Up @@ -387,7 +478,7 @@ public function testWatermark(
$this->assertFalse($result, $message);
}

public function imageWatermarkDataProvider()
public function imageWatermarkPositionDataProvider()
{
return $this->_prepareData(
[
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 113 additions & 8 deletions lib/internal/Magento/Framework/Image/Adapter/Gd2.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Framework\Image\Adapter;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class Gd2 extends \Magento\Framework\Image\Adapter\AbstractAdapter
{
/**
Expand Down Expand Up @@ -404,7 +407,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.'),
Expand Down Expand Up @@ -465,7 +468,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->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -478,7 +481,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
);
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_RIGHT) {
$positionX = $this->_imageSrcWidth - imagesx($watermark);
imagecopymerge(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -490,7 +493,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
$this->getWatermarkImageOpacity()
);
} elseif ($this->getWatermarkPosition() == self::POSITION_TOP_LEFT) {
imagecopymerge(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -504,7 +507,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->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -517,7 +520,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
);
} elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT) {
$positionY = $this->_imageSrcHeight - imagesy($watermark);
imagecopymerge(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -531,7 +534,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
}

if ($tile === false && $merged === false) {
imagecopymerge(
$this->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$positionX,
Expand All @@ -547,7 +550,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->copyImageWithAlphaPercentage(
$this->_imageHandler,
$watermark,
$offsetX,
Expand Down Expand Up @@ -778,4 +781,106 @@ protected function _createEmptyImage($width, $height)
$this->imageDestroy();
$this->_imageHandler = $image;
}

/**
* Copy source image onto destination image with given alpha percentage
*
* @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
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function copyImageWithAlphaPercentage(
$destinationImage,
$sourceImage,
$destinationX,
$destinationY,
$sourceX,
$sourceY,
$sourceWidth,
$sourceHeight,
$alphaPercentage
) {
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 ($alphaPercentage < 0) {
return false;
}

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

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

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

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

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

$result = imagecopy(
$destinationImage,
$tmpImg,
$destinationX,
$destinationY,
$sourceX,
$sourceY,
$sourceWidth,
$sourceHeight
);
imagedestroy($tmpImg);

return $result;
}
}
Loading

0 comments on commit 04d90c2

Please sign in to comment.