PHP imagettftext部分粗体

Wie*_*ies 4 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);
?>
Run Code Online (Sandbox Code Playgroud)

这导致此图像:

图片

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

真诚地感谢!

小智 6

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

如果您要创建此文本:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);
Run Code Online (Sandbox Code Playgroud)

如果你想要加粗,你应该添加以下内容:

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);
Run Code Online (Sandbox Code Playgroud)


Wie*_*ies 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);
?>
Run Code Online (Sandbox Code Playgroud)

结果如下图:

图像