频道栏目
首页 > 资讯 > 文件处理 > 正文

php GD库饼状图生成接口源码

16-01-06        来源:[db:作者]  
收藏   我要投稿

 GD库是一个php处理图像图片的扩展库,GD库提供了一系列的用来处理图图片的API,使用GD库可以处理图片,或者生成图片,在网站上GD库通常用来生成缩略图,或者对图片加水印等,对于目前最新版的PHP版本来说,生成缩略图或加水印基本上已经用不上专门的GD库来处理了,php可以直接处理简单的图片应用,比如提到的处理图片缩略图,为图片加水印,图片验证码等,而目前PHP 的 GD 图最多的应用应该是饼状图或者是统计图等,下面是一个php GD库饼状图生成的一个接口源码,直接将其运行,可以生成一个蓝色为主的饼状图。

PHP代码
  1. <?   
  2. //图片宽和高   
  3. $w = isset($_GET['w']) ? intval($_GET['w']) : 400;   
  4. $h = isset($_GET['h']) ? intval($_GET['h']) : 200;   
  5. $title = $_GET['t'];   
  6. //颜色库   
  7. $color_arr = array(   
  8.     array(0,0,255),   
  9.     array(30,30,255),   
  10.     array(60,60,255),   
  11.     array(90,90,255),   
  12.     array(120,120,255),   
  13.     array(150,150,255),   
  14.     array(180,180,255),   
  15.     array(210,210,255),         
  16.     array(0,255,0),   
  17.     array(120,255,120),   
  18.     array(150,255,150),   
  19.     array(180,255,180),   
  20.     array(210,255,210),          
  21.     array(255,0,0),   
  22.     array(255,30,30),   
  23.     array(255,60,60),   
  24.     array(255,90,90),   
  25.     array(255,120,120),   
  26.     array(255,150,150),   
  27.     array(255,180,180),   
  28.     array(255,210,210),        
  29.     array(255,255,0),          
  30.     array(255,0,255),   
  31.     array(255,60,255),   
  32.     array(255,90,255),   
  33.     array(255,120,255),   
  34.     array(255,150,255),   
  35.     array(255,180,255),   
  36.     array(255,210,255),          
  37.     array(0,255,255),   
  38.     array(90,255,255),   
  39.     array(120,255,255),   
  40.     array(150,255,255),   
  41.     array(180,255,255),   
  42.     array(210,255,255),   
  43.     array(50,50,50),   
  44.     array(70,70,70),   
  45.     array(90,90,90),   
  46.     array(110,110,110),   
  47.     array(130,130,130),   
  48.     array(150,150,150),   
  49.     array(170,170,170),   
  50.     array(190,190,190),   
  51.     );   
  52. //数值   
  53. $points = isset($_GET['ps']) ? explode(',', $_GET['ps']) : array(1,1,1);   
  54. //数值对应名称   
  55. $pts = isset($_GET['pts']) ? explode(',', $_GET['pts']) : array('1','2','3');   
  56. $cnt = count($points);   
  57. //总值   
  58. $total = 0;   
  59. $tlen = 0;   
  60. for ($i=0; $i<$cnt; $i++){   
  61.     $total += $points[$i];   
  62.     $tlen = $tlen>strlen($pts[$i]) ? $tlen : strlen($pts[$i]);   
  63. }   
  64. //最短边   
  65. $min_wh = ($w-2*($tlen+8)*ImageFontWidth(2))>($h-10) ? $h : ($w-2*($tlen+8)*ImageFontWidth(2));   
  66.   
  67. //创建图片   
  68. $im = imagecreate($w,$h);   
  69. //图片背景色为白色   
  70. $white = imagecolorallocate($im, 255, 255, 255);   
  71. $black = imagecolorallocate($im, 0, 0, 0);   
  72. ImageFill($im,0,0,$white);   
  73. //圆心坐标   
  74. $cx = $w/2;   
  75. $cy = $h/2+20;   
  76. //圆外接矩形的宽和高   
  77. $cw = $min_wh-65;   
  78. $ch = $min_wh-65;   
  79.   
  80. $start = 0;   
  81. $end = 0;   
  82. for ($i=0; $i<$cnt; $i++){   
  83.     //扇形的颜色   
  84.     $color = $i>=43 ? imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)) : imagecolorallocate($im, $color_arr[$i][0], $color_arr[$i][1], $color_arr[$i][2]);   
  85.     $color = imagecolorallocate($im, $color_arr[$i][0], $color_arr[$i][1], $color_arr[$i][2]);   
  86.     //起始角度和结束角度   
  87.     $start = $end;   
  88.     $end = $start + $points[$i]/$total*360;   
  89.     imagefilledarc($im, $cx, $cy, $cw, $ch, $start, $end, $color, IMG_ARC_PIE);   
  90.     $notel1x = $cx+cos(deg2rad( $start/2+$end/2 ))*$cw/2;   
  91.     $notel1y = $cy+sin(deg2rad( $start/2+$end/2 ))*$cw/2;   
  92.     $notel2x = $cx+cos(deg2rad( $start/2+$end/2 ))*($cw/2+10);   
  93.     $notel2y = $cy+sin(deg2rad( $start/2+$end/2 ))*($cw/2+10);   
  94.     imageline($im,$notel1x,$notel1y,$notel2x,$notel2y,$black);   
  95.     $str = strlen( $pts[$i] ) ? $pts[$i]."(".(substr($points[$i]/$total*100,0,5))."%)" : ($i+1)."(".(substr($points[$i]/$total*100,0,5))."%)";   
  96.     if ($notel2x > $notel1x ) {   
  97.         $notel3x = $notel2x+10;   
  98.         $notestrx = $notel2x+10;   
  99.     }else {   
  100.         $notel3x = $notel2x-10;   
  101.         $notestrx = $notel2x-10-ImageFontWidth(2)*strlen($str);   
  102.     }   
  103.     $notel3y = $notel2y;   
  104.     $notestry = $notel2y-10;   
  105.     imageline($im,$notel2x,$notel2y,$notel3x,$notel3y,$black);   
  106.     imagestring($im,2,$notestrx,$notestry,$str,$black);   
  107. }   
  108.   
  109. $wf5 = ImageFontWidth(5);   
  110. $t_len = $wf5 * strlen($title);   
  111. //echo $cw.' '.$ch;die();   
  112. imagestring($im, 5, $w/2-$t_len/2, 5, $title, $black);   
  113. // flush image   
  114. header('Content-type: image/gif');   
  115. imagegif($im);   
  116. imagedestroy($im);   
  117. ?>  

相关TAG标签
上一篇:各种网站进度条的图片汇集
下一篇:php文件搜索函数、文件搜索类
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站