PHP imagettftext中心文本左,右,上,下

时间:2019-05-30 18:32:58

标签: php gd imagettftext

我已经创建了一些功能来为用户生成化身,但是中间不能设置一些字母。

这是图像的外观

https://i.stack.imgur.com/6YK89.png

https://i.stack.imgur.com/4IxFQ.png

https://i.stack.imgur.com/iP2U2.png

和代码

    $font = 'arial.ttf';
    $font_size = '120';
    $angle = '0';
    $text = 'M';

    // Create the image
    $im = imagecreatetruecolor(200,240);

    $bg = imagecolorallocate($im, $bgColor[0],$bgColor[1],$bgColor[2]);
    $white = imagecolorallocate($im, 255, 255,255);

    imagefilledrectangle($im, 0, 0, 200, 240, $bg);

    $text_box = imagettfbbox($font_size,$angle,$font,$text);
    $text_width = $text_box[2]-$text_box[0];
    $text_height = $text_box[7]-$text_box[1];

    $x = 100 - ($text_width/2);
    $y = 120 - ($text_height/2);

    // Add the text
    imagettftext($im, $font_size, $angle, $x, $y, $white, $font, $text);

不确定问题出在哪里,也许imagettfbbox无法正确读取字体大小?

此外,我试图在StackOverflow上找到解决方案,但不能

0 个答案:

没有答案