带有文本的形成图像始终偏心

问题描述 投票:0回答:1

我有一个简单的PHP Avatar发电机,该发电机以某人的用户名为单位,并在其上添加缩写。这效果很好,但是以某种方式文字总是离心的,我不知道必须始终将文本正确居中。 this使用

ubuntu

字体。 $im = imagecreatetruecolor(125, 125); $str = 'dpoint'; if (str_word_count($str) > 1) { $words = explode(' ', $str); $text = $words[0][0]. $words[1][0]; } else { $text = substr($str, 0, 2); } $font_size = '50'; $white = imagecolorallocate($im, 255, 255, 255); $angle = 0; // Get image dimensions $width = imagesx($im); $height = imagesy($im); // Get center coordinates of image $centerX = $width / 2; $centerY = $height / 2; // Get size of text list($left, $bottom, $right, , , $top) = imageftbbox($font_size, $angle, './Ubuntu-L.ttf', $text); // Determine offset of text $left_offset = ($right - $left) / 2; $top_offset = ($bottom - $top) / 2; // Generate coordinates $x = $centerX - $left_offset; $y = $centerY + $top_offset; // Add text to image imagettftext($im, $font_size, $angle, $x, $y, $white, './Ubuntu-L.ttf', $text); // Set the content type header header('Content-Type: image/jpeg'); // Skip the file parameter using NULL, then set the quality imagejpeg($im, NULL, 100); // Free up memory imagedestroy($im); 例如,例如这样的结局:


您可以看到它应该较低。我在这里做错了什么?
comeyly我只想要它,无论文字大小,下案例或上案例如何,它都会在盒子内垂直和水平地居中。
    

enter image description herey

参数是位置 字符底部的

baseline

php fonts gd
1个回答
0
投票

y-属。这设置了字体基线的位置,而不是字符的底部。

请从Wikipedia页面(红线)中看到插图
没有干净的解决此问题的干净方法,但这是解决问题的方法:
检查字符串是否包含一个已知字符,带有descender

如果这样做,通过比较

imagettftext()

a的高度来计算下降的高度,然后从p

  • 示例:

    $y
  • 输出:

        

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.