php报表 jpgraph柱状图实例

 更新时间:2016年11月25日 16:57  点击:1642
jpgraph是php教程中一个非常非常强大的第三方报表工具,据说能完成一切你想要的图形…
新手初识jpgraph肯定会遇到各种各样的问题,比如乱码什么的,本案例是jpgraph3.0.7制作,也经过本人的多次实验,解决乱码问题
<?php

$datay=array(); //纵坐标数据

$datax=array(); //横坐标数据

foreach ($usernums as $key => $value){
$datay[] = $value;
$datax[] = $userids[$key];
}

require_once (‘jpgraph-3.0.7/jpgraph/jpgraph.php’);
require_once (‘jpgraph-3.0.7/jpgraph/jpgraph_bar.php’);

// Create the graph. These two calls are always required
$graph = new Graph(800,600); //图像高宽
$graph->SetScale(“textlin”);

$graph->xaxis->SetTickLabels($datax);

$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(30);

$graph->yaxis->scale->SetGrace(20);
$graph->xaxis->scale->SetGrace(20);
// Add a drop shadow
$graph->SetShadow();

// Adjust the margin a bit to make more room for titles
$graph->img->SetMargin(40,30,20,40);

// Create a bar pot
$bplot = new BarPlot($datay);

// Adjust fill color
$bplot->SetFillColor(‘orange’);
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD,10);
$bplot->value->SetAngle(45);
$bplot->value->SetFormat(‘%d’);
$graph->Add($bplot);

// Setup the titles
$graph->title->Set(iconv(“UTF-8″, “gb2312″,”用户消费报表图”));
$graph->xaxis->title->Set(iconv(“UTF-8″, “gb2312″,”用户姓名”));
$graph->yaxis->title->Set(iconv(“UTF-8″, “gb2312″,”用户订单数量”));

$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);

// Display the graph
$graph->Stroke();

?>
  

效果图:
说 明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度 * 返 回 值:如果创建成功返回文件保存的地址,否则返回false

 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false

<?php教程
/************************************************************************
* 函数名称:createSmallImg()
* 函数说明:创建等比例图片
* 输入参数:
 $dir 保存路径
 $source_img 原图片名称
 $small_ex 缩率图文件名后缀
 $maxwidth 最大宽度
 $maxheight 最大高度
* 说    明:$maxwidth和$maxheight只能传递一个,如果传最大宽度将自动计算高度,如果传最大高度将自动计算宽度
* 返 回 值:如果创建成功返回文件保存的地址,否则返回false
* 编 写 者:李小宇
* 编写时间:2011/8/18
**************************************************************************/
function createSmallImg($dir,$source_img,$small_ex="_s",$maxwidth='',$maxheight='') {
 if(!empty($maxwidth) && !empty($maxheight)) {
  return false;
 }
 $img_name=substr($source_img,0,-4);
 $img_ex = strtolower(substr(strrchr($source_img,"."),1));
 /*注释的这段用作直接在浏览器上显示图片
 $im=imagecreatefromjpeg($file);
 header("Content-type: image/jpeg");
 imagejpeg($im);*/
 switch($img_ex) {
  case "jpg":
   $src_img=imagecreatefromjpeg($dir.$source_img);
   break;
  case "gif":
   $src_img=imagecreatefromgif($dir.$source_img);
   break;
  case "png":
   $src_img=imagecreatefrompng($dir.$source_img);
   break;
 }
 $old_width=imagesx($src_img);
 $old_height=imagesy($src_img);
 if(!empty($maxheight) && $old_height>=$maxheight) {
  $new_height=$maxheight;
  $new_width=round(($old_width*$new_height)/$old_height);
 } elseif(!empty($maxwidth) && $old_width>=$maxwidth) {
  $new_width=$maxwidth;
  $new_height=round(($old_height*$new_width)/$old_width);
 }
 if(!empty($new_width) || !empty($new_height)) {
  if($img_ex=="jpg" || $img_ex=="png") {
   $dst_img=imagecreatetruecolor($new_width,$new_height);
  } else {
   $dst_img=imagecreate($new_width,$new_height);
  }
  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);
  $smallname=$dir.$img_name.$small_ex.".".$img_ex;
  switch($img_ex) {
   case "jpg":
    imagejpeg($dst_img,$smallname,100);
    break;
   case "gif":
    imagegif($dst_img,$smallname);
    break;
   case "png":
    imagepng($dst_img,$smallname);
    break;
  }
 }
 return $smallname;
}

