欢迎您的光临,本博所发布之文章皆为作者亲测通过,如有错误,欢迎通过各种方式指正。

教程  php进阶(一)--Jpgraph图形图像处理技术讲解

PHP学习 本站 1026 0评论

一、PHP中加载GD库


Jpgraph需要GD库的支持,首先需要在php中加载GD库。


什么是GD库


GD是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。

GD库官方网站:www.boutell.com/gd 

GD库支持GIF,PNG,JPEG,WBMP和XBM等多种图像格式。


开启GD库


如上图表示php中GD库已打开生效。


linux服务器上配置方法:

yum install php-gd* #适用于32bit操作系统安装命令;

yum install php-gd.x86_64 #适用于64bit操作系统安装命令;

333.jpg


二、Jpgraph的安装和配置


Jpgraph是掌握复杂抽象的画图函数。以前用PHP作图时必须要掌握复杂抽象的画图函数,或者借助一些网上下载的画柱形图、饼形图的类来实现。没有一个统一的chart类来实现图表的快速开发。

444.jpg


1.Jpgraph的安装


555.jpg

666.jpg


2.Jpgraph的配置

 

777.jpg


三、图形图像的应用


1.中文字体和背景设置


<?php
require_once ('source/jpgraph.php');
require_once ('source/jpgraph_pie.php');
$graph = new Graph(350,250);
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->title->Set("文本内容");
$graph->title->SetFont(FF_FONT1,FS_BOLD);  //英文字体FS_BOLD表示默认字体
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,24); //设置字体,类型,大小
$graph->title->SetColor('red'); //设置字体颜色
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); // 设置标题中文字体 
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD); //设置线条指示字体
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);//设置Y轴线条指示字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);//设置X轴线条指示字体
$graph->SetColor('red'); //设置背景
$graph->SetBackgroundImage("images/photo.jpg",1); //设置背景
$graph->img->SetMargin(0,0,0,0); // 空余四角边距(左右上下)
$graph->Set3DPerspective(SKEW3D_LEFT,700,600,true);
//倾斜3D效果
//   1. 'SKEW3D_UP'
//   2. 'SKEW3D_DOWN'
//   3. 'SKEW3D_LEFT'
//   4. 'SKEW3D_RIGHT'
//绘制饼状图
$data=array(120,140,500);
$graph = new PieGraph(400,300);  //画饼状图
$graph->SetShadow();
$graph->title->Set("文本内容"); //设置标题
$graph->title->setFont(FF_SIMSUN,FS_BOLD,24); //设置字体,类型,大小
$graph->title->SetColor('red');
$p1=new PiePlot($data);
$graph->Add($p1);
$graph->Stroke();  //画图
?>

123.jpg


2.创建一个简单的图像


使用GD2函数库可以实现对各种图形图像的处理。创建画布是使用GD2函数库来创建图像的第一步,无论创建什么样的图像,首先都需要创建一个画布,其他操作都是在这个画布上完成。在GD2中创建画布可以通过函数imagecreate()函数实现。  

语法如下:resource imagecreate ( int $width , int $height ) 

该函数返回一个图像的标识符,参数$width,$height表示为图像的尺寸,单位为像素。

<?php
header("Content-type: image/gif");//该页面以gif格式输出,要不然不能正常显示图片
$im = imagecreate(200,60);      //创建一个画布
$white = imagecolorallocate($im, 225,66,159);  //设置画布的背景颜色为浅绿色
imagegif($im);
?>

 888.jpg

 

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


999.jpg

<?php
header("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,22,0,180,40,$textcolor,$fnt,$motto); //写TTF文字到图中
imagejpeg($im);       //建立JPEG图形
imagedestroy($im);    //结束图形,释放内存空间
?>

100.jpg


4.使用图像处理生成验证码


101.jpg

验证码文件checks.php:

<?php
/*PHP实现验证码*/

session_start();//开启会话

//创建画布
$image=imagecreatetruecolor(100,38);
//背景颜色
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);


$captch_code='';//存储验证码

//随机选取4个数字
for($i=0;$i<4;$i++){
    $fontsize=10;        //
    $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
    $fontcontent=rand(0,9);
    $captch_code.=$fontcontent;
    $x=($i*100/4)+rand(5,10);   //随机坐标
    $y=rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}


/*//字母和数字混合验证码
for($i=0;$i<4;$i++) {
    $fontsize = 10;        //
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//??????
    $data = 'abcdefghijklmnopqrstuvwxyz1234567890';   //数据字典
    $fontcontent = substr($data, rand(0, strlen($data)), 1);
    $captch_code.=$fontcontent;
    $x = ($i * 100 / 4) + rand(5, 10);
    $y = rand(5, 10);
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}*/

