PHP imagettftext 部分加粗

5

我正在训练PHP技能以生成图片。我只需要一件事情,但它却不起作用。我认为按照我想要的方式可能不可能实现,但应该还有另一种方法。我已经拥有的代码如下:

<?php
$textSize = 10;
$text = addslashes("Wietse: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique lectus imperdiet nec. Pellentesque et.");
$maxWidth = 448;

//Create image
$im = imagecreate(468, 60);

// Black background and White text
$bg = imagecolorallocate($im, 0, 0, 0);
$textColor = imagecolorallocate($im, 255, 255, 255);

// Split in multiple lines
$words = explode(' ', $text);
$lines = array();
$line = "";
foreach ($words as $word) {
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word);
    $width = $box[4] - $box[0];
    if($width > $maxWidth) {
        $line = trim($line);
        $line .= "\n";
    }
    $line .= $word . ' ';
}

// Write the text in the image
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $line); // write text to image

// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

这将导致出现这张图片:

Image

它能够正常工作,但我的名字Wietse应该是粗体字。我不认为有简单的方法可以实现这一点,但应该是可能的。我可以添加另一个带粗体Arial字体的imagettftext(),但Lorum ipsum文本应该从名称旁边开始,并且第二行应该在相同的X坐标上继续名称。就像现在这样。我还有一个愿望,但我认为这太难了,因为这个文本可以有任何位置。但如果有人知道如何做到这一点,他就很棒了。这就是链接(http://www.google.com)具有下划线。我不认为我需要再提供更多信息。谢谢!
4个回答

9
几乎所有字体都可以使用这种解决方案。
如果您正在创建此文本:
imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);

如果您想加粗字体,您应该添加以下内容:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x, $y+1, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x, $y+2, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x+1, $y, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x+2, $y, $color, $font_path, $text2);

4
我已经自己找到了一种方法,可以使名称加粗。我不想解释所有的细节,因为我是提问者,所以我只给出新代码和结果图片。明天我会尝试给链接添加下划线。如果有人知道答案,我会接受那个答案。请注意,我已经对代码进行了一些不必要的更改,这些更改并不影响代码的工作。
代码如下:
<?php
$username = 'WietsedeVries';
$textSize = 10;
$text = addslashes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique http://www.google.com pellentesque et.");
$maxWidth = 448;

//Create image
$im = imagecreate(468, 60);

// Black background and White text
$bg = imagecolorallocate($im, 0, 0, 0);
$textColor = imagecolorallocate($im, 255, 255, 255);

// Write Bold Username in image
$usernameTotal = '@' . $username . ':';
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arialbd.ttf', $usernameTotal);

// Create white space with the width of the username
$usernameBox = imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal);
$usernameWidth = $usernameBox[4] - $usernameBox[0];
$whiteSpace = '';
$whiteSpaceWidth = 0;
while($whiteSpaceWidth < $usernameWidth) {
    $whiteSpace .= ' ';
    $whiteSpaceBox = imagettfbbox($textSize, 0, './arial.ttf', $whiteSpace);
    $whiteSpaceWidth = $whiteSpaceBox[4] - $whiteSpaceBox[0];
}

// Split in multiple lines
$words = explode(' ', $text);
array_unshift($words, $whiteSpace);
$lines = array();
$line = "";
foreach ($words as $word) {
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word);
    $width = $box[4] - $box[0];
    if($width > $maxWidth) {
        $line = trim($line);
        $line .= "\n";
    }
    $line .= $word . ' ';
}

// Write the text in the image
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $whiteSpace . ' ' . $line);

// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

这会导致出现以下图片:

Image


1
如果其他人遇到了这个问题,解决方案是使用不同的.ttf文件。由于斜体/粗体/删除线/下划线实际上是完全不同的字体,因此很容易找到所需字体的粗体/斜体版本。看看OP如何使用imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal);来设置粗体,以及imagettfbbox($textSize, 0, './arial.ttf', $line . $word);来设置其余文本?2个不同的.ttf文件。 - IsThisJavascript

2
尝试使用轮廓/描边。如何实现,请查看以下代码。
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}

$font_color = imagecolorallocate($im, 255, 255, 255);
$stroke_color = imagecolorallocate($im, 0, 0, 0);
imagettfstroketext($im, 60, 10, 300, 130, $font_color, $stroke_color, "wqy-microhei.ttc", "简体繁體", 2);

结果: http://wmh.github.io/hunbook/_static/examples/gd-imagettftext/003.jpg

前往:http://wmh.github.io/hunbook/examples/gd-imagettftext.html


0
为获得最佳可视效果,请使用-$textColor; 这将显示更好的文本视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接