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
https://forums.phpfreaks.com/topic/88735-problems-with-transparent-truecolor-png/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.