gpt4 book ai didi

PHP imagettftext 部分加粗

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:17 24 4
gpt4 key购买 nike

我正在训练我的 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 ) 有下划线。我认为我不需要告诉更多信息。

特意感谢!

最佳答案

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

如果您要创建此文本:

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

关于PHP imagettftext 部分加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com