使用imagettftext在文本下划线 [英] Underline text using imagettftext

查看:166
本文介绍了使用imagettftext在文本下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何在图像中的所有文本下划线?

代码:

function createImage($text)
{
    $text .= "\n";
    $text = wordwrap($text, 40, "\n");
    $newlines = substr_count($text, "\n");
    if($newlines == 0)
    {
        $height = 30;
    }
    else
    {
        $height = 30*$newlines-$newlines*7;
    }
    putenv('GDFONTPATH=' . realpath('.'));
    header('Content-Type: image/png');

    $im = imagecreatetruecolor(315, $height);
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    $purple = imagecolorallocate($im, 97, 26, 139);
    imagefilledrectangle($im, 0, 0, 399, $height, $white);
    $font = 'arialbd.ttf';

    imagettftext($im, 11, 0, 10, 20, $purple, $font, $text);
    imagepng($im);
    imagedestroy($im);
}

createImage("Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow ");

结果 http://i.imgur.com/Jdr7HPy.png

我希望所有文本都带有下划线,我找到了一些解决方案,但它们只是从左到右,而不是取决于单词.

RESULT http://i.imgur.com/Jdr7HPy.png

I want all the text to be underlined, I've found some solutions but they're only from left to right, not depending on words.

推荐答案

使用结合字符U + 0332的Unicode下划线.

Use the Unicode underline combining character U+0332.

您将需要循环或巧妙地合并数组以修改文本,

You will need a loop to or clever array merge to modify the text,

$e = explode(' ', $text);

for($i=0;$i<count($e);$i++) {
  $e[$i] = implode('&#x0332;', str_split($e[$i]));
}

$text = implode(' ', $e);

这篇关于使用imagettftext在文本下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