用透明背景的GD绘制文本


Draw text with GD with transparent background

我有这样的代码来在新创建的图像上写一些文本:

class ImageCreator
{
    public function Draw($string)
    {
        $font = "lg.ttf";
        $txtsize = imagettfbbox(20, 0, $font, $string);
        $image = imagecreatetruecolor($txtsize[2], $txtsize[4]);
        imagealphablending($image, false);
        $c = imagecolorallocatealpha($image,0,0,0,127);
        imagefilledrectangle($image,0,0,$txtsize[2],$txtsize[4], $c);
        imagealphablending($image, true);
        $c = ImageCreator::hexcol2dec("#FFFFFF");
        $white = imagecolorallocate($image, $c[0], $c[1], $c[2]);
        $c = ImageCreator::hexcol2dec("#000000");
        $black = imagecolorallocate($image, $c[0], $c[1], $c[2]);
        $c = ImageCreator::hexcol2dec("#044D8F");
        $tcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]);
        imagettftext($image, 20, 0, 0, 0, $black, $font, $string);
        imagettftext($image, 20, 0, 1, 0, $tcolor, $font, $string);
        imagettftext($image, 20, 0, 2, 0, $white, $font, $string);
        imagealphablending($image,false);
        imagesavealpha($image,true);
        ob_start();
        imagepng($image);
        $imgData = ob_get_contents();
        ob_end_clean();
        return $imgData;
    }
    public function hexcol2dec($hexColor)
    {
        $R=0;
        $G=0;
        $B=0;
        for($i = 0; $i < strlen($hexColor);$i++)
        {
            if($hexColor[$i] == "#")
            {
            }
            else if($hexColor[$i] == 'A' || $hexColor[$i] == 'a')
            {
                $dec[$i] = 10;
            }
            else if($hexColor[$i] == 'B' || $hexColor[$i] == 'b')
            {
                $dec[$i] = 11;
            }
            else if($hexColor[$i] == 'C' || $hexColor[$i] == 'c')
            {
                $dec[$i] = 12;
            }
            else if($hexColor[$i] == 'D' || $hexColor[$i] == 'd')
            {
                $dec[$i] = 13;
            }
            else if($hexColor[$i] == 'E' || $hexColor[$i] == 'e')
            {
                $dec[$i] = 14;
            }
            else if($hexColor[$i] == 'F' || $hexColor[$i] == 'f')
            {
                $dec[$i] = 15;
            }
            else
            {
                $dec[$i] = $hexColor[$i];
            }
        }
        if($hexColor[0] == "#")
        {
            $R = 16*$dec[1]+$dec[2];
            $G = 16*$dec[3]+$dec[4];
            $B = 16*$dec[5]+$dec[6];
        }
        else
        {
            $R = 16*$dec[0]+$dec[1];
            $G = 16*$dec[2]+$dec[3];
            $B = 16*$dec[4]+$dec[5];
        }
        return array ($R, $G, $B);
    }

}

我得到的只是透明的背景,没有显示任何文本。我是PHP GD的新手,我不明白为什么不写文本。请帮我算出

谢谢。

imagettfbbox返回的坐标是相对于基点的,因此可以是负数。

因此当角度为0时,上Y和左X为负,您需要从下Y和右X中减去它们来获得方框大小。

$width = $txtsize[2] - $txtsize[0];
$height = $txtsize[1] - $txtsize[5]; 
$image = imagecreatetruecolor($width, $height);

然后你必须使用负坐标的绝对值作为绘制文本的基点坐标:

imagettftext($image, 20, 0, -$txtsize[0], -$txtsize[5], $black, $font, $string);

是否启用了警告?PHP查找字体文件时可能会出现问题,因为我注意到它不是一个完整的路径。

您的服务器是否安装了FreeType库?使用phpinfo()进行验证。http://www.freetype.org/

你可以使用这个沙盒来测试你的坐标是否正确:http://ruquay.com/sandbox/imagettf/.

请记住,基点是用于使用imagettftext绘制文本的x,y坐标。一件有用的事情是拿一根像。。。

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

并使用"aboveBasepoint"值作为字体的高度。现在,您可以绘制线条,并使用"字体的高度*前导"作为文本行之间的距离,其中前导是一个数字,例如1.45(表示45%的前导)。

查看此函数的PHP手册条目底部的注释,了解许多有用的函数:http://php.net/manual/en/function.imagettfbbox.php