很多人不了解 PHP 可以產生非HTML的資料.這是對產生影像圖非常有用的.可以從 database 產生一個簡單的廣告橫圖或更簡單只產生一個圖形按鈕 .
我用 TTF 字型在以下的范例中 我通常取名作 'button.php3':
####################################################### -----button.php3------ <? Header("Content-type: image/gif"); if(!isset($s)) $s=11; $size = imagettfbbox($s,0,"fonts/TIMES.TTF",$text); $dx = abs($size[2]-$size[0]); $dy = abs($size[5]-$size[3]); $xpad=9; $ypad=9; $im = imagecreate($dx+$xpad,$dy+$ypad); $blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF); $black = ImageColorAllocate($im, 0,0,0); $white = ImageColorAllocate($im, 255,255,255); ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black); ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white); ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/TIMES.TTF", $text); ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/TIMES.TTF", $text); ImageGif($im); ImageDestroy($im); ?> ####################################################### 很重要一點是你不能在這檔案中放任何HTML tags.也不能有空白行在 <?和 ?> tag 之前或之後. 如果你用這段Script後看到一個不完整的影像, 表示你可能在PHP標簽以外誤打了字元.
以上的 script 可以由此語法在網頁中叫出來: <IMG SRC="button.php3?s=36&text=PHP+is+Cool">
####################################################### ----test.php-----
<html> <head> <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gb2312"> <title>New Page 1</title> </head>
<body>
<IMG SRC="button.php3?s=36&text=PHP+is+Cool">
</body> </html> #######################################################
test.php結果會像這樣: .
's' 參數是設定字型大小 .
這是當 s=18 時:
注I: 字型路徑 "/fonts/TIMES.TTF" 可由windows/fonts目錄下取得 TIMS.TTF 字型檔 Copy 至你網站的目錄 fonts下即可測試 至於中文的表現 尚待各位網友提供心得
注意 我先畫了一個黑色方塊區再用白色位移產生 3D 效果.也陬L法在淺色背景中看出來 但你可以把背景色改為深色看看此效果. 字型也做了同樣效果表現立體感.
你要先確定你的安裝 PHP 時有設定支援 GD 和 TTF. 可參考 PHP FAQ . 我建議可以 copy libgd.a 到 /usr/local/lib 和 gd*.h 相關檔案到 /usr/local/include 然後 'make install' for FreeTTF library.
可以在這http://rover.wiesbaden.netsurf.de/~kikita/ 找到釵httf 字型哦!
注:
以下的原始碼改進了上面的弁?可多行文字顯示: ####################################################### --------------button.php----------------- <?
Header("Content-type: image/jpeg"); if(!isset($bgred)) $bgred=0; if(!isset($bggreen)) $bggreen=51; if(!isset($bgblue)) $bgblue=153; if(!isset($chred)) $chred=255; if(!isset($chgreen)) $chgreen=255; if(!isset($chblue)) $chblue=255; if(!isset($shadow)) $shadow="yes"; if(!isset($wrappos)) $wrappos=20; if(!isset($crop)) $crop=2.2; if(!isset($jpegquality)) $jpegquality=80; if(!isset($s)) $s=11; $savetext=$text; $text=wordwrap($text,$wrappos," ",0); if (!isset($font)) $fontname="/www/ttfonts/arialbd.ttf"; else $fontname="/www/ttfonts/".$font.".ttf"; $size = imagettfbbox($s,0,$fontname,$text); $dx = abs($size[2]-$size[0]); $dy = abs($size[5]-$size[3]); $upper=abs($size[5]); $under=$size[1]; $th=$upper-$under; $xpad=9; if (substr_count($text,chr(13))>=1) { $mult=(substr_count($text,chr(13))); $ypad=($mult*$crop*$s)+$s; } else $ypad=($crop-2)*$s; $im = imagecreate($dx+$xpad,$th+$ypad); $color = ImageColorAllocate($im, $bgred,$bggreen,$bgblue); $black = ImageColorAllocate($im, 0,0,0); $fontcolor = ImageColorAllocate($im, $chred,$chgreen,$chblue); ImageRectangle($im,0,0,$dx+$xpad-1,$th+$ypad-1,$black); ImageRectangle($im,0,0,$dx+$xpad,$th+$ypad,$white); if ($shadow=="yes") ImageTTFText($im, $s, 0, (int)($xpad/2)-2+1, $th+2+(int)($ypad/2)-3, $black, $fontname, $text); ImageTTFText($im, $s, 0, (int)($xpad/2)-2, $th+2+(int)($ypad/2)-1-3, $fontcolor, $fontname, $text); Imagejpeg($im,"",$jpegquality); ImageDestroy($im);
?> ####################################################### 這可以下面這個 form 來產生: ####################################################### ----------test.php-------------------- <html>
<head> <title>New Page 1</title> </head>
<body>
<form method="POST" action="button.php"> <p>文字<input type="text" name="text" size="60"></p> <p>大小<input type="text" name="s" size="6" value="14"></p> <p>斷句的位置(wrap break position) <input type="text" name="wrappos" size="3" value="20"></p> <p>背景顏色</p> <p>紅色<input type="text" name="bgred" size="6" value="0"> 綠色<input type="text" name="bggreen" size="8" value="51"> 藍色<input type="text" name="bgblue" size="7" value="153"></p> <p>字元顏色</p> <p>紅色 <input type="text" name="chred" size="6" value="255"> 綠色 <input type="text" name="chgreen" size="8" value="255"> 藍色 <input type="text" name="chblue" size="7" value="255"></p> <p>字型 <input type="text" name="font" size="20" value="arialbd"></p> <p>陰影 <input type="radio" value="yes" checked name="shadow">是 <input type="radio" name="shadow" value="no">否</p> <p>Crop size <input type="text" name="crop" size="20" value="2.2"></p> <p>Jpeg 品質 (0-100) <input type="text" name="jpegquality" size="20" value="80"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> </body> </html> #######################################################
或是直接像上例一樣呼叫:
####################################################### ----test.php-----
<html> <head> <title>New Page 1</title> </head>
<body>
<IMG SRC="button.php?s=36&text=PHP+is+Cool">
</body> </html>
|