使用PHPGD创建具有不同字体的图像表单文本
我一直在使用这个简单的脚本从文本生成图像:使用PHPGD创建具有不同字体的图像表单文本,php,image,image-processing,gd,imagettftext,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)
<?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调用脚本
您需要加载一个arial粗体字体文件,并执行两个单独的imagettftext()调用,每个调用一个要使用的字体。至于解析字符串以找出哪些部分需要加粗,以及应该在哪里打印文本的每一部分,这听起来好像会变成非常复杂的代码。你用这个干什么?可能有更好的解决方案来完成同样的事情
添加
使用imagettftext()函数的返回值确定下一次文本打印应该从何处开始
从文件:
返回一个数组,该数组包含8个元素,表示构成文本边界框的四个点。点的顺序为左下、右下、右上、左上。无论角度如何,这些点都是相对于文本的,因此当您水平查看文本时,“左上角”表示位于左上角。错误时返回FALSE。响应:
解析不会是一个问题,我不清楚的是如何找到示例中第二个单词r的X位置?text=测试脚本。我的意思是,如何知道第一个单词的结尾,以便将第二个单词与另一个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];
}
?>
试试看,它似乎可以做你想做的,它是灰色字体之上的黑色字体,你可以改变灰色字体的位置,让它们按照你喜欢的方式放置。URL将类似于file.php?text=testing&bold=script&color=000000
。这看起来不像粗体,但更像一个阴影,如果我将它放在彼此的前面,我认为它们不会出现任何不同,因为只有一个imagettftext()。解析不会有问题,我不清楚的是如何找到示例中第二个单词R的X位置?text=测试脚本。我的意思是,我如何知道第一个单词的结尾,以便将第二个单词与另一个imagettftext()和粗体字体放在一起。
<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>