Page 1 of 1

Problems with transparent truecolor png

Posted: Thu Jan 31, 2008 2:20 pm
by bjoern
Hi everyone

I got a problem I just can't seem to figure out :-\

I made this script for generating image reflection automatically. It all seems to work fine, except for one thing. I want the background of the png I am outputting to be transparent. Found a few ways to do that and it is transparent as of now.
However, when I copy-merge from from the original image to the new image, the semi-transparency of the pixels does not blend in with the transparent background, but rather the original color.

To see what I mean, here is the part of the code which is not work.

Code: Select all

 
// Create canvas for the output image
$image = @imagecreatetruecolor($width, $height + $size + $offset);
 
    // Alpha and color properties required for transparent background
    imagesavealpha($image, true);
    $background-color = imagecolorallocatealpha($image, 255, 255, 255, 127);
    imagefill($image, 0, 0, $background-color);
 
// Copy the original image to the canvas
imagecopymerge($image, $imageinput, 0, 0, 0, 0, $width, $height, 100);
    
// Flip the image vertical
$opacity = $transparency / $size;
 
for ($y = 0; $y < $size; $y++)
    {
        $x = $x + $opacity;
        imagecopymerge($image, $imageinput, 0, $height + $offset + $y, 0, $height - $y - 1, $width, 1, $transparency - $x);
    }
    
imagepng($image);
 
The for-loop copies from the input image to the new image i created and is transparent the way it should be, but it somehow ignores that the background of $image is set to opacity: 127..

Here is a screenshot, so you can see what I mean:

Where the bug is showing:
Image

How it should look like, regardless of background color:
Image

So to summarize, I want the bottom part (the reflection) to be properly transparent and blend into whatever is in the background. As often the case, I fully understand the problem, but I do not have the solution :)

Hope someone can help or perhaps provide an alternative solution.

Thanks in advance :)

Re: Problems with transparent truecolor png

Posted: Fri Feb 08, 2008 1:02 pm
by bjoern
*bump*