PHP繪圖入門以及JPGraph庫的使用

php繪圖的座標系統(X軸向右,Y軸向下)


1.      php繪圖的基本原理和步驟

l  創建畫布

l  繪製需要的各種圖形(圓,直線,矩形,弧線,扇形...)

l  輸出圖像到網頁,也可以另存

l  銷燬該圖片(釋放內存)

  

☞ 目前網站開發常見的圖片格式有gif  jpg/jpeg  png bmp ....

l  gif 圖片壓縮率高,但是隻能顯示256色,可能造成顏色丟失,可以顯示動畫

l  jpg/jpeg 圖片的壓縮率高(有損壓縮),可以用較小的文件來顯示,網頁上用的比較圖

l  png , 該格式綜合了gif 和jpg的優勢,缺點是不能顯示動畫;在傳送之前有個壓縮算法,提高效率

 

php繪圖技術快速入門

前提 : 首先請確認你的  gd庫啓用 php.ini

;啓用圖像庫

extension=php_gd2.dll

記住,需要重啓啓動apache

 

 

image1.php案例:

<?php
       $im=imagecreatetruecolor(400,300);
       $red=imagecolorallocate($im,255,0,0);
       //圓
       //imageellipse($im,20,20,20,20,$red);
       //直線
       //imageline($im,0,0,400,300,$red);
       //矩形
       //imagerectangle($im,2,2,40,50,$red);
       //填充矩形
       //imagefilledrectangle($im,2,2,40,50,$red);
       //弧線
       //imagearc($im,100,100,50,50,180,270,$red);
       //扇形
       //imagefilledarc($im,100,100,80,50,180,270,$red,IMG_ARC_PIE);
      
       //拷貝圖片到畫布
       //1.加載源圖片
       //$srcImage=imagecreatefromgif("2.GIF");
       //這裏我們可以使用一個getimagesize()
       //$srcImageInfo=getimagesize("2.GIF");
 
       //拷貝源圖片到目標畫布
       //imagecopy($im,$srcImage,0,0,0,0,$srcImageInfo[0],$srcImageInfo[1]);
 
       //寫字
       $str="hello,world,中文";
       //imagestring($im,5,0,0,"hello,world,中文",$red);
       //在字體庫中去找中文
       imagettftext($im,20,10,50,50,$red,"simhei.ttf",$str);
       header("content-type:image/png");
       imagepng($im);
       imagedestory($im);
?>


 

綜合案例:(綜合使用)

 

代碼:

<?php
      
       //分析思路(先畫出扇形)
        //1.畫布
       $im=imagecreatetruecolor(400,300);
        //默認是黑色背景(一會告訴大家怎麼修改)
       $white=imagecolorallocate($im,255,255,255);
       imagefill($im,0,0,$white);
 
       //2.畫出扇形
       //創建三個顏色
       $red=imagecolorallocate($im,254,0,0);
       $darkred=imagecolorallocate($im,144,0,0);
       $blue=imagecolorallocate($im,0,0,128);
       $darkblue=imagecolorallocate($im,0,0,80);
       $gary=imagecolorallocate($im,192,192,192);
       $darkgary=imagecolorallocate($im,144,144,144);
 
       for($i=60;$i>=50;$i--){
       imagefilledarc($im,100,$i,100,50,0,35,$darkblue,IMG_ARC_PIE);
       imagefilledarc($im,100,$i,100,50,35,75,$darkgary,IMG_ARC_PIE);
       imagefilledarc($im,100,$i,100,50,75,360,$darkred,IMG_ARC_PIE);
       }
 
       //在上面加蓋
       imagefilledarc($im,100,50,100,50,0,35,$blue,IMG_ARC_PIE);
       imagefilledarc($im,100,50,100,50,35,75,$gary,IMG_ARC_PIE);
       imagefilledarc($im,100,50,100,50,75,360,$red,IMG_ARC_PIE);
 
       //輸出圖片
       header("content-type:image/png");
       imagepng($im);
       imagedestory($im);
      
       //有興趣的同學可以將其封裝成一個函數.
 
 
?>

 介紹一個取色器,FastStone截圖軟件附帶的一個功能,截取我們要的顏色,可以看到其RGB值


u  思考

如果每次都必須自己去畫,這樣的圖,是不是很麻煩,有沒有現成可以用於做圖表開發的庫

->jpgrapf

 

u  jpgraph的介紹

 

jpgraph的安裝和配置

1.      下載 官網

2.      解壓 (先拷貝到htdocs目錄)

3.      配置 完畢使用( 就是把 emample 目錄的其它文件 剪切到 emamlpe 文件夾內,注意要新建一個文件夾名字一定是  jpgraph )

4.      測試


 jpgraph 的實際使用案例(網名調查統計圖)

 

完成案例示意圖如下:

 

數據庫和數據

--參加選舉人的表

create tableelector(

electorId int,

namevarchar(64),

voteNums int,

voteMonth int);

 

insert intoelector values(1,'布什',10,1);

insert intoelector values(1,'布什',12,2);

insert intoelector values(1,'布什',34,3);

 

 

insert intoelector values(2,'奧巴馬',34,1);

insert intoelector values(2,'奧巴馬',30,2);

insert intoelector values(2,'奧巴馬',12,3);

insert intoelector values(2,'奧巴馬',30,4);

 

☞如果希望吧jpgraph 的圖表嵌入到其它的php文件中,可以使用<img/>

 如果您的圖片無法正常顯示,有可能是你在  PHP文件最開始放入了一個空行

代碼:

 

靜態顯示數據(沒有實時的取數據)

實時取數據(動態更新)

 

vote.php

<html>
<head>
<title>請投票</title>
<scriptlanguage="javascript">
function look(){
       window.location.href="showAll.php";
}
</script>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
</head>
<formaction="" method="">
<table>
<tr><td>請投票</td></tr>
<tr><td>
<inputtype="radio" name="vote" value="1">布什
<inputtype="radio" name="vote" value="2">奧巴馬
<inputtype="submit" value="投票"/>
</td></tr>
</table>
</form>
<formaction="" method="">
<tr>
<td><inputtype="button" οnclick="look();" value="查看投票統計圖表"/></td>
</tr>
</form>
</html>
 


showAll.php

 

<html>
<head>
<title>顯示</title>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
</head>
<body>
<h1>顯示網民支持情況</h1>
<imgsrc="showVote.php?id=1" />
<imgsrc="showvote.php?id=2" />
</body>
</html>


 

showVote.php (最核心的)

<?php //content="text/plain; charset=utf-8"
require_once('jpgraph/jpgraph.php');
require_once('jpgraph/jpgraph_bar.php');
 
//$datay1=array(13,8,119,7,17,6);
//$datay2=array(0,0,0,0,0,0);
 
//$datay1=array(13,8,11);
//$datay2=array(0,0,0);
// Create thegraph.
$graph = newGraph(350,250);
$graph->SetScale('textlin');
$graph->SetMarginColor('silver');
 
 
// Setup title
/*$str="";
$id=$_REQUEST['id'];
if($id==1){
       $str="支持布什的統計情況(萬)";
}
else if($id==2){
       $str="支持奧巴馬的統計情況(萬)";
}*/
 
//從數據庫
 
$id=$_REQUEST["id"];
//組織sql
$sql="select* from elector where electorId=$id order by voteMonth";
  
$conn=mysql_connect("localhost","root","root")or die("連接失敗".mysql_error());
mysql_select_db("test",$conn)or die(mysql_error());
mysql_query("setnames gbk") or die(mysql_error());
$res=mysql_query($sql,$conn)or die(mysql_error());
 
$datay1=array();
$datay2=array();
$i=0;
$title="";
while($row=mysql_fetch_array($res))
{
       $datay1[$i]=$row[2];
       $datay2[$i]=0;
      
       if($i==0){
              $title="支持".$row[1]."情況統計圖";
       }
       $i++;
}
mysql_free_result($res);
mysql_close($conn);
 
 
$graph->title->Set($title);
$graph->title->setFont(FF_SIMSUN,FS_BOLD,14);
// Create thefirst bar
$bplot = newBarPlot($datay1);
$bplot->SetFillGradient('AntiqueWhite2','AntiqueWhite4:0.8',GRAD_VERT);
$bplot->SetColor('darkred');
 
// Create thesecond bar
$bplot2 = new BarPlot($datay2);
$bplot2->SetFillGradient('olivedrab1','olivedrab4',GRAD_VERT);
$bplot2->SetColor('darkgreen');
 
// And join themin an accumulated bar
$accbplot = newAccBarPlot(array($bplot,$bplot2));
$graph->Add($accbplot);
 
$graph->Stroke();
?>


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章