我已经创建了一个使用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
字符串看起来像:
答案 0 :(得分:1)
看起来你的字体没有字距对表
在这里查看Kerning
x/2
直接round up
.. ceil()
$largeur_source
中拥有图片的宽度,请使用它!result[2]
中的result[4]
或imagettfbbox()
进行计算。 (值相同)<?php
$image = imagecreatefrompng("avatar.png");
$couleur = imagecolorallocate($image, 0, 0, 0);
$largeur_source = imagesx($image);
$fontfile = '59250___.TTF';
$angle = 0;
$police = 24;
$text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de' );
$text_size2 = imagettfbbox($police, $angle, $fontfile, 'username');
$x = ceil(($largeur_source - $text_size[2]) / 2);
$x2 = ceil(($largeur_source - $text_size2[2]) / 2);
imagettftext($image, $police, $angle, $x2, 80, $couleur, $fontfile, 'username');
imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de ');
imagepng($image);