等比例生成缩略图的php程序 这个程序很实现,但只是用来进来等比例生成缩略图哦,你要把文件上传到服务器,然后再由此函数来操作,有需要的朋友参考一下。

等比例生成缩略图的php教程程序
这个程序很实现,但只是用来进来等比例生成缩略图哦,你要把文件上传到服务器,然后再由此函数来操作,有需要的朋友参考一下。

function reSizeImg($imgSrc, $resize_width, $resize_height, $isCut=false) {
 //图片的类型
 $type = substr ( strrchr ( $imgSrc, "." ), 1 );
 //初始化图象
 if ($type == "jpg") {
  $im = imagecreatefromjpeg ( $imgSrc );
 }
 if ($type == "gif") {
  $im = imagecreatefromgif ( $imgSrc );
 }
 if ($type == "png") {
  $im = imagecreatefrompng ( $imgSrc );
 }
 //目标图象地址
 $full_length = strlen ( $imgSrc );
 $type_length = strlen ( $type );
 $name_length = $full_length - $type_length;
 $name = substr ( $imgSrc, 0, $name_length - 1 );
 $dstimg = $name . "_s." . $type;

 $width = imagesx ( $im );
 $height = imagesy ( $im );

 //生成图象
 //改变后的图象的比例
 $resize_ratio = ($resize_width) / ($resize_height);
 //实际图象的比例
 $ratio = ($width) / ($height);
 if (($isCut) == 1) //裁图
  {
  if ($ratio >= $resize_ratio) //高度优先
  {
   $newimg = imagecreatetruecolor ( $resize_width, $resize_height );
   imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, (($height) * $resize_ratio), $height );
   ImageJpeg ( $newimg, $dstimg );
  }
  if ($ratio < $resize_ratio) //宽度优先
  {
   $newimg = imagecreatetruecolor ( $resize_width, $resize_height );
   imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $resize_ratio) );
   ImageJpeg ( $newimg, $dstimg );
  }
 } else //不裁图
 {
  if ($ratio >= $resize_ratio) {
   $newimg = imagecreatetruecolor ( $resize_width, ($resize_width) / $ratio );
   imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, ($resize_width) / $ratio, $width, $height );
   ImageJpeg ( $newimg, $dstimg );
  }
  if ($ratio < $resize_ratio) {
   $newimg = imagecreatetruecolor ( ($resize_height) * $ratio, $resize_height );
   imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, ($resize_height) * $ratio, $resize_height, $width, $height );
   ImageJpeg ( $newimg, $dstimg );
  }
 }
 ImageDestroy ( $im );
}

调用方法简单,直接reSizeImg就可以了,参考很简单。

验证码在php中是经常会用到了,下面我们为你提供了一些关于php验证码生成类与程序,直接复制下去就可以直接用的哦,下面看代码。

验证码在php教程中是经常会用到了,下面我们为你提供了一些关于php验证码生成类与程序,直接复制下去就可以直接用的哦,下面看代码。

<?php
session_start();
Header("Content-type: image/gif");
class SecurityCode
{
private $codes = '';
function __construct()
{
$code = '0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z';
$codeArray = explode('-',$code);
shuffle($codeArray);
$this->codes = implode('',array_slice($codeArray,0,4));
}
public function CreateImg()
{
$_SESSION['check_pic'] = $this->codes;
$img = imagecreate(70,25);
imagecolorallocate($img,222,222,222);
$testcolor1 = imagecolorallocate($img,255,0,0);
$testcolor2 = imagecolorallocate($img,51,51,51);
$testcolor3 = imagecolorallocate($img,0,0,255);
$testcolor4 = imagecolorallocate($img,255,0,255);
for ($i = 0; $i < 4; $i++)
{
imagestring($img,rand(5,6),8 + $i * 15,rand(2,8),$this->codes[$i],rand(1,4));
}
imagegif($img);
}
}
$code = new SecurityCode();
$code->CreateImg();
$code = NULL;
?>

代码二

 

<?
session_start();
for($i=0; $i<4; $i++){
$rand.= dechex(rand(1,15));
}
$_SESSION[check_pic]=$rand;
//echo $_SESSION[check_pic];
// 设置图片大小">图片大小
$im = imagecreatetruecolor(100,30);
// 设置颜色
$bg=imagecolorallocate($im,0,0,0);
$te=imagecolorallocate($im,255,255,255);
// 把字符串写在图像左上角
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
// 输出图像
header("Content-type:image/jpeg");
imagejpeg($im);
?>

