1.確認php中GD庫是否開啟
在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前邊的(分號) ';' 即可,一般php是默認開啟的

2.繪畫步驟
- 創建一個畫布(畫板)、畫筆、色彩。
- *開始繪畫(重點,難點)
- 輸出圖像(復制型)
- 銷毀圖像資源(釋放內存)
3.示例
為了方便演示,先了解后兩步
輸出圖像:
1 header("Content-Type: image/jpeg");//設置響應頭信息為一個jpeg的圖片 2 imagejpeg($im);//輸出一個jpeg圖片 3 4 header("Content-Type: image/png");//設置響應頭信息為一個png的圖片 5 imagepng($im);//輸出一個png圖片 6 7 //輸出到指定文件中(另存為) 8 imagepng($im,"**.png");
銷毀圖像(解除占用的資源):
1 bool imagedestroy ( resource $image )//銷毀一圖像
創建一個畫布:
1 //創建一個基於256色的畫布 2 $img=imagecreate(400,400); 3 echo $img;
顯示效果:
1 //新建一個真彩色圖像 2 $img=imagecreatetruecolor(400,400); 3 echo $img;
顯示效果:

其他的方式:
//還有基於某個圖片的畫布 imagecreatefromgif( string filename ) imagecreatefrompng( string filename ) imagecreatefromjpeg( string filename )
真彩和256色畫布的顯示區別-----真彩的會默認填充黑色的畫布
<?php //創建一個基於256色的畫布 $img1=imagecreate(400,400); $img2=imagecreatetruecolor(400,400); // 輸出圖像 header('content-type:image/png'); // imagepng($img1); imagepng($img2); // 銷毀圖像,解除占用的資源 // imagedestroy($img1); imagedestroy($img2); ?>
顯示效果:

*開始繪畫
<?php //分配定義顏色 $red = imagecolorallocate($im,255,0,0); //分配一個顏色 //填充背景 bool imagefill(resource image,int x,int y, int color ); //填充背景 //畫點 bool imagesetpixel(resource image,int x,int y,int color );//畫一個像素點 //畫一個線段的函數 bool imageline ( resource image, int x1, int y1, int x2, int y2, int color ) //畫矩形框(不填充) bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color ) //畫矩形框(填充) bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color ) //繪制多邊形 bool imagepolygon ( resource image, array points, int num_points, int color ) bool imagefilledpolygon ( resource image, array points, int num_points, int color ) //繪制橢圓(正圓) imageellipse ( resource image, int cx, int cy, int w, int h, int color ) imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color ) //繪制圓弧(可填充) imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style ) imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style ) //繪制字串 bool imagestring ( resource image, int font, int x, int y, string s, int col ) bool imagestringup ( resource image, int font, int x, int y, string s, int col ) //繪制字符 imagechar //繪制文本: *array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text ) //當上面的字體出現亂碼時,使用下面的函數轉換編碼 string iconv ( string in_charset, string out_charset, string str ) $name="張三"; $str = iconv("ISO8859-1","UTF-8",$name); $str = iconv("GBK","UTF-8",$name); $str = iconv("GB2312","UTF-8",$name); //圖片的裁剪、合成、縮放 **bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h ) * imagesx — 取得圖像寬度 * imagesy — 取得圖像高度 * array getimagesize ( string $filename [, array &$imageinfo ] ) 取得圖像大小 ?>
綜合代碼示例:
1 <?php 2 // 創建畫布 3 $img=imagecreatetruecolor(1000,1000); 4 // 創建顏色 5 $red=imagecolorallocate($img,255,0,0); 6 $blue=imagecolorallocate($img, 0, 0, 255); 7 $green=imagecolorallocate($img, 0, 255, 0); 8 $gray=imagecolorallocate($img, 200, 200, 200); 9 10 //填充顏色 11 imagefill($img,100,0,$gray); 12 //創建一個點 13 imagesetpixel($img, 20, 20, $red); 14 // 隨機創建1000個點 15 for($i=0;$i<1000;$i++){ 16 imagesetpixel($img, rand(0,1000), rand(0,1000), $red); 17 } 18 //輸出線段 19 imageline($img, 0, 0, 200, 200, $blue); 20 //輸出矩形 21 imagerectangle($img, 200, 200, 400, 400, $blue);//不填充 22 imagefilledrectangle($img, 200, 400, 400, 600, $blue);//填充 23 //繪制多邊形 imagefilledpolygon----填充 24 imagepolygon($img, array(0,0,100,200,300,200), 3, $blue); 25 //繪制橢圓(正圓) 26 imageellipse($img, 600, 600, 200, 200, $blue); 27 imagefilledellipse($img, 800, 600, 200, 200, $blue); 28 // 繪制字符串 29 imagestring($img, 15, 600, 200, "string", $blue);//水平 30 imagestringup($img, 15, 600, 200, "string", $blue);//垂直 31 // 繪制字符 32 imagechar($img, 5, 800, 200, 'H', $blue); 33 imagecharup($img, 5, 800, 200, 'H', $blue); 34 //繪制文本 35 imagettftext($img, 20, 0, 500, 500, $blue, 'bb.ttc', 'this is string!'); 36 /*當上面的字體出現亂碼時,使用下面的函數轉換編碼 37 string iconv ( string in_charset, string out_charset, string str )*/ 38 //imagecopyresampled($img, "dd.jpg", 200, 200, 200, 200, 200, 200, 200, 200); 39 //輸出圖像 40 header('content-type:image/png'); 41 imagepng($img); 42 //銷毀圖像 43 imagedestroy($img); 44 ?>
難重點:
