首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP imagettftext部分加粗

PHP imagettftext部分加粗
EN

Stack Overflow用户
提问于 2012-07-13 22:42:41
回答 4查看 13K关注 0票数 5

我正在训练我的PHP技能来生成图像。我只需要一件不起作用的东西。我不认为这在我想要的方式上是可能的,但应该有另一种方式。我已经拥有的代码是:

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

特意谢谢你!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-07-14 01:43:44

我自己找到了一个解决方案,让名字用粗体显示。我不想解释所有的事情,因为我就是问这个问题的人,所以我只是给出了带有结果图像的新代码。明天我会试着给这个链接加上下划线。如果有人知道答案,我会接受这个答案。请注意,我对代码做了一些修改,这对于使代码正常工作并不是必需的。

代码:

代码语言:javascript
复制
<?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);
?>

结果如下图所示:

票数 4
EN

Stack Overflow用户

发布于 2016-02-02 05:32:30

几乎在每种字体中,这个解决方案都会起作用。

如果您要创建此文本:

代码语言:javascript
复制
imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);

如果你想加粗,你应该添加这些:

代码语言:javascript
复制
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);
票数 7
EN

Stack Overflow用户

发布于 2017-10-10 16:31:43

尝试使用轮廓/笔划。如何,请查看此代码。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11472760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档