Skip to content

Handle transparncy correctly for watermark #11060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

elzekool
Copy link
Contributor

@elzekool elzekool commented Sep 26, 2017

Handle alpha transparency correctly for 24-bit PNG image

Description

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.

Fixed Issues (if relevant)

  1. Opacity png watermark became white box on product images #10661: Opacity png watermark became white box on product images

Manual testing scenarios

  1. Create a 24-bit PNG image with alpha transparency (or use the image in issue in Opacity png watermark became white box on product images #10661)
  2. Configure the image under "Product image watermarks"
  3. Leave size empty, set image position to something different than stretch
  4. Create a product with a JPEG image
  5. See that the watermark is applied with the correct transparency.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

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: magento#10661
@elzekool elzekool force-pushed the bugfix/handle-alpha-transparency-correctly-for-watermark branch from 9f1fe15 to 92f24ec Compare September 27, 2017 15:50
@orlangur orlangur added this to the September 2017 milestone Sep 28, 2017
@vrann vrann reopened this Sep 29, 2017
@@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the proper name for this function should be something like copyImage as we are not even using imagecopymerge anymore.

*
* @return bool
*/
private function imagecopymergeWithAlphaFix(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just mention in PHPDoc of this method that signature matches http://php.net/manual/en/function.imagecopymerge.php function but name arguments properly: $destinationImage, $sourceImage, $destinationX, etc.


$sizeX = imagesx($src_im);
$sizeY = imagesy($src_im);
if (false === $sizeX || false === $sizeY) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert all Yoda-style conditions to normal ones.

Is checking for false really necessary here - do we need to continue execution flow when $sizeX === 0?

@orlangur
Copy link
Contributor

Thanks, fix looks quite good in general, especially that you changed all the places. Probably we should even add imagecopymerge function to list of disallowed functions :)

Would be really nice to cover fix with integration test using mentioned image. Please tell us if you need any assistance with that.

@Koc
Copy link

Koc commented Oct 6, 2017

Have checked manually this PR on my env - works perfectly, thanx.

Does image magick also requires changes?

@orlangur
Copy link
Contributor

Hi @elzekool, are you going to continue work on this PR?

@orlangur
Copy link
Contributor

@Koc no as problematic function imagecopymerge belongs to gd extension :)

As you already tested this PR, maybe you will be interested in fixing CR notes so that it can be merged? You just need to checkout it locally as described in https://help.github.com/articles/checking-out-pull-requests-locally/, commit your changes on top and create a new PR. With merge of new PR they both will be marked as Merged.

@orlangur
Copy link
Contributor

Ping @Koc @elzekool :)

@okorshenko okorshenko modified the milestones: October 2017, November 2017 Nov 1, 2017
@magento-engcom-team magento-engcom-team added 2.2.x bugfix Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release labels Nov 7, 2017
@orlangur
Copy link
Contributor

Closing due to inactivity. Feel free to reach me out anytime later if you wish to continue work on pull request and it will be reopened.

@orlangur orlangur closed this Nov 10, 2017
@elzekool
Copy link
Contributor Author

Hi, sorry for the late response. Been realy busy, want to pick it up again

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
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
@orlangur
Copy link
Contributor

@elzekool no problem, please go ahead 👍

@orlangur orlangur reopened this Nov 19, 2017
@okorshenko okorshenko removed the 2.2.x label Dec 14, 2017
@okorshenko okorshenko modified the milestones: December 2017, January 2018 Jan 8, 2018
@magento-engcom-team magento-engcom-team added the Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release label Jan 8, 2018
@okorshenko okorshenko assigned okorshenko and unassigned orlangur Jan 10, 2018
@magento-engcom-team
Copy link
Contributor

@elzekool thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Release Line: 2.2 Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants