PHP imagettftext部分粗体

时间:2012-07-13 14:42:41

标签: php gd imagettftext

我正在训练我的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应该是Bold。我认为不可能以简单的方式做到这一点,但它应该是可能的。我可以使用粗体Arial字体添加另一个imagettftext(),但Lorum ipsum文本应该从名称旁边开始,第二行应该在与名称相同的X坐标上继续。就像现在一样。 我也有另一个愿望,但我认为这太难了,因为这个文本可以有任何立场。但如果有人知道如何做到这一点他很棒。这是链接(http://www.google.com)有下划线。我认为我不需要告诉更多信息。

非常感谢!

3 个答案:

答案 0 :(得分:6)

几乎在所有字体中,此解决方案都可以使用。

如果您要创建此文字:

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);

答案 1 :(得分: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

答案 2 :(得分:1)

尝试使用大纲/笔画。如何,请检查此代码。

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

相关问题