【问题标题】:Letter 'j' bugged with imagettftext function字母“j”被 imagettftext 函数窃听
【发布时间】: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


【解决方案1】:

看起来你的字体没有字距调整表

在这里寻找Kerning

  • 你必须计算整数不要使用x/2直接round up..ceil()
  • 如果您有$largeur_source 中的图像宽度,请使用它!
  • 使用result[2]result[4] from 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); 

【讨论】:

  • 我已经采纳了您的建议,谢谢。为了更轻松地做,我更改了 Calibri Bold 的字体,它的工作。
  • 抱歉,我不能接受这个答案,因为尽管这对我有帮助,但我的问题并没有真正解决,因为我更改了字体。
  • @louis67 :在第一句话中,我指出了。字体有问题!!! 看起来你的字体没有字距调整表。大多数功能不适用于无效资源。
猜你喜欢
  • 2015-01-13
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2011-06-10
相关资源
最近更新 更多