我盡量不說大理論,諸如什麼是png,自己查解決.
PHP自4.3版本開始,捆綁了自己的GD2庫,用戶可以自行下載並設定.如果要查看自己的php版本是否支援gd模組(支援JPEG,PNG,WBMP但不再支援GIF),如下方式是一種方法:
if(!function_exists('imagecreate')) {
die('本伺服器不支援GD模組');
}
如果不支援的話,如何設定 ? 下載gd模組的dll檔,修改php.ini,重啟伺服器即可.
以下簡稱PHP作圖為PS.
當您打算 PS的話,應該完成如下以下步驟,這是必經的.
1:建立基本PS物件(我假設為$image),填充背景(預設黑),以後的全部ps操作都是基於這個背景影像的.
2:在$image上作圖.
3:輸出這個影像.
4:銷毀物件,清除使用記憶體.
首先,我們來認識幾個常用的函數,這些函數在php手冊裡面都有詳細介紹,此處大體引用下.
resource imagecreate ( int x_size, int y_size )
imagecreate() 傳回一個圖片標識符,代表了一幅大小為 x_size 和 y_size 的空白圖片。
此函數基本上是同imagetruecolor($width,$height).
int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 傳回一個標識符,代表了由給定的 RGB 成分組成的顏色。 image 參數是 imagecreatetruecolor() 函數的回傳值。 red,green 和 blue 分別是所需的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或十六進位的 0x00 到 0xFF。 imagecolorallocate() 必須被呼叫以建立每一種用在 image 所代表的影像中的顏色。
bool imagefill ( resource image, int x, int y, int color )
imagefill() 在image 圖像的座標x,y(圖像左上角為0, 0)處以color 顏色執行區域填充(即與x, y 點顏色相同且相鄰的點都會被填滿)。
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用color 顏色在圖片image 中從座標x1,y1 到x2,y2(圖片圖像左上角為0, 0)畫一條線段。
bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用col 顏色將字串s 畫到image 所代表的圖像的x,y 座標處(這是字串左上角座標,整個圖像的左上角為0,0)。如果 font 是 1,2,3,4 或 5,則使用內建字體。
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
本函數比較重要,參數較多,此處不再列出它,它主要是寫字到圖像上,和上面的函數類似,但必前者強大.
bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() 從x,y(圖片左上角為0, 0)點開始用color 顏色執行區域填充,直到碰到顏色為border 的邊界為止。 【註:邊界內的所有顏色都會被填滿。如果指定的邊界色和該點顏色相同,則沒有填滿。如果影像中沒有該邊界色,則整個影像都會被填滿。 】
bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在image 所代表的圖片中以cx,cy(圖片為0, 0)為中心畫一個橢圓。 w 和 h 分別指定了橢圓的寬和高。橢圓用 color 顏色填滿。如果成功則傳回 TRUE,失敗則傳回 FALSE。
輸出影像資料:imagepng($image[,$filename])