<?
$handle = opendir('./'); //当前目录
while (false !== ($file = readdir($handle))) { //遍历该php教程文件所在目录
list($filesname,$kzm)=explode(".",$file);//获取扩展名
if ($kzm=="gif" or $kzm=="jpg") { //文件过滤
if (!is_dir('./'.$file)) {  //文件夹过滤
$array[]=$file;//把符合条件的文件名存入数组
}
}
}
$suiji=array_rand($array); //使用array_rand函数从数组中随机抽出一个单元
?>
<img src="<?=$array[$suiji]?>">

实例二

<?php
/**********************************************
* Filename : img.php
* Author : freemouse
* web : www.111cn.net * email :freemouse1981@gmail.com
* Date : 2010/12/27
* Usage:
* <img src=img.php>
* <img src=img.php?folder=images2/>
***********************************************/
if($_GET['folder']){
$folder=$_GET['folder'];
}else{
$folder='/images/';
}
//存放图片文件的位置
$path = $_SERVER['DOCUMENT_ROOT']."/".$folder;
$files=array();
if ($handle=opendir("$path")) {
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file;
}
}
}
closedir($handle);

$random=rand(0,count($files)-1);
if(substr($files[$random],-3)=='gif') header("Content-type: image/gif");
elseif(substr($files[$random],-3)=='jpg') header("Content-type: image/jpeg");
readfile("$path/$files[$random]");
?>


readrand.php(此程序实际上是生成一句网页特效语言)
<?
$arrayall=file("tp.txt");读出tp.txt内容到数组
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//设定随机数种子
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr(9),$arrayall[$selectrand]);//从全部中随机取出一个并分割
?>
document.write('<a href="<? echo $exstr[1];?>" target="new"><img src="<? echo $exstr[2];?>" width="200" height="50" alt="<? echo $exstr[0];?>" ></a>');


HTML文件
<html>
<body>
<script language='javascript' src='readrand.php'>
</script>
</body>
</html>


(你可以把scripty放到你需要的位置,并可以加入setTimeout()函数以实现定时刷新)

随机广告代码

<?php
  #########随机广告显示##########
  function myads(){
  $dir="ads"; #设置存放记录的目录
  //$dir="ads"; #设置存放记录的目录
  $ads="$dir/ads.txt"; #设置广告代码文件
  $log ="$dir/ads.log"; #设置ip记录文件
  
  $ads_lines=file($ads);
  $lines=count($ads_lines);#文件总行数
  
  ####读出广告总数$ads_count和显示次数到数组$display_array########
  $ads_count=0;
  $display_count=0;
  for ($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
   $ads_count+=1;
   $display_array[$ads_count]=substr($ads_lines[$i],8);
   $display_count+=$display_array[$ads_count];
   }
  }
  ####决定随机显示序号$display_rand#####
  srand((double)microtime()*1000000);
  $display_rand = rand(1,$display_count);
  
  ###决定广告序号$ads_num######
  $pricount=0;
  $ads_num=1;
  for($i=1; $i<=$ads_count; $i++) {
   $pricount += $display_array[$i];
   if ($display_rand<=$pricount) {$ads_num=$i;break;}
  }
  
  #####播放广告########
  $num=0;
  $flag=0;
  
  for($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){$num++;}
   if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
   if(($flag==1)and strcmp($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
   if(($flag==1)and(!(strcmp($ads_lines[$i][0],"#")))){break;}
  }
  ####纪录广告显示次数#########
  $fp=fopen($log,"a");
  fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
  fclose($fp);
  }
  ?>


广告代码文件ads.txt

以下为引用的内容:
  ########每个广告代码之间用'#'隔开,display为显示加权数,越大显示次数越多######
  display=10   
  <a href="广告1连接地址">
  <img src=/images/banner/webjxcomad1.gif" alt="广告1"> </a>
  ################################
  display=10   
  <a href="广告2连接地址" target=_blank>
  <img src=images/banner/webjxcomad2.gif" width="468" height="60" alt="广告2" border="0"></a> 

[!--infotagslink--]

