Skip to content

Commit f2446b6

Browse files
committed
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
1 parent 6581e80 commit f2446b6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,17 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
269269
);
270270
}
271271

272-
if (method_exists($watermark, 'setImageOpacity')) {
273-
// available from imagick 6.3.1
274-
$watermark->setImageOpacity($opacity);
275-
} else {
276-
// go to each pixel and make it transparent
277-
$watermark->paintTransparentImage($watermark->getImagePixelColor(0, 0), 1, 65530);
278-
$watermark->evaluateImage(\Imagick::EVALUATE_SUBTRACT, 1 - $opacity, \Imagick::CHANNEL_ALPHA);
272+
if (method_exists($watermark, 'getImageAlphaChannel')) {
273+
// available from imagick 6.4.0
274+
if ($watermark->getImageAlphaChannel() == 0) {
275+
$watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE);
276+
}
279277
}
280278

279+
$compositeChannels = \Imagick::CHANNEL_ALL;
280+
$watermark->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity, \Imagick::CHANNEL_OPACITY);
281+
$compositeChannels &= ~(\Imagick::CHANNEL_OPACITY);
282+
281283
switch ($this->getWatermarkPosition()) {
282284
case self::POSITION_STRETCH:
283285
$watermark->sampleImage($this->_imageSrcWidth, $this->_imageSrcHeight);
@@ -309,14 +311,14 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
309311
$offsetY = $positionY;
310312
while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) {
311313
while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) {
312-
$this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY);
314+
$this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY, $compositeChannels);
313315
$offsetX += $watermark->getImageWidth();
314316
}
315317
$offsetX = $positionX;
316318
$offsetY += $watermark->getImageHeight();
317319
}
318320
} else {
319-
$this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $positionX, $positionY);
321+
$this->_imageHandler->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $positionX, $positionY, $compositeChannels);
320322
}
321323
} catch (\ImagickException $e) {
322324
throw new \Exception('Unable to create watermark.', $e->getCode(), $e);

0 commit comments

Comments
 (0)