2011-01-09 42 views
6

我一直在使用这个简单的脚本来从文字图像的图像格式的文本:使用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

+0

尝试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

+0

这看起来不像大胆,但更像是一个影子,如果我把它放在彼此的前面,我不认为它们会出现任何不同,因为只有一个imagettftext()。 – Meredith 2011-01-09 02:45:21

回答

0

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

加成

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

从文档:

返回与代表四个点制作的文本的边界框8个元素的数组。点的顺序是左下,右下,右上,左上。无论角度如何,这些点都是相对于文本的,所以“左上角”表示在左上角水平看到文本时。错误时返回FALSE。对于

-1

响应:

解析将不会是一个问题,我没有明确的是如何将我找到了在该示例文本=测试脚本第二wordr X位置?我的意思是我怎么知道第一个单词的结束位置,因此我可以将第二个单词与另一个imagettftext()和一个粗体字体放在一起。 - 梅雷迪思1月9日在'11 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]; 
?> 

是的,我将$字命名为一种精神视觉效果。

$句子[1]是字2