1 <?php 2 //1. 使用GD庫繪製圖形 3 /* 使用GD庫畫圖分爲四個基礎步驟 4 * 建立圖像->繪製圖像->輸出圖像->釋放資源 5 */ 6 /* 建立圖像 7 * imagecreate(int sx, int sy) 基於調色板的圖像(256色) 8 * imagecreatetruecolor(int sx, int sy) 基於真彩色圖像,不能用於gif圖像 9 */ 10 $image = imagecreatetruecolor(800, 800); 11 /* 設置顏色 12 * imagecolorallocate(resource image, int red, int green, int blue) 13 */ 14 $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 15 $col_gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); 16 $col_darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); 17 $col_navy = imagecolorallocate($image, 0x00, 0x00, 0x80); 18 $col_darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); 19 $col_red = imagecolorallocate($image, 0xFF, 0x00, 0x00); 20 $col_darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); 21 22 23 /* 簡單繪製圖像的函數舉例 24 * imagefill($image,$sx,$sy,$color) 區域填充,與(sx,sy)相鄰且顏色相同的電都會被填充 25 * imagesetpixel($image,$sx,$sy,$color) 畫點 (sx,sy) 26 * imageline($image,$sx,$sy,$ex,$ey,$color) 畫線 (sx,sy)->(ex,ey) 27 * imagerectangle($image,$sx,$sy,$ex,$ey,$color) 畫空心矩形 28 * imagefilledrectagle($image,$sx,$sy,$ex,$ey,$color) 畫填充矩形 29 * imagepolygon($image,$array,$arraynum,$colors) 畫多邊形 30 * imagefilledpolygon($image,$array,$arraynum,$colors) 畫填充多邊形 31 * imageellipse($image,$cx,$cy,$w,$h,$color) 畫橢圓,(cx,cy)爲圓心,w,h分別是寬高 32 * imagefilledellipse($image,$cx,$cy,$w,$h,$color) 畫填充橢圓 33 * imagearc($image,$cx,$cy,$w,$h,$s,$e,$color) 畫弧,s,e指的是起始和結束度數 34 * imagefilledarc($image,$cx,$cy,$w,$h,$s,$e,$color,$style) 畫填充弧,多了一個style參數 35 */ 36 imagefill($image, 0, 0, $col_white); 37 38 imagesetpixel($image, 150, 20, $col_red); 39 imageline($image, 120, 50, 180, 50, $col_red); 40 41 imagerectangle($image, 200, 30, 300, 130, $col_red); 42 imagefilledrectangle($image, 225, 55, 275, 105, $col_red); 43 44 $array_pts = array(350, 30, 400, 40, 430, 70, 370, 100); 45 $array_ptsfilled = array(450, 30, 500, 40, 530, 70, 470, 100); 46 imagepolygon($image, $array_pts, 4, $col_red); 47 imagefilledpolygon($image, $array_ptsfilled, 4, $col_red); 48 49 imageellipse($image, 650, 80, 150, 100, $col_red); 50 imagefilledellipse($image, 650, 80, 75, 50, $col_red); 51 52 imagearc($image, 100, 200, 150, 100, 0, 180, $col_red); 53 imagefilledarc($image, 100, 200, 150, 100, 180, 210, $col_red, IMG_ARC_PIE); //餅狀 54 imagefilledarc($image, 100, 200, 150, 100, 215, 245, $col_red, IMG_ARC_CHORD); //扇形對應的三角 55 imagefilledarc($image, 100, 200, 150, 100, 250, 280, $col_red, IMG_ARC_EDGED); 56 imagefilledarc($image, 100, 200, 150, 100, 285, 315, $col_red, IMG_ARC_NOFILL); 57 imagefilledarc($image, 100, 200, 150, 100, 320, 350, $col_red, IMG_ARC_ROUNDED); 58 59 //如下繪製餅形圖,畫出3D效果 60 for ($i = 60; $i > 50; $i--) { 61 imagefilledarc($image, 50, $i, 100, 50, -160, 40, $col_darknavy, IMG_ARC_PIE); 62 imagefilledarc($image, 50, $i, 100, 50, 40, 75, $col_darkgray, IMG_ARC_PIE); 63 imagefilledarc($image, 50, $i, 100, 50, 75, 200, $col_darkred, IMG_ARC_PIE); 64 } 65 66 imagefilledarc($image, 50, 50, 100, 50, -160, 40, $col_navy, IMG_ARC_PIE); 67 imagefilledarc($image, 50, 50, 100, 50, 40, 75, $col_gray, IMG_ARC_PIE); 68 imagefilledarc($image, 50, 50, 100, 50, 75, 200, $col_red, IMG_ARC_PIE); 69 70 imagestring($image, 1, 15, 55, '34.7%', $col_white); 71 imagestring($image, 1, 45, 35, '55.5%', $col_white); 72 73 /* 繪製文字函數 74 * imagestring($image,$font,$x,$y,$s,$color) 畫水平字符串,font取1~5越大尺寸越大 75 * imagestringup($image,$font,$x,$y,$s,$color) 畫豎直字符串 76 * imagechar($image,$font,$x,$y,$c,$color) 畫水平字符 77 * imagecharup($image,$font,$x,$y,$c,$color) 畫豎直字符 78 * 以上函數都只能輸出內置字體,使用imagettftext輸出與設備無關的truetype字體 79 * imagettftext($image,$size,$angle,$x,$,$color,$fontfile,$text) 80 * size字體大小(GD1是像素,GD2是點數,$angle表示角度,fontfile是字體路徑,text是字符串,注意要使用UTF8編碼) 81 */ 82 $array_str = "string"; 83 imagestring($image, 4, 250, 150, $array_str, $col_red); 84 imagestringup($image, 4, 250, 250, $array_str, $col_red); 85 86 for ($i = 0; $i < strlen($array_str); $i++) { 87 imagechar($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red); 88 imagecharup($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red); 89 } 90 91 $text = iconv("GB2312", "UTF-8", "無兄弟,不編程"); 92 imagettftext($image, 20, 0, 450, 200, $col_red, "msyh.ttc", $text); 93 94 95 96 /* 生成圖像 97 * 支持GIF,JPEG,PNG,WBMP格式,分別對應四個函數imagepng,imagegif,imagejpeg,imagewbmp 98 * 這四個函數第一個參數都是資源名,參數二是文件名,將圖片保存到文件;不提供的話會直接輸出到瀏覽器,可是要設置header類型 99 * imagejpeg第三個參數指定圖像質量(0~100),imagewbmp第三個參數指定背景色 100 * 如下程序自動監測GD庫支持的圖像類型以生成對應格式的圖像 101 */ 102 if (function_exists("imagegif")) { 103 header('Content-type: image/gif'); 104 imagegif($image); 105 } elseif (function_exists("imagejpeg")) { 106 header('Content-type: image/jpeg'); 107 imagejpeg($image); 108 } elseif (function_exists("imagepng")) { 109 header('Content-type: image/png'); 110 imagepng($image); 111 } elseif (function_exists("imagewbmp")) { 112 header('Content-type: image/vnd.wap.wbmp'); 113 imagewbmp($image); 114 } 115 /* 銷燬資源 116 */ 117 imagedestroy($image); 118 119 120 ?>
執行結果php