PHP使用imagettftext()和imagefttext()函数更新字距调整问题

时间:2010-04-09 18:48:08

标签: php image gd

我们的开发服务器最近升级到PHP v5.2.13。通过升级,我们发现我们的png图像存在字距调整(字母间距)问题。我们尝试了很多字体,还没有找到解决方案。

我们使用GD库创建图像,并使用字体文件和imagettftext()或imagefttext()函数将文本写入图像。

有没有其他人遇到这个?我误解了某些内容还是应该将其作为错误提交给PHP?有没有我没想过的很酷的解决方法?

这是新老tahoma大胆的一个例子。其他字体(粗体和非粗体)具有相同的问题。有些字母和数字看起来像偏离中心或类似的东西。

糟糕 - 新的PHP

好 - 旧的PHP v5.2.11(单词略有不同,因为这是我们的开发服务器而另一个是实时服务器)

2 个答案:

答案 0 :(得分:3)

“跟踪”是一个类似的术语,用于设置紧密或松散的文本。你可能有更好的运气谷歌搜索,例如this result

答案 1 :(得分:0)

由于我们使用的字体,Kerning对我们不起作用,因此我们必须为AV,AW等特定字母组合进行手动字距调整。

/**
 * This function lets you write a string with your own letter spacing ($t)
 * and kern specific letter combinations like AV
 * 
 * @param type $im An image resource, returned by one of the image creation functions
 * @param type $size The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
 * @param type $angle The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
 * @param type $t Letter Spacing
 * @param type $k Kerning Spacing
 * @param type $x The coordinates given by x and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y define the upper-left corner of the first character. For example, "top left" is 0, 0.
 * @param type $y The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
 * @param type $color The color index. Using the negative of a color index has the effect of turning off antialiasing. See imagecolorallocate().
 * @param type $font The path to the TrueType font you wish to use.
 * @param type $text Text to write/print to the image
 */
function ImageTTFTextWithSpacing($im, $size, $angle, $t, $k, $x, $y, $color, $font, $text) {
    $numchar = strlen($text);
    for($i = 0; $i < $numchar; $i++) {
        # Assign character
        $char[$i] = substr($text, $i, 1);

        //Top is wider than bottom of character
        $up = ['Y','V','W'];
        //Bottom is wider than top of character
        $down = ['A'];
        //From the second letter on
        if( $i > 0 && 
                //check whether we have TOP and BOTTOM type character 
                //next to each other so we need to adjust spacing
                ((in_array($char[$i], $up) && in_array($char[$i-1], $down)) || 
                (in_array($char[$i-1], $up) && in_array($char[$i], $down)) )) {
            $w -= $k;
        }

        # Write character
        imagettftext($im, $size, $angle, ($x + $w + ($i * $t)), $y, $color, $font, $char[$i]);

        # Get width of character
        $width = imagettfbbox($size, $angle, $font, $char[$i]);
        $w = $w + $width[2];
    }
}