標籤:
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 ?>
難重點:
PHP->利用GD庫建立映像