我有一个简单的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我只想要它,无论文字大小,下案例或上案例如何,它都会在盒子内垂直和水平地居中。

y
参数是位置
字符底部的
baseline
,