基礎環境:PHP5
因爲jpgraph依賴於GD庫,所以使用jpgraph之前需確認GD庫是否已經安裝。(GD庫,是php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片。)
如何確認GD庫是否安裝?方法如下:
在apache網站根目錄(默認爲/var/www)建立一個文件,如phpinfo.php,內容如下:
<%php
phpinfo()
%>
通過瀏覽器,訪問此php文件,如:http://server_ip/phpinfo.php,若顯示有如下內容表示GD庫已經安裝,若無則需先安裝GD庫。
如何安裝gd庫可參考:http://www.boutell.com/gd/
jpgraph
1. 下載jpgraph
2. 安裝jpgraph
將下載的jpgraph解壓後放到php能訪問的目錄,這裏爲/usr/share/php5/,在/usr/share/php5下建立一軟連接指向jpgraph庫的src目錄
ln -s jpgraph-3.5.0b1/src jpgraph
更改php的配置文件/etc/php5/apache2/php.ini和/etc/php5/cli/php.ini,找到include_path項,取消註釋並把/usr/share/php5加入到搜索目錄,如:
include_path = ".:/usr/share/php5"
3. 測試jpgraph
到http://jpgraph.net/上面隨便找個例子,點擊進去,將其源碼拷貝出來,這裏拷貝的是http://jpgraph.net/features/src/show-example.php?target=new_bar3.php
代碼如下:
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
$datay=array(62,105,85,50);
// Create the graph. These two calls are always required
$graph = new Graph(350,220,'auto');
$graph->SetScale("textlin");
//$theme_class="DefaultTheme";
//$graph->SetTheme(new $theme_class());
// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);
//$graph->ygrid->SetColor('gray');
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($datay);
// ...and add it to the graPH
$graph->Add($b1plot);
$b1plot->SetColor("white");
$b1plot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(45);
$graph->title->Set("Bar Gradient(Left reflection)");
// Display the graph
$graph->Stroke();
?>
在服務器根目錄新增文件jpgraph.php,將拷貝的代碼粘貼上去,保存後通過瀏覽器訪問,看到如下圖:
因爲我的目的是要製作pdf文件,所以需要把生成的圖片保存成文件,把代碼封裝修改一下,製作成一個腳本文件,如下:
#!/usr/bin/php -q
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
require_once ('fpdf/fpdf.php');
$datay=array(62,105,85,50);
// Create the graph. These two calls are always required
$graph = new Graph(350,220,'auto');
$graph->SetScale("textlin");
//$theme_class="DefaultTheme";
//$graph->SetTheme(new $theme_class());
// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);
//$graph->ygrid->SetColor('gray');
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($datay);
// ...and add it to the graPH
$graph->Add($b1plot);
$b1plot->SetColor("white");
$b1plot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(45);
$graph->title->Set("Bar Gradient(Left reflection)");
// Display the graph
//$graph->Stroke();
$graph->Stroke("/tmp/test1.png");
?>
給上述文件添加執行權限後,可在/tmp/下生成test1.png圖片
tcpdf
1. 下載tcpdf
http://sourceforge.net/projects/tcpdf/files/
2. 安裝tcpdf
解壓tcpdf壓縮包到/usr/share/php5目錄
因爲安裝jpgraph時已經將/usr/share/php5目錄添加到php的搜索路徑中,所以這裏就不必修改php.ini了
3. 測試tcpdf
建立文件tcpdf,內容如下:
#!/usr/bin/php -q
<?
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// add a page
$pdf->AddPage();
// set JPEG quality
$pdf->setJPEGQuality(75);
// Image example
$pdf->Image('/tmp/test1.png', 50, 50, 100, 100, '', 'http://www.tcpdf.org', '', true, 150);
$pdf->Cell( 0, 0, 'TCPDF test', 0, 1 );;
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example.pdf', 'D');
?>
給上述文件添加執行權限然後執行,生成pdf如下:
這裏只是簡單測試,把jpgraph動態生成的圖形包含到pdf,若要把pdf整的更加美觀,需詳細查看jpgraph和tcpdf庫。