php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50183 PNG alpha blending doesn't seem to work
Submitted: 2009-11-15 16:38 UTC Modified: 2017-10-24 07:39 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: scratch65535 at att dot net Assigned:
Status: Open Package: GD related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2009-11-15 16:38 UTC] scratch65535 at att dot net
Description:
------------
I am trying to combine PNG images by 'pasting' them onto a transparent background such that I end up with one large image (the 'assembly') rather than a multitude of small ones.  I'm using PNG because I need more than 8 bits/pixel.  I created the images in p'shop 7.

Using a GIF works fine, apart from it having too few bits per pixel.

Using a PNG with a designated xparent color ("GIF mode", as it were) also works fine.

PNG with alpha doesn't.  I don't know whether the problem is in the documentation, in imageCreateFromPNG(), in my understanding, or somewhere else.  I've fooled around with different combinations (e.g., putting in or taking out imageAlphaBlending() for the $dest value) but nothing seems to help.

(Using PNGs in 'GIF mode' will be my fallback if alpha is broken, but it would mean re-doing all my images so I'm hoping alpha is NOT broken)

Reproduce code:
---------------
$png = 1 ;

if ( $png == 1 )  // png in 'gif mode'
{
    $dest       = imageCreateTrueColor( 200, 200 ) ;
    $xparent    = imageColorAllocate( $dest, 220,2,240 ) ;
    imageFilledRectangle($dest, 0,0, 200,200, $xparent) ;
    imageColorTransparent($dest, $xparent);

    $src        = imageCreateFromPNG( 'TESTcolor.png' ) ;
    $srcxparent = imageColorAllocate( $src, 0xFF,0,0xFF ) ;
    imageColorTransparent($src, $srcxparent ) ;
    imageCopyMerge( $dest, $src, 50, 50, 0, 0, 100, 100,  100 ) ;

    header('Content-Type: image/png') ;
    imagePNG( $dest ) ;
}
else // png with alpha
{
    $dest       = imageCreateTrueColor( 200, 200 ) ;
    $xparent    = imageColorAllocateAlpha( $dest, 0xFF,0x99,0xFF, 0 );
    imageFilledRectangle($dest, 0,0, 200,200, $xparent) ;
    imageAlphaBlending( $dest, 1 ) ;    
    imageSaveAlpha( $dest, 1 ) ;
   
    $src        = imageCreateFromPNG( 'TESTalpha.png' ) ;
    imageAlphaBlending( $src, 1 ) ;
    imageSaveAlpha( $src, 1 ) ;
    imageCopyMerge( $dest, $src, 50, 50, 0, 0, 100, 100,  100 ) ;

    header('Content-Type: image/png') ;
    imagePNG( $dest ) ;
}


Expected result:
----------------
I expect to see a dark blue outline box on the background color of the page itself.

Actual result:
--------------
I see a dark blue outline box on an opaque white field overlaying the assembly field that I painted magenta.  I can get rid of the magenta by calling imageColorTransparent() on $dest, but nothing I can do gets rid of the white background of the box.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-15 16:46 UTC] pajoye@php.net
Please provide a link to the image you get and what you would expect.
 [2009-11-15 17:30 UTC] scratch65535 at att dot net
AT&T is my only web access, and they don't seem to support PHP.  I can upload screenshots if that's what you're looking for, but of course they'll just be dead bitmaps.
 [2009-11-15 17:31 UTC] pajoye@php.net
Upload the two images here: http://kalleload.net/, that will do it just fine.
 [2009-11-15 17:50 UTC] scratch65535 at att dot net
Expected:
http://www.kalleload.net/uploads/gricmj/suhmidvvdrvt.jpg 

Got:
http://www.kalleload.net/uploads/otgxlc/fdahibpvswas.jpg

As I mentioned, I can get rid of the magenta - I left it in to show the size of the "assembly" area.  But that white background in the PNG defeats me.
 [2009-11-15 18:21 UTC] pajoye@php.net
Quick note: imagecopymerge is not supposed to work with alpha. See the examples code here: http://pierre.libgd.org/watermark/
 [2009-11-15 18:26 UTC] scratch65535 at att dot net
UGH!  I just did another test using a more sophisticated image (semi-transparent pixels) and "GIF mode" doesn't work for what I want.

It seems possible that imageCopyMerge() might be the culprit here or at least one of them, because if I call imagePNG on the $src, I don't get the white background color.
 [2009-11-15 18:29 UTC] scratch65535 at att dot net
So the documentation is broken then?

"The two images will be merged according to pct which can range from 0 to 100. When pct = 0, no action is taken, when 100 this function behaves identically to imagecopy() for pallete images, while it implements alpha transparency for true colour images. "
 [2009-11-23 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".
 [2009-11-23 14:42 UTC] scratch65535 at att dot net
I'm confused.  The status shows as "no feedback" in r/o mode, but "open" in edit mode.  I don't know what feedback was/is expected - as far as I'm aware, it's an open problem.  Am I expected to post another comment every week to keep it open, or what?
 [2009-11-23 19:25 UTC] pajoye@php.net
It looks to me to be a misusage for imagecopymerge. I have to test with your code to be sure. move to assigned.
 [2010-06-14 10:42 UTC] pajoye@php.net
-Summary: What feedback is expected? +Summary: PNG alpha blending doesn't seem to work
 [2017-01-23 14:06 UTC] cmb@php.net
The images are not available anymore, so I can't exactly reproduce
the issue. However, imagecopymerge() is supposed to *emulate*
alpha by means of the $pct parameter. For instance, $pct==50 means
that all pixels of the $src_im will be treated as if their alpha
channel would be 50% (i.e. 64).

In your case, you likely will want to set `alphablending($dest,
false)` and use imagecopy() instead of imagecopymerge(). For more
complex cases where $pct!=100 see my comment on bug #23815.

As the documentation of imagecopymerge() is obviously not clear
enough, I'm changing to doc bug.
 [2017-01-23 14:07 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2017-10-24 07:39 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: pajoye +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 10:01:28 2024 UTC