我一直在使用这个简单的脚本来从文字图像的图像格式的文本:使用PHP GD创建具有不同的字体
<?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文本=测试脚本&颜色脚本= 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
尝试http://www.roseindia.net/tutorial/php/phpgd/About-boldtext.html它似乎你想要做什么,它的黑色字体,是在灰色字体的顶部,你可以改变灰色的位置,让它们被放置在你喜欢的位置。该URL将类似于`file.php?text = testing&bold = script&color = 000000`。 – Daniel 2011-01-09 01:09:01
这看起来不像大胆,但更像是一个影子,如果我把它放在彼此的前面,我不认为它们会出现任何不同,因为只有一个imagettftext()。 – Meredith 2011-01-09 02:45:21