Jump to content

Problems with transparent truecolor png


bjoern

Recommended Posts

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.

 

// 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:

grey.gif

 

How it should look like, regardless of background color:

white.gif

 

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  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.