Merge images and preserve transparency
I have an image that contains transparency which I am merging with another image (created in php) along with getting some text added. Currently the transparency DOES seem to work, but it makes the background transparent behind it, leaving a large cutout in the image:
//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开发者_JS百科);
//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 );
What do I need to do to preserve the transparency of $logo when added to $img?
I would recommend using the phpThumb library, it comes with a lot of nice features including preserving transparency. It also works with GDLib or ImageMagick:
http://phpthumb.sourceforge.net/
精彩评论