我正在训练我的PHP技能来生成图像。我只需要一件不起作用的东西。我不认为在我想要的方式上是可能的,但应该有另一种方式。我已经有代码是这样的:PHP imagettftext部分大胆
<?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);
?>
导致这个形象:
它工作正常,但我的名字,Wietse,应该大胆。我认为不可能以简单的方式做到这一点,但它应该是可能的。我可以添加另一个带有粗体Arial字体的imagettftext()
,但是Lorum ipsum文本应该在名称旁边开始,第二行应该继续与名称相同的X坐标。就像现在一样。 我也有另一个愿望,但我认为这太难了,因为这个文本可以有任何位置。但是如果有人知道如何做到这一点,那他很棒。这是链接(http://www.google.com)有一个下划线。我不认为我需要告知更多信息。
谢谢你的目的!
的代码不给上述输出 – h2O 2013-10-08 14:33:50
如果任何人遇到这种解决方案;解决方案是使用不同的'.ttf'文件。由于斜体/粗体/穿透/下划线实际上是完全不同的字体,所以很容易找到所需字体的粗体/斜体版本。看看OP如何使用'imagettfbbox($ textSize,0,'./arialbd.ttf',$ usernameTotal);'为粗体和'imagettfbbox($ textSize,0,'./arial.ttf',$ line。$ word );'为文本的其余部分? 2个不同的'.ttf'文件 – IsThisJavascript 2018-02-09 14:34:28