使用PHP GD创建不同字体的图像形式文本

发布于 2024-10-10 21:55:16 字数 2571 浏览 2 评论 0原文

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

<?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 script&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+script&color=000000 调用脚本

I have been using this simple script to generate images from text:

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

?>

I call the script with file.php?text=testing script&color=000000

Now I'd like to know how could I generate text with normal and bold fonts mixed in the same image, something like file.php?text=testing <b>script</b>&color=000000


Thanks to dqhendricks for helping me figure this out.

Here's a quick script I wrote, still needs lot of improvements but for the basic functionality it seems to be working fine:

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

?>

Note: This will only work for "bolding" single words separated by spaces and not for bolding part of a word.

Call the script with file.php?text=testing+<b>script</b>&color=000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

默嘫て 2024-10-17 21:55:16

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

添加

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

来自文档:

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

you will need to load an arial-bold font file, and do two separate imagettftext() calls, one with each font you want to use. as for parsing the string to find out which parts you would like to be bold, and where you should print each section of text, this sounds like it will become very complicated code. what are you even using this for? there may be better solutions for accomplishing the same thing.

Addition

Use the return value from the imagettftext() function to determine where the next text print should start.

From documentation:

Returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is lower left, lower right, upper right, upper left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontally. Returns FALSE on error.

单调的奢华 2024-10-17 21:55:16

响应:

解析不会有问题,我不清楚的是如何找到示例中第二个单词的 X 位置?text=testing script。我的意思是我如何知道第一个单词在哪里结束,以便我可以将第二个单词与另一个 imagettftext() 和粗体字体一起放置在那里。 – Meredith Jan 9 '11 at 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

Response for:

The parsing wouldn't be a problem, what I don't have clear is how I would find the X position for the second wordr in the example ?text=testing script. I mean how do I know where the first word ends so I can place the second one there with another imagettftext() and a bold font. – Meredith Jan 9 '11 at 2:43


funny how someone took the time to say "see edits for the answer to that" when they coulda just said to explode the string at the spaces, then each word is in an array..

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

}
?>

Your X position would be simply $s for your logic. To get the second word you can do this:

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

Yes, i named the $words just for a mental visual effect.

$sentence[1] would be word 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文