使用 GD keep transparent 将透明图像合二为一
Combines transparent images in one using GD keep transparent
我有一个透明图像和比图像宽的文本,我试图嵌入透明图像顶部居中的文本并获得最终的透明 png 图像,我已将透明文本生成为容器具有宽度和高度,但当与图像结合时,它带有黑色背景
function createImageServer($name, $state)
{
$path = 'js/FusionCharts/FusionCharts_XT_Website/Charts/Resources/';
$server = $path . 'server' . $state . '.png';
list($width, $height, $type, $attr) = getimagesize($server);
$font_path = 'images_machines/FreeSans.ttf';
$bbox = imagettfbbox(10,0,$font_path,$name);
$diffHeight = 5;
$fontHeight = ($bbox[1] - $bbox[7]) + $diffHeight;
$textWidth = $bbox[2] + $bbox[0];
$dstimage = imagecreatetruecolor($textWidth,$height + $fontHeight);
imagealphablending($dstimage, false);
//Create alpha channel for transparent layer
$col=imagecolorallocatealpha($dstimage,255,255,255,127);
imagefilledrectangle($dstimage,0,0,$textWidth, $height+$fontHeight,$col);
imagealphablending($dstimage,true);
imagefttext($dstimage,10,0,0,$fontHeight-$diffHeight,0,$font_path,$name);
$srcimage = imagecreatefrompng($server);
imagealphablending($srcimage, false);
imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100);
imagealphablending($dstimage, true);
imagesavealpha($dstimage, true);
$pathImage = 'images_machines/' . $name . '.png';
imagepng($dstimage,$pathImage);
imagedestroy($dstimage);
imagedestroy($srcimage);
return $pathImage;
}
原图:
结果图像:
将您对 imagecopymerge
的调用替换为 imagecopy
(更改函数名称并删除最后一个参数):
//imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100);
imagecopy($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height);
结果:
我有一个透明图像和比图像宽的文本,我试图嵌入透明图像顶部居中的文本并获得最终的透明 png 图像,我已将透明文本生成为容器具有宽度和高度,但当与图像结合时,它带有黑色背景
function createImageServer($name, $state)
{
$path = 'js/FusionCharts/FusionCharts_XT_Website/Charts/Resources/';
$server = $path . 'server' . $state . '.png';
list($width, $height, $type, $attr) = getimagesize($server);
$font_path = 'images_machines/FreeSans.ttf';
$bbox = imagettfbbox(10,0,$font_path,$name);
$diffHeight = 5;
$fontHeight = ($bbox[1] - $bbox[7]) + $diffHeight;
$textWidth = $bbox[2] + $bbox[0];
$dstimage = imagecreatetruecolor($textWidth,$height + $fontHeight);
imagealphablending($dstimage, false);
//Create alpha channel for transparent layer
$col=imagecolorallocatealpha($dstimage,255,255,255,127);
imagefilledrectangle($dstimage,0,0,$textWidth, $height+$fontHeight,$col);
imagealphablending($dstimage,true);
imagefttext($dstimage,10,0,0,$fontHeight-$diffHeight,0,$font_path,$name);
$srcimage = imagecreatefrompng($server);
imagealphablending($srcimage, false);
imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100);
imagealphablending($dstimage, true);
imagesavealpha($dstimage, true);
$pathImage = 'images_machines/' . $name . '.png';
imagepng($dstimage,$pathImage);
imagedestroy($dstimage);
imagedestroy($srcimage);
return $pathImage;
}
原图:
结果图像:
将您对 imagecopymerge
的调用替换为 imagecopy
(更改函数名称并删除最后一个参数):
//imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100);
imagecopy($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height);
结果: