合并图像并保持透明度

时间:2011-07-26 19:12:46

标签: php transparency gd

我有一个包含透明度的图像,我正在与另一个图像(在php中创建)合并,同时添加了一些文本。目前透明度似乎有效,但它使背景透明,在图像中留下一个大的剪切:

//creates a image handle
$img = imagecreate( 500, 200 );

$logo = imagecreatefrompng('logo.png');// <--- logo with transparent background, png24 from photoshop
imagealphablending($logo, true);
imagesavealpha($logo, true);

//choose a bg color, u can play with the rgb values
$background = imagecolorallocate( $img, 173, 184, 194);

//chooses the text color
$text_colour = imagecolorallocate( $img, 255, 255, 255 );

//sets the thickness/bolness of the line
imagesetthickness ( $img, 3 );

//pulls the value passed in the URL
$text = $_GET['name'];
$pos = $_GET['title'];

// place the font file in the same dir level as the php file
$font = 'NeutraText-BoldAlt.ttf';

//this function sets the font size, places to the co-ords
imagettftext($img, 30, 0, 11, 128, $text_colour, $font, $text);
//places another text with smaller size
imagettftext($img, 16, 0, 10, 155, $text_colour, $font, $pos);
// PUC
imagettftext($img, 16, 0, 10, 180, $text_colour, $font, "My Organization");

 // fix trans
imagealphablending($img, false);
imagesavealpha($img, true);

// Merge the images
imagecopyresampled($img, $logo, 10, 10, 0, 0, 150, 78, 150, 78);

//alerts the browser abt the type of content i.e. png image
header( 'Content-type: image/png' );

//now creates the image
imagepng( $img );

//destroys used resources
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $img );

添加到$ img后,我需要做些什么来保持$ logo的透明度?

1 个答案:

答案 0 :(得分:0)

我建议使用phpThumb库,它带有许多不错的功能,包括保持透明度。它也适用于GDLib或ImageMagick:

http://phpthumb.sourceforge.net/