GD库的使用

一、创建一个简单的图像

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/simple/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); //结束图形,释放资源

imagettffext( )函数中的参数:$im表示图片,20表示字体大小,0表示旋转角度,10和140表示字体坐标

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

header("content-type:image/png");
$image_width = 70;
$image_height = 18;
srand(microtime()*100000);
$array = Array();
for($i=0; $i<4;$i++){
   $array[$i]=dechex(rand(0, 15));
}
$num_image = imagecreate($image_width, $image_height);
imagecolorallocate($num_image, 255,255, 255);
for($i=0; $i<count($array);$i++){
    $font = mt_rand(3,5);
    $x = mt_rand(1,8)+$image_width*$i/4;
    $y = mt_rand(1, $image_height/4);
    $color = imagecolorallocate($num_image,mt_rand(0,100), mt_rand(0,150),mt_rand(0,200));
    imagestring($num_image, $font,$x, $y, $array[$i],$color);
}
imagepng($num_image);
imagedestroy($num_image);

四、绘制折线图

使用前的配置:

  1. 打开PHP.ini文件,定位到extension=php_gd2.dll,将前面的分号删除
  2. 下载Jpgraph文件压缩包,将解压后的文件夹放到”D:\wampserve\wamp\www“中
  3. 打开PHP.ini文件,修改include_path参数,增加Jpgraph的路径,如:‘include_path="D:\wampserve\wamp\www\Jpgraph"’,保存PHP.ini文件,重新启动服务。
include ("jpgraph/src/jpgraph.php");
include ('jpgraph/src/jpgraph_line.php');
$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();

五、绘制柱状图

include ("jpgraph/src/jpgraph.php");
include ('jpgraph/src/jpgraph_bar.php');
// 这是柱状图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 = "Red apple sales";
$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();

六、绘制饼图

include ("jpgraph/src/jpgraph.php");
include ('jpgraph/src/jpgraph_pie.php');
include ('jpgraph/src/jpgraph_pie3d.php');

$data = array(266036, 295621, 335851, 254256, 254254, 685425);
$graph=new PieGraph(800,500);
$graph->SetShadow("false");
//设置图像边界范围
$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();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值