PHP头像生成器生成的头像文本始终偏移居中问题求助
PHP头像生成器文本居中问题
我开发了一个简单的PHP头像生成器,可根据用户名生成带首字母的头像,但生成的文本始终无法居中,尤其是垂直方向明显偏下。该生成器使用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);
生成的头像文本明显偏下,我希望实现无论文本大小写、字号,都能在125×125的画布中垂直水平完全居中,请问定位代码哪里出错了?
问题原因
你对imageftbbox返回值的逻辑和imagettftext的坐标规则匹配错误:
imageftbbox返回的$top是负数(代表文本基线以上的高度),$bottom是正数(代表基线以下的深度)imagettftext的$y参数指定的是文本基线的y坐标,不是文本中心的y坐标
之前的计算把文本整体高度的一半直接加到画布中心y值上,导致基线被推到中心下方,最终文本整体偏下。
修正方案
调整文本坐标的计算逻辑,核心是基于文本基线的位置来推导垂直居中的坐标:
核心修正代码片段
// Get size of text list($left, $bottom, $right, , , $top) = imageftbbox($font_size, $angle, './Ubuntu-L.ttf', $text); // 计算文本实际宽度和高度 $text_width = $right - $left; $text_height = $bottom - $top; // 水平居中:画布中心x减去文本宽度的一半 $x = $centerX - ($text_width / 2); // 垂直居中:画布中心y加上文本高度的一半,再减去基线到文本底部的距离($bottom) $y = $centerY + ($text_height / 2) - $bottom; // Add text to image imagettftext($im, $font_size, $angle, $x, $y, $white, './Ubuntu-L.ttf', $text);
完整修正代码
$im = imagecreatetruecolor(125, 125); // 添加背景色方便查看居中效果 $bg_color = imagecolorallocate($im, 52, 152, 219); imagefill($im, 0, 0, $bg_color); $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); // 计算文本宽度和高度 $text_width = $right - $left; $text_height = $bottom - $top; // 水平居中计算 $x = $centerX - ($text_width / 2); // 垂直居中计算:核心修正 $y = $centerY + ($text_height / 2) - $bottom; // 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);
补充说明
- 将
$font_size改为数字类型,避免字符串转数字可能出现的异常 - 新增背景色填充,便于直观验证文本居中效果
- 该逻辑适配任意大小写文本和字号,完全基于
imageftbbox返回的实际文本尺寸计算
内容的提问来源于stack exchange,提问作者NaughtySquid
相关产品推荐
相关产品推荐

