PHP imagettftext部分加粗 [英] PHP imagettftext partially bold

查看:1206
本文介绍了PHP imagettftext部分加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在训练我的PHP技能来生成图像.我只需要一件事是行不通的.我认为我想要的方式是不可能的,但应该有另一种方式.我已经拥有的代码是这样:

I'm training my PHP skills to generate images. I need only one thing which doesn't work. I don't think it's possible on the way I want, but there should be another way. The code I already have is this:

<?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 )带有下划线.我认为我不需要告诉更多信息.

It works fine, but my name, Wietse, should be Bold. I don't think it's possible to do this on a simple way, but it should be possible. I can add another imagettftext() with a Bold Arial font, but the Lorum ipsum text should start next to the name and the second line should continue on the same X coordinates as the name. Like it is now. I also have another wish, but I think it's too difficult because this text can have any position. But if someone knows how to do this he's great. This is that the link (http://www.google.com) has an underline. I don't think I need to tell further information.

特意感谢!

推荐答案

我自己找到了一个解决方案,以粗体显示该名称.我不想解释所有事情,因为我是问这个问题的人,所以我只给出新的代码以及得到的图像.明天我将尝试在链接上添加下划线.如果有人知道答案,我会接受的.请注意,我对代码进行了一些更改,这对于使代码正常工作是不必要的.

I've found a solution myself to get the name in bold. I have no desire to explain everything because I am the one who asked this question, so I just give the new code with the resulting image. Tomorrow I'll try to give an underline to the link. If anyone knows the answer to that I'll accept that answer. Note that I've changed the code a little more which isn't necessary to get the code working.

代码:

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

这将产生此图像:

这篇关于PHP imagettftext部分加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