PHP学习笔记-GD库与Jpgraph的使用

来源:互联网 发布:淘宝联盟怎么登录账号 编辑:程序博客网 时间:2024/05/17 00:57

转载请标明出处:
http://blog.csdn.net/hai_qing_xu_kong/article/details/52281196
本文出自:【顾林海的博客】

前言

学习PHP从第一篇笔记到现在这篇,已经十多篇了,每天花时间去学习是需要毅力的,好在自己对IT这行也是比较感兴趣,算是每天自娱自乐吧,下周一就去考科目三了,想想也是醉了,拖这么长时间。

GD库

GD库是一个开放的动态创建图像、源代码公开的函数库,可以从官方网站http://www.boutell.com/gd处下载。目前,GD库支持GIF、PNG、JPEG、WBMP和XBM等多种图像格式,用于对图像的处理。

GD库在PHP 5中是默认安装的,但要激活GD库,必须修改php.ini文件。将该文件中的“;extension=php_gd2.dll”选项前的分号“;”删除,保存修改后的文件并重新启动Apache服务器即可生效。

怎么查看是否成功加载GD库,可以在地址栏中输入“http://127.0.0.1/phpinfo.php”,如果在页面中检索到如下图所示的GD库安装信息,就说明GD加载成功。

这里写图片描述

Jpgraph的安装

Jpgraph可以从其官方网站http://www.aditus.nu/jpgraph/下载。

Jpgraph的安装方法非常简单,文件下载后,安装步骤如下:

  1. 将压缩包下的全部文件解压到一个文件夹中,如F:\AppServ\www\jpgraph。
  2. 打开PHP的安装目录,编辑php.ini文件并修改其中的include_path参数,在其后增加前面的文件夹名,如include_path = “.;F:\AppServ\www\jpgraph”。
  3. 重新启动Apache服务器即可生效。

在下面例子中,如果出现以下错误:
strtotime(): It is not safe to rely on the system’s timezone settings

请不要慌张,打开php.ini文件输入以下内容:

date.timezone = “Asia/Shanghai”

实例展示

使用GD2函数在照片上添加文字

PHP中的GD库支持中文,但必须要以UTF-8格式的参数来进行传递,如果使用imageString()函数直接绘制中文字符串就会显示乱码,这是因为GD2对中文只能接收UTF-8编码格式,并且默认使用英文字体,所以要输出中文字符串,必须对中文字符串进行转码,并设置中文字符使用的字体。否则,输出的只能是乱码。

<?phpheader("content-type:image/jpeg");       //定义输出为图像类型$im=imagecreatefromjpeg("images/photo.jpg");       //载入照片$textcolor=imagecolorallocate($im,56,73,136);//设置字体颜色为蓝色,值为RGB颜色值$fnt="c:/windows/fonts/simhei.ttf";      //定义字体$motto=iconv("gb2312","utf-8","大美女");     //定义输出字体串imageTTFText($im,120,0,80,340,$textcolor,$fnt,$motto);      //写TTF文字到图中imagejpeg($im);       //建立JPEG图形imagedestroy($im);    //结束图形,释放内存空间?>

效果展示

这里写图片描述

使用图像处理技术生成验证码

验证码功能的实现方法很多,有数字验证码、图形验证码和文字验证码等。在本节中介绍一种使用图像处理技术生成的验证码。

1、创建一个checks.php文件,在该文件中使用GD2函数创建一个4位的验证码,并且将生成的验证码保存在Session变量中。

<?phpheader("Content-Type:text/html;charset=utf-8");session_start();header("content-type:image/png");     //设置创建图像的格式$image_width=70;                      //设置图像宽度$image_height=18;                     //设置图像高度srand(microtime()*100000);            //设置随机数的种子for($i=0;$i<4;$i++){                  //循环输出一个4位的随机数   $new_number.=dechex(rand(0,15));}$_SESSION[check_checks]=$new_number;    //将获取的随机数验证码写入到SESSION变量中     $num_image=imagecreate($image_width,$image_height);  //创建一个画布imagecolorallocate($num_image,255,255,255);         //设置画布的颜色for($i=0;$i<strlen($_SESSION[check_checks]);$i++){  //循环读取SESSION变量中的验证码   $font=mt_rand(3,5);                             //设置随机的字体   $x=mt_rand(1,8)+$image_width*$i/4;               //设置随机字符所在位置的X坐标   $y=mt_rand(1,$image_height/4);                   //设置随机字符所在位置的Y坐标   $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));    //设置字符的颜色   imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color);                     //水平输出字符}imagepng($num_image);                  //生成PNG格式的图像imagedestroy($num_image);              //释放图像资源?>

2、创建一个用户登录的表单,并调用checks.php文件,在表单中输出图像的内容,提交表单信息,使用if语句判断输入的验证码是否正确。如果填写的验证码与随机产生的相等,提示“用户登录成功!”。

<?phpheader("Content-Type:text/html;charset=utf-8");session_start();if($_POST["Submit"]!=""){$checks=$_POST["checks"];if($checks==""){echo "<script> alert('验证码不能为空');window.location.href='index.php';</script>";}if($checks==$_SESSION[check_checks]){    echo "<script> alert('用户登录成功!');window.location.href='index.php';</script>";}else{    echo "<script> alert('您输入的验证码不正确!');window.location.href='index.php';</script>";}}?><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>rand函数的应用</title><style type="text/css"><!--.STYLE1 {    font-size: 12px;    color: #FFFFFF;    font-weight: bold;}.style2 {font-weight: bold; font-size: 12px;}--></style></head><body><form name="form" method="post" action="">  <table width="1003" border="0" cellspacing="0" cellpadding="0">    <tr>      <td width="168" height="169" background="images/index_01.gif">&nbsp;</td>      <td width="685" background="images/index_02.gif">&nbsp;</td>      <td width="150" background="images/index_03.gif">&nbsp;</td>    </tr>    <tr>      <td width="168" height="311" background="images/index_04.gif">&nbsp;</td>      <td background="images/index_05.gif"><table width="675" height="169"  border="0" cellpadding="0" cellspacing="0">        <tr>          <td height="43" align="center" valign="baseline">&nbsp;</td>          <td align="center" valign="middle">&nbsp;</td>          <td align="center" valign="baseline">&nbsp;</td>        </tr>        <tr>          <td width="382" height="24" align="center" valign="baseline">&nbsp;</td>          <td width="207" height="24" valign="middle"><span class="style2">用户名</span><span class="STYLE1">            <input  name="txt_user" id="txt_user" style="height:20px " size="10">              </span></td>          <td width="86" height="24" align="center" valign="baseline">&nbsp;</td>        </tr>        <tr>          <td height="24" align="center" valign="baseline">&nbsp;</td>          <td height="24" valign="middle"><span class="style2">密码</span><span class="STYLE1">          <input  name="txt_pwd" type="password" id="txt_pwd" style="FONT-SIZE: 9pt; height:20px" size="10">          </span></td>          <td height="24" align="center" valign="baseline">&nbsp;</td>        </tr>        <tr>          <td height="24" align="center" valign="baseline">&nbsp;</td>          <td height="24" valign="middle"><span class="style2">验证码</span><span class="STYLE1">          <input name="checks" size="6" style="height:20px ">          <img src="checks.php" width="70" height="18" border="0" align="bottom"></span>&nbsp;&nbsp;</td>          <td height="24" align="center" valign="baseline">&nbsp;</td>        </tr>        <tr>          <td height="40" align="center" valign="baseline">&nbsp;</td>          <td align="center" valign="baseline">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="Submit" value="登录"></td>          <td align="center" valign="baseline">&nbsp;</td>        </tr>      </table></td>      <td background="images/index_06.gif">&nbsp;</td>    </tr>    <tr>      <td height="100">&nbsp;</td>      <td>&nbsp;</td>      <td>&nbsp;</td>    </tr>  </table></form></body></html>

效果1,登录失败:

这里写图片描述

效果2,登录成功:

这里写图片描述

使用柱形图统计图书月销售量

柱形图的使用在Web网站中非常广泛,它可以直观地显示数据信息,使数据对比和变化趋势一目了然,从而可以更加准确、直观地表达信息和观点。

<?phpheader("Content-Type:text/html;charset=utf-8");include ("jpgraph.php");include ("jpgraph_bar.php");$datay=array(160,180,203,289,405,488,489,408,299,166,187,105);//创建画布$graph = new Graph(600,300,"auto");    $graph->SetScale("textlin");$graph->yaxis->scale->SetGrace(20);//创建画布阴影$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");//创建标题$graph->title->Set("2016年销量统计");//设置X坐标轴文字$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");$graph->xaxis->SetTickLabels($a); //设置字体$graph->title->SetFont(FF_SIMSUN);$graph->xaxis->SetFont(FF_SIMSUN); //输出矩形图表$graph->Stroke();?>

效果:

这里写图片描述

使用折线图统计图书月销售额

折线图的使用同样十分广泛,如商品的价格走势、股票在某一时间段的涨跌等,都可以使用折线图来分析。

<?phpheader("Content-Type:text/html;charset=utf-8");        include ("jpgraph.php");        include ("jpgraph_line.php");                                                       //引用折线图LinePlot类文件        $datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460);     //填充的数据           $graph = new Graph(600,300,"auto");                                //创建画布        $graph->img->SetMargin(50,40,30,40);                           //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素        $graph->img->SetAntiAliasing();                                    //设置折线的平滑状态        $graph->SetScale("textlin");                                   //设置刻度样式        $graph->SetShadow();                                           //创建画布阴影        $graph->title->Set("2016年图书月销售额折线图");  //设置标题        $graph->title->SetFont(FF_SIMSUN,FS_BOLD);                     //设置标题字体        $graph->SetMarginColor("lightblue");                           //设置画布的背景颜色为淡蓝色        $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);              //设置Y轴标题的字体        $graph->xaxis->SetPos("min");        $graph->yaxis->HideZeroLabel();        $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5');        $a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");          //X轴        $graph->xaxis->SetTickLabels($a);                               //设置X轴        $graph->xaxis->SetFont(FF_SIMSUN);                                 //设置X坐标轴的字体        $graph->yscale->SetGrace(20);         $p1 = new LinePlot($datay);                                       //创建折线图对象        $p1->mark->SetType(MARK_FILLEDCIRCLE);                         //设置数据坐标点为圆形标记        $p1->mark->SetFillColor("red");                                    //设置填充的颜色        $p1->mark->SetWidth(4);                                            //设置圆形标记的直径为4像素        $p1->SetColor("blue");                                         //设置折形颜色为蓝色        $p1->SetCenter();                                              //在X轴的各坐标点中心位置绘制折线        $graph->Add($p1);                                              //在统计图上绘制折线        $graph->Stroke();                                              //输出图像?>

效果:

这里写图片描述

使用3D饼形图统计各类商品的年销售额比率

饼形图是一种非常实用的数据分析技术,可以清晰地表达出数据之间的关系。在调查某类商品的市场占有率时,最好的显示方式就是使用饼形图,通过饼形图可以直观地看到某类产品的不同品牌在市场中的占有比例。

<?phpheader("Content-Type:text/html;charset=utf-8");include_once ("jpgraph.php");include_once ("jpgraph_pie.php");include_once ("jpgraph_pie3d.php");         //引用3D饼图PiePlot3D对象所在的类文件$data = array(266036,295621,335851,254256,254254,685425);          //定义数组$graph = new PieGraph(540,260,'auto');             //创建画布$graph->SetShadow();                               //设置画布阴影$graph->title->Set("应用3D饼形图统计2016年商品的年销售额比率");         //创建标题$graph->title->SetFont(FF_SIMSUN,FS_BOLD);         //设置标题字体$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL);          //设置图例字体$p1 = new PiePlot3D($data);                            //创建3D饼形图对象$p1->SetLegends(array("IT数码","家电通讯","家居日用","服装鞋帽","健康美容","食品烟酒"));$targ=array("pie3d_csimex1.php?v=1","pie3d_csimex1.php?v=2","pie3d_csimex1.php?v=3",            "pie3d_csimex1.php?v=4","pie3d_csimex1.php?v=5","pie3d_csimex1.php?v=6");$alts=array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d");$p1->SetCSIMTargets($targ,$alts);$p1->SetCenter(0.4,0.5);                   //设置饼形图所在画布的位置$graph->Add($p1);                          //将3D饼图形添加到图像中$graph->StrokeCSIM();                      //输出图像到浏览器?>

效果:

这里写图片描述

0 0
原创粉丝点击