相关文章

  • 快速理解MySQL中主键与外键的实例教程

    主键与外键的关系,通俗点儿讲,我现在有一个论坛,有两张表,一张是主贴 thread,一张是回帖 reply先说说主键,主键是表里面唯一识别记录的字段,一般是帖子id,体现在访问的时候,例如是 thread.php&#63;id=1 表示我要访问的是帖子...2015-11-24
  • yii添删改查实例

    一、数据访问对象 (DAO)YiiDAO 基于 PHP Data Objects (PDO) 构建。它是一个为众多流行的DBMS提供统一数据访问的扩展,这些 DBMS 包括MySQL, PostgreSQL 等等。因此,要使用 Yii DAO,PDO 扩展和特定的 PDO 数据库驱动(例如...2015-11-24
  • C#在Winform开发中使用Grid++报表

    这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • c# 接口使用实例

    这篇文章主要介绍了c#接口使用的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-17
  • C#中的IEnumerable简介及简单实现实例

    这篇文章主要介绍了C#中的IEnumerable简介及简单实现实例,本文讲解了IEnumerable一些知识并给出了一个简单的实现,需要的朋友可以参考下...2020-06-25
  • vue实例的选项总结

    这篇文章主要介绍了Vue实例的选项有哪些,文中讲解非常细致,代码帮助大家更好的学习,感兴趣的朋友可以了解下...2020-06-10
  • php socket讲解与实例

    在这一章里你将了解到迷人而又让人容易糊涂的套接字(Sockets)。Sockets在PHP中是没有充分利用的功能。今天你将看到产生一个能使用客户端连接的服务器,并在客户端使用sock...2016-11-25
  • Vue实例简单方法介绍

    这篇文章主要为大家详细介绍了Vue实例的一些简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
  • C++内核对象封装单实例启动程序的类

    这篇文章主要介绍了利用C++内核对象封装的类,程序只能运行单个实例,可防止多次启动,大家参考使用吧...2020-04-25
  • php 中缓冲输出实例代码

    ob_start([string output_callback])- 打开输出缓冲区   所有的输出信息不在直接发送到浏览器,而是保存在输出缓冲区里面,可选得回调函数用于处理输出结果信息。 ob...2016-11-25
  • JpGraph中文乱码处理办法

    打开jpgraph.php教程文件,找到 private $font_family=ff_font1,$font_style=fs_normal,$font_size=12; 用 private $font_family=ff_simsun,$font_style=fs_normal,$...2016-11-25
  • C# 委托(跨窗体操作控件)实例流程讲解

    今天研究了一下,在C#里面却是可以不用自定义消息这么复杂的方法来实现跨窗体调用控件,C#有更好的办法就是委托。...2020-06-25
  • PHP支持断点续传实例代码

    下面我们来介绍一下关于php实现断点续传的代码,有需要学习的朋友可参考一下。 让PHP下载代码支持断点续传 主要靠的 HTTP协议中header Content-Range来实现 先来...2016-11-25
  • 同时ping多个ip找了最快的ip网的php实例

    本文我们来分享一段可以同时ping多个ip然后对比找出网络最快的ip的php程序,这段程序用来找代理ip可是神器。 为了翻墙方便 ,买了个vpn,转到osx下面官方没有提供合...2016-11-25
  • 举例说明JavaScript中的实例对象与原型对象

    这篇文章主要介绍了JavaScript中的实例对象与原型对象,针对constructor属性和prototype属性展开来讲,需要的朋友可以参考下...2016-03-12
  • PHP中获取星期几实例程序

    在php中我们要获取今天是星期几可以直接使用date函数来操作,下面我来给各位同学总结一下具体的操作方法,希望此方法对各位朋友有帮助。 PHP中获取星期方法大全简单...2016-11-25
  • python3中类的重点与难点:类属性和实例属性的区别说明

    这篇文章主要介绍了python3中类的重点与难点:类属性和实例属性的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-02
  • ireport数据表格报表的简单使用

    这篇文章给大家介绍了如何画一个报表模板,这里介绍下画表格需要用到的组件,文中通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧...2021-10-26
  • 快速实现MySQL的部署以及一机多实例部署

    这篇文章主要为大家详细介绍了快速实现MySQL的部署以及一机多实例部署的相关资料,感兴趣的小伙伴们可以参考一下...2016-04-22
  • C C++ 算法实例大全

    这篇文章主要介绍了C C++ 算法实例大全,里面大量的实例介绍,学习c语言的朋友可以收藏...2020-04-25