繁体   English   中英

使用GD PHP创建具有文本和透明背景的图像

[英]Creating an Image with Text and a Transparent Background with GD PHP

一切都可以在这个图像中旋转和居中显示文本,但是imagecolortransparent函数创建的文本周围有一个黑色边框。 如何创建带有文本和透明背景的图像? 这是代码:

header('Content-Type: image/png');

$title = "test text";

$im = imagecreatetruecolor(87, 80);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 87, 80, $black);
imagecolortransparent($im, $black);

$white = imagecolorallocate($im, 255, 255, 255);
$font = "../fonts/arial.ttf";

$bbox = imagettfbbox(12,-40,"../fonts/arial.ttf", $title);
$width = $bbox[2]-$bbox[0];
$height = ceil(($bbox[6]-$bbox[0])/2);

$pos = ceil((87-$width)/2);
$posx = $pos-$height;
$posy = $pos+$height;

imagettftext($im, 12, -45, $posx, $posy, $white, $font, $title);

imagepng($im);
imagedestroy($im);

你想要的是关闭消除锯齿,使用颜色的负数完成此操作。

imagettftext($im, 12, -45, $posx, $posy, -$white, $font, $title);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM