php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40572 Alpha composite allows color to bleed through
Submitted: 2007-02-21 05:01 UTC Modified: 2008-11-10 01:00 UTC
From: seth at pricepages dot org Assigned: pajoye (profile)
Status: No Feedback Package: GD related
PHP Version: 5.2.1 OS: Mac 10.4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: seth at pricepages dot org
New email:
PHP Version: OS:

 

 [2007-02-21 05:01 UTC] seth at pricepages dot org
Description:
------------
I am filling the background of an image with a transparent red 
(it shouldn't have an effect on the rest of the drawing). Over 
it, I'm drawing a black, semi-transparent, square.



Reproduce code:
---------------
$img = imagecreatetruecolor(100, 100);
imagealphablending($img, true);

$trans = imagecolorresolvealpha($img,255,0,0, 127);
imagefill($img, 0,0, $trans);

$pTrans = imagecolorresolvealpha($img, 0,0,0, 64);
imagefilledrectangle($img, 10, 10, 50, 50, $pTrans);

imagealphablending($img, false);
imagesavealpha($img,true);

header('Content-Type: image/png');
imagepng($img);

Expected result:
----------------
I would expect the resulting image to be 100% transparent, 
except for a grey, 50% transparent square.

Actual result:
--------------
Instead, the black is mixed with the red to form a dark-red 
semi-transparent square. The red color should not be there, 
because it was 100% transparent.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-21 10:03 UTC] pajoye@php.net
imagefill does not care about alpha blending but when you use a tiled color (as it is actually an image/pattern filling).

"$trans = imagecolorresolvealpha($img,255,0,0, 127);"

Will always fill the area with red and a 100% transparent alpha.


"imagealphablending($img, true);" enables the blending mode. Set it to false will store the alpha in the image.

You set it to true, that's why you get a "red/black" rectangle instead of a gray semi transparent area.

However I agree that the behavior is not user friendly. The GD 2.0.34 behaves differently in a more logic way when the dst pixel is either fully transparent or opaque.

OS X Fink already have 2.0.34, you can try to compile php against it. Or you can see the different implementation in the GD sources (function gdAlphaBlend):

http://cvs.php.net/viewvc.cgi/gd/libgd/gd.c


I will try to sync php gdAlphaBlend as soon as possible. I have to check that it will not break BC in one way or another (I do not think it will, but still need to test).


Do you get what you expect using the 2.0.34's gdAlphaBlend?
 [2007-02-23 00:38 UTC] seth at pricepages dot org
Well, I ran the following code against the newly released C 
library, and things seem to work as expected. GD 2.0.33 seg 
faults for some reason, but 2.0.34 fixes that. So, the bug 
will be fixed when you sync with PHP's code?

    gdImagePtr img;
    int trans, pTrans;
    FILE *fp;
    
    img = gdImageCreateTrueColor(100, 100);
    gdImageAlphaBlending(img, 1);
    
    trans = gdImageColorResolveAlpha(img,255,0,0, 127);
    gdImageFill(img, 0,0, trans);
    
    pTrans = gdImageColorResolveAlpha(img, 0,0,0, 64);
    gdImageFilledRectangle(img, 10, 10, 50, 50, pTrans);
    
    gdImageAlphaBlending(img, 0);
    gdImageSaveAlpha(img,1);
    
    fp = fopen("test.png", "w");
    gdImagePng(img, fp);
    fclose(fp);
    gdImageDestroy(img);
 [2007-02-23 00:50 UTC] pajoye@php.net
Yes, it will work the same way in php as in 2.0.34.

But you can already have the same result by disabling the alpha blending:

$img = imagecreatetruecolor(100, 100);
imagealphablending($img, false); // <<< Here
$trans = imagecolorresolvealpha($img,255,0,0, 127);
imagefill($img, 0,0, $trans);
$pTrans = imagecolorresolvealpha($img, 0,0,0, 64);
imagefilledrectangle($img, 10, 10, 50, 50, $pTrans);
imagesavealpha($img,true);
imagepng($img);



 [2008-11-02 12:40 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2008-11-10 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 13:01:27 2024 UTC