6

我一直在使用这个简单的脚本从文本生成图像:

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$text = urldecode($_GET['text']);

$font = 'arial.ttf';

$im = imagecreatetruecolor(400, 30);

$bg_color = imagecolorallocate($im, 255, 255, 255);

$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);

imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);

imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

我用 file.php?text=testing script&color=000000 调用脚本

现在我想知道如何在同一图像中生成混合了普通字体和粗体字体的文本,例如file.php?text=testing <b>script</b>&color=000000


感谢 dqhendricks 帮助我解决这个问题。

这是我写的一个快速脚本,仍然需要很多改进,但对于基本功能,它似乎工作正常:

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$im = imagecreatetruecolor(400, 30);

$white = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 399, 29, $white);

$tmp = $_GET['text'];

$words = explode(" ", $tmp);

$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD

$addSpace = 0;

foreach($words as $word)
{
    if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE

    if(stristr($word, "<b>"))
    {
        $font = 'arialbd.ttf'; // BOLD FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
    }
    else
    {
        $font = 'arial.ttf'; // NORMAL FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
    }

    $addSpace = 1;
}

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

注意:这仅适用于以空格分隔的“加粗”单个单词,不适用于加粗单词的一部分。

调用脚本file.php?text=testing+<b>script</b>&color=000000

4

2 回答 2

0

您将需要加载一个 arial-bold 字体文件,并执行两个单独的 imagettftext() 调用,一个用于您要使用的每种字体。至于解析字符串以找出您想要加粗的部分,以及您应该在哪里打印每个文本部分,这听起来会变成非常复杂的代码。你甚至用这个做什么?可能有更好的解决方案来完成同样的事情。

添加

使用 imagettftext() 函数的返回值来确定下一个文本打印应该从哪里开始。

从文档:

返回一个包含 8 个元素的数组,这些元素代表构成文本边界框的四个点。点的顺序是左下、右下、右上、左上。无论角度如何,这些点都是相对于文本的,因此“左上角”表示水平查看文本时位于左上角。出错时返回 FALSE。

于 2011-01-09T01:32:52.883 回答
-2

回应:

解析不会有问题,我不清楚的是如何在示例 ?text=testing script中找到第二个 wordr 的 X 位置。我的意思是我怎么知道第一个单词在哪里结束,所以我可以将第二个单词与另一个 imagettftext() 和粗体字体一起放在那里。– 梅雷迪思 2011 年 1 月 9 日 2:43


有趣的是,当他们可以说在空格处爆炸字符串时,有人花时间说“查看答案的编辑”,然后每个单词都在一个数组中..

<?PHP
$sentence = "I LOVE giving retarded answers that don't amount to jack!";
$sentence = explode(" ",$sentence );

for($s=0;$s<count($sentence);$s++){

#THERES YOUR INDIVIDUAL WORDS!
$word = $sentence[$s];

}
?>

对于您的逻辑,您的 X 位置将只是 $s 。要获得第二个单词,您可以这样做:

<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>

是的,我命名 $words 只是为了一种心理视觉效果。

$sentence[1] 将是单词 2

于 2013-03-25T15:53:13.523 回答