【问题标题】:Creating an Image with Text and a Transparent Background with GD PHP使用 GD PHP 创建带有文本和透明背景的图像
【发布时间】:2012-07-28 00:32:30
【问题描述】:

一切都可以使该图像中的文本旋转和居中,但是 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);

【问题讨论】:

    标签: php image gd


    【解决方案1】:

    您想要关闭抗锯齿,使用负片颜色来实现此目的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2011-01-09
      • 1970-01-01
      • 2010-12-02
      • 2014-04-30
      • 2010-10-17
      相关资源
      最近更新 更多