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

标签 php image image-processing gd imagettftext

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

<?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 调用脚本

最佳答案

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

添加

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

来自文档:

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

关于php - 使用PHP GD创建具有不同字体的图像形式文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4636984/

相关文章:

javascript - 有没有一种有效的方法可以让按钮自动将文本复制到剪贴板? (JavaScript)

php - 提交后显示消息

php - 有什么方法可以在 PHP 中保持与 mysql 数据库的连接?

c++ - 图像旋转 OpenCV 错误

javascript - 在函数参数声明中完成变量赋值

java - 我旋转 jpeg 照片的 java 代码有什么问题?

c++ - C++ 中的图像模糊。矩阵拷贝的问题

html - 如何在网格框中移动图像?

image-processing - opencv用于原始二进制文件

javascript - 将缓冲区图像数据管道化为 Node.js 中的 Uint8ClampedArray 格式