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

发布于 2024-08-28 11:28:16 字数 375 浏览 3 评论 0原文

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

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

还有其他人遇到过这个吗?我是否误解了什么,或者应该将其作为错误提交给 PHP?有什么我还没有想到的很酷的解决方法吗?

这是新旧 tahoma 粗体的示例。其他字体(粗体和非粗体)也有同样的问题。有些字母和数字看起来像是偏离中心或类似的东西。

坏 - 新 PHP

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

Our dev server was recently upgraded to PHP v5.2.13. With that upgrade we have found that our png images are having kerning (letter spacing) problems. We've tried numerous fonts and haven't found a solution yet.

We are creating images using the GD library and writing text to the images using font files and the imagettftext() or imagefttext() functions.

Has anyone else run into this? Am I misunderstanding something or should this be submitted to PHP as a bug? Are there any cool workarounds I haven't thought of yet?

Here's an example of the new and old tahoma bold. Other fonts (bold and non-bold) have the same problem. Some letters and numbers seem like they're off-center or something like that.

Bad - new PHP

Good - old PHP v5.2.11 (the words are slightly different because this is our dev server and the other one is the live server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-09-04 11:28:16

“跟踪”是一个类似的术语,表示文本设置的松紧程度。您可能会更好地通过谷歌搜索,例如 此结果

"Tracking" is a similar term for how tight or loose text is set. You might have better luck googling for that, such as this result.

够运 2024-09-04 11:28:16

由于我们使用的字体,字距调整对我们不起作用,因此我们必须对特定字母组合(如 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];
    }
}

Kerning didn't work for us thanks to the font we used, so we had to put in manual kerning for specific letter combinations like AV, AW ...etc.

/**
 * 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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文