Oleh Prypin
Oleh Prypin

Reputation: 34116

PHP: Merging images into an alpha translucent image with GD

I'm trying to create an image with varying level of transparency, using GD. But I can get only fully transparent or fully opaque pixels in the image.

What I want to get:                                                      What I actually get:

(source: bx.at.ua)

                                                               
(source: bx.at.ua)


(source: bx.at.ua)

      
(source: bx.at.ua)

Here's a piece of the code I used to create images like this one ▲ fixed code:

$im=imagecreatetruecolor($sx,$sy);
imageantialias($im,true);
imagesavealpha($im,true);

$c00FF00=imagecolorallocate($im,0,255,0);
$cFFFFFF=imagecolorallocate($im,255,255,255); $cFFFFFF_00=imagecolorallocatealpha($im,255,255,255,127); imagecolortransparent($im,$cFFFFFF);

imagefilledrectangle($im,0,0,$sx,$sy,$cFFFFFF$cFFFFFF_00);

$sim=imagecreatefrompng('gradient.png');
imagecopy($im,$sim,$dest_x,$dest_y,0,0,imagesx($sim),imagesy($sim));
imagedestroy($sim);

imagettftext($im,$size,0,$text_x,$text_y,$c00FF00,$font,'Test');

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

What can I do to get the desired (translucent) result?

Upvotes: 1

Views: 1415

Answers (2)

Stan Rogers
Stan Rogers

Reputation: 2170

You need to call imagesavealpha() against your imported PNG ($sim) before merging, as well as against the final image.

Upvotes: 2

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

What you are looking for is not transparency but alpha. Alpha is another color channel that goes along with the red, green and blue colors to determine the transparency level (just transparent is visible or not as opposed to being able to set the level of transparency). Check out imagecolorallocatealpha

Upvotes: 0

Related Questions