$_SESSION['code']=$captch_code;

//增加干扰点
for($i=0;$i<200;$i++){
    $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
    imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);//
}

//增加干扰线
for($i=0;$i<3;$i++){
    $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
    imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}

//输出格式
header('content-type:image.png');
imagepng($image);

//销毁图片
imagedestroy($image);

?>

主文件index.php:

<?php
session_start();
if($_POST["submit"]!=""){
$checks=$_POST["checks"];
if($checks==""){
echo "<script> alert('验证码不能为空');window.location.href='index.php';</script>";
}
if($checks==$_SESSION[code]){
   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=gb2312">
<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 type="text" placeholder="验证码" name="checks" size="6" style="height:20px ">
          <img id="captcha_img" src="checks.php?r=<?php echo rand();?>" alt="验证码" >
                  <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='checks.php?r='+Math.random()">换一个</a>
           </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>

102.jpg


5.使用柱形图统计图书月销量


103.jpg

<?php
include ("source/jpgraph.php");
include ("source/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("《PHP从入门到精通》2009年销量统计");
//设置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();
?>

 104.jpg


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


<?php
include ("source/jpgraph.php");
include ("source/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("2009年《PHP从入门到精通》图书月销售额折线图");//设置标题
$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();//输出图像
?>

105.jpg


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


<?php
include_once ("source/jpgraph.php");
include_once ("source/jpgraph_pie.php");
include_once ("source/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饼形图统计2009年商品的年销售额比率");//创建标题
$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();//输出图像到浏览器
?>

106.jpg


中文字体乱码


· Gpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示,如果文件的编码方式是gb2312,只需把SetFont()方法的第一个参数设置为FF_SIMSUN即可

· 如果是utf-8编码的,需要先把汉字编码转化为gb2312,这样汉字才能正常显示

· 转换编码方式可以使用 iconv("UTF-8","gb2312",$x);


1)支持标题中文

修改jpgraph_ttf.inc.php

搜索CHINE_TTF_FONT选项,修改常量的值使支持中文的字体

define('CHINESE_TTF_FONT', 'SIMYOU.TTF');

设置标题前,加上这段代码

$graph->title->SetFont(FF_CHINESE);


2)支持图例中文

修改jpgraph_legend.inc.php,将$font_family修改成FF_CHINESE

public $font_family=FF_DEFAULT,$font_style=FS_NORMAL,$font_size=8;


一些常用的方法:

$graph->title->Set('设置图表的标题');
$graph->tabtitle->Set('设置图片头部文字');
$graph->xaxis->title->Set("设置X轴的标题");
$graph->yaxis->title->Set("设置Y轴的标题");
$graph->SetScale('textlin');//设置刻度值类型
$graph->img->SetMargin(50,40,40,55);//边框间距(左右上下)
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);//标题字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10);
$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,12);//坐标柱上的字体
$graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD,12);
$graph->title->SetColor('red'); ///标题颜色
$graph->xaxis->title->SetColor('red');
$graph->yaxis->title->SetColor('red');
$graph->xaxis->title->SetMargin(20);//距离坐标轴的距离
$graph->yaxis->title->SetMargin(20);//距离Y轴的距离
$linepot->SetColor('red');//折线的颜色(折线图)
$linepot->SetWeight(2);//折线的宽度
$linepot->value->SetFormat('%0.1f'); //值的格式化
$linepot->value->show(tru('%0.1f'); //值的格式化
$linepot->value->show(true);//显示值
$graph->SetBackGroundImage ( );设置背景
$graph->SetMarginColor('lightblue');//设置图形颜色
$graph->SetShadow();//
$graph->Set3DPerspecttive(); //设置3d效果图

/*倾斜3D效果
1、'SKEW3D_UP'
2、'SKEW3D_DOWN'
3、'SKEW3D_LEFT'
4、'SKEW3D_RIGHT'
*/
$p1->SetTheme('water');//设置样式
$p1->SetCenter(0.5,0.55);//设置图形位置
$graph->legend->Pos(0.1,0.9);//设置注释文字的位置
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD,12);//设置注释文字的字体


本文案例下载https://pan.baidu.com/s/1GwRZe1-bCXpfKgUWZDpDPw   提取码:nci3


参考网址:

https://blog.csdn.net/luomao2012/article/details/72575408 

https://blog.csdn.net/yanlintao1/article/details/26056685 

http://www.php.cn/php-weizijiaocheng-354184.html

https://blog.csdn.net/liu826710/article/details/19909187


转载请注明: ITTXX.CN--分享互联网 » php进阶(一)--Jpgraph图形图像处理技术讲解

最后更新:2019-03-19 12:02:38

赞 (3) or 分享 ()
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