合并图像并保持透明度

Posted

技术标签:

【中文标题】合并图像并保持透明度【英文标题】:Merge images and preserve transparency 【发布时间】:2011-07-26 19:12:45 【问题描述】:

我有一个包含透明度的图像,我正在与另一个图像(在 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】:

我会推荐使用 phpThumb 库,它有很多不错的功能,包括保持透明度。它也适用于 GDLib 或 ImageMagick:

http://phpthumb.sourceforge.net/

【讨论】:

以上是关于合并图像并保持透明度的主要内容,如果未能解决你的问题,请参考以下文章

设置 UIButton 图像并保持透明度

PHP - 从 blob 文件创建图像并在保持透明度的同时与另一个文件组合

(Android) 如何填充ListView背景并保持标题透明度

如何使图像变暗以保持透明度不受 CSS 或 JS 影响?

Gif 图像格式中如何合并透明度

如何使用 pygame 在 Python 中保持 png 图像的透明度? [复制]