GD庫的使用

2021-08-21 21:17:33 字數 3777 閱讀 1039

一、建立乙個簡單的影象

header("content-type:image/gig"); // 新增此句解決亂碼問題

$im = imagecreate(200, 60);

$white = imagecolorallocate($im, 225, 65, 165);

imagegif($im);

二、使用gd2函式在**上新增文字

header("content-type:image/jpeg"); // 定義輸入影象的型別

$im = imagecreatefromjpeg("images/timg.jpg"); //載入

$textcolor = imagecolorallocate($im,225, 66,156);//設定字型顏色

$fnt = 'c:/users/******/desktop/font1945/stcaiyun.ttf'; //定義字型

$motto = iconv("utf-8","utf-8","年年有今日\n歲歲有今朝"); //定義需要輸出的字串

imagettftext($im,20,0,10,140,$textcolor,$fnt,$motto); //將需要輸入的字串寫到中

imagegif($im); //建立jpeg圖形

imagedestroy($im); //結束圖形,釋放資源

三、使用影象處理技術生成驗證碼

header("content-type:image/png");

$image_width = 70;

$image_height = 18;

srand(microtime()*100000);

$array = array();

for($i=0; $i<4;$i++)

$num_image = imagecreate($image_width, $image_height);

imagecolorallocate($num_image, 255,255, 255);

for($i=0; $i四、繪製折線圖

使用前的配置:

開啟php.ini檔案,定位到extension=php_gd2.dll,將前面的分號刪除

開啟php.ini檔案,修改include_path引數,增加jpgraph的路徑,如:『include_path="d:\wampserve\wamp\www\jpgraph"』,儲存php.ini檔案,重新啟動服務。

$graph = new graph(400, 300);//建立新的graph物件

$graph->setscale("textlin");//設定刻度樣式

$graph->img->setmargin(30,30,80, 30);//設定圖示邊界

$graph->title->set("year to date cost");//設定圖示標題

//繪製曲線

//將要用於圖表建立的資料存放在陣列中

$data = array(19,23,34,38,45,67,71,78,85,87,90.96);

$lineplot = new lineplot($data);

$lineplot->setlegend("amount(m dollars)");

$lineplot->setcolor("green");

//講曲線放在圖表中

$graph->add($lineplot);

//輸出圖表

$graph->stroke();

五、繪製柱狀圖

// 這是柱狀圖12個柱的資料

$datay = array(160,180,203,289,405,488,489,408,299,166,187,105);

建立畫布

$graph = new graph(600,300);

$graph->setscale("textlin"); //設定刻度樣式,x軸和y軸

$graph->yaxis->scale->setgrace(20); //設定y軸刻度值解析度

// 建立畫布陰影

$graph->setshadow();

$graph->img->setmargin(40,30,30,40); //設定生成圖形與畫布邊緣距離,順序為左右上下

$bplot = new barplot($datay); //建立柱狀圖,傳入資料

$bplot->setfillcolor('orange'); //設定柱狀圖的顏色

$bplot->value->show(); //讓具體資料在頂端顯示

//$bplot->value->setformat('%d'); //設定具體資料的輸出型別

$graph->add($bplot); //將柱形圖新增到圖形中

$graph->setmargincolor("lightblue"); //設定畫布背景顏色

$title = iconv("gb2312", "utf-8", $title);

$graph->title->set($title); //設定柱形圖表頭

//$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");

//$graph->xaxis->setticklabels($a); //設定x軸下方的顯示資料

//$graph->title->setfont(ff_simsun);

//$graph->xaxis->setfont(ff_simsun);

$graph->stroke();六、繪製餅圖

//設定影象邊界範圍

$graph->img->setmargin(30,30,80,30);

//設定標題

$graph->title->set("pieplot test");

//得到餅圖物件

$pieplot=new pieplot($data);

//設定圖例

$pieplot->setlegends(array("one","tow","three","four","five"));

//$graph->legend->setfont(ff_simsun,fs_normal);

//設定圖例位置

$graph->legend->pos(0.01,0.45,"left","top");

//新增到畫布中

$graph->add($pieplot);

//輸出

$graph->stroke();

php中gd庫的簡單使用

如果要使用gd庫,首先你需要開啟gd庫的擴充套件,extension php gd2.dll。然後使用phpinfo 函式檢測擴充套件是否被開啟。建立畫布 width 500 height 500 image imagecreatetruecolor width height 建立顏色 red im...

php中GD庫的簡單使用

在php中需要影象處理的地方gd庫會發揮重要的作用,php可以建立並處理包括gif,png,jpeg,wbmp以及xpm在內的多種影象格式,簡單的舉幾個例子 1 用gd庫會建立一塊空白,然後繪製乙個簡單的線條 1 img imagecreatetruecolor 100,100 建立空白 2 red...

php中有關GD庫的使用

class test extends indexbase public mui fonts mui.ttf 如果沒有要自己載入到相應的目錄下 本地www protected angle 0 protected size 15 protected showx 100 protected showy 1...