【发布时间】:2015-04-24 16:16:54
【问题描述】:
我已经创建了一个脚本来使用 PHP 生成头像并且一切正常,但是当 $data1['username'] 中有字母 j 时,该字母会删除前一个字母的一部分。注意:我使用 calibri 粗斜体 (Downloaded here)
这是我的代码:
$image = imagecreatefrompng("avatar.png");
$couleur = imagecolorallocate($image, 0, 0, 0);
$largeur_source = imagesx($image);
$fontfile = 'calibri.ttf';
$angle = 0;
$police = 18;
$text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']);
$text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']);
$text_width = (($text_size[2] + $text_size[4]) / 2) - (($text_size[0] + $text_size[6]) / 2);
$text_width2 = (($text_size2[2] + $text_size2[4]) / 2) - (($text_size2[0] + $text_size2[6]) / 2);
$x = ($largeur_source - $text_width)/2;
$x2 = (176 - $text_width2)/2 + 74;
//imagestring($image, $police, $x, $y, $texte_a_ecrire, $couleur);
imagettftext($image, $police, $angle, $x2, 175, $couleur, $fontfile, $data1['username']);
imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']);
imagepng($image);
当$data1['username'] = 'Paj46 时,字符串看起来像:
【问题讨论】:
-
最好逐个字母计算宽度。
P的一部分也被剪掉了。 -
@moskito-x 很抱歉,我不太明白你的意思。你可以给我一个例子吗 ?谢谢
-
为什么图片中的字体是倾斜的,即使你使用的是普通的 Calibri 和 0 的角度?
-
这也是没用的假设 $text_size2[2] => 198 // 右下 X 坐标 $text_size2[4] => 198 // 右上 X 坐标。您要添加相同的值,然后添加 diff 2 。意味着你又得到了 198。
($text_size2[2] + $text_size2[4]) /2 -
使用
$x2 = ceil((176 - $text_size2[2]) / 2);如果不够,则增加176
标签: php png gd truetype imagettftext