A-A+

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

2012年07月06日 PHP技术文章 暂无评论 阅读 131 views 次

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

程序运行预览图:

<?   
//图片宽和高   
$w = isset($_GET&#91;'w'&#93;) ? intval($_GET&#91;'w'&#93;) : 400;   
$h = isset($_GET&#91;'h'&#93;) ? intval($_GET&#91;'h'&#93;) : 200;   
$title = $_GET&#91;'t'&#93;;   
//颜色库   
$color_arr = array(   
    array(0,0,255),   
    array(30,30,255),   
    array(60,60,255),   
    array(90,90,255),   
    array(120,120,255),   
    array(150,150,255),   
    array(180,180,255),   
    array(210,210,255),   
       
    array(0,255,0),   
    array(120,255,120),   
    array(150,255,150),   
    array(180,255,180),   
    array(210,255,210),   
       
    array(255,0,0),   
    array(255,30,30),   
    array(255,60,60),   
    array(255,90,90),   
    array(255,120,120),   
    array(255,150,150),   
    array(255,180,180),   
    array(255,210,210),   
       
    array(255,255,0),   
       
    array(255,0,255),   
    array(255,60,255),   
    array(255,90,255),   
    array(255,120,255),   
    array(255,150,255),   
    array(255,180,255),   
    array(255,210,255),   
       
    array(0,255,255),   
    array(90,255,255),   
    array(120,255,255),   
    array(150,255,255),   
    array(180,255,255),   
    array(210,255,255),   
       
    array(50,50,50),   
    array(70,70,70),   
    array(90,90,90),   
    array(110,110,110),   
    array(130,130,130),   
    array(150,150,150),   
    array(170,170,170),   
    array(190,190,190),   
    );   
//数值   
$points = isset($_GET&#91;'ps'&#93;) ? explode(',', $_GET&#91;'ps'&#93;) : arra
y(1,1,1);   
//数值对应名称   
$pts = isset($_GET&#91;'pts'&#93;) ? explode(',', $_GET&#91;'pts'&#93;) : arra
y('1','2','3');   
$cnt = count($points);   
//总值   
$total = 0;   
$tlen = 0;   
for ($i=0; $i<$cnt; $i++){   
    $total += $points&#91;$i&#93;;   
    $tlen = $tlen>strlen($pts[$i]) ? $tlen : strlen($pts[$i]);   
}   
//最短边   
$min_wh = ($w-2*($tlen+8)*ImageFontWidth(2))>($h-10) ? $h : ($w
-2*($tlen+8)*ImageFontWidth(2));   
  
//创建图片   
$im = imagecreate($w,$h);   
//图片背景色为白色   
$white = imagecolorallocate($im, 255, 255, 255);   
$black = imagecolorallocate($im, 0, 0, 0);   
ImageFill($im,0,0,$white);   
//圆心坐标   
$cx = $w/2;   
$cy = $h/2+20;   
//圆外接矩形的宽和高   
$cw = $min_wh-65;   
$ch = $min_wh-65;   
  
$start = 0;   
$end = 0;   
for ($i=0; $i<$cnt; $i++){   
    //扇形的颜色   
    $color = $i>=43 ? imagecolorallocate($im, rand(0,255), ran
d(0,255), rand(0,255)) : imagecolorallocate($im, $color_arr[$i
][0], $color_arr[$i][1], $color_arr[$i][2]);   
    $color = imagecolorallocate($im, $color_arr[$i][0], $color_a
rr[$i][1], $color_arr[$i][2]);   
    //起始角度和结束角度   
    $start = $end;   
    $end = $start + $points[$i]/$total*360;   
    imagefilledarc($im, $cx, $cy, $cw, $ch, $start, $end, $colo
r, IMG_ARC_PIE);   
    $notel1x = $cx+cos(deg2rad( $start/2+$end/2 ))*$cw/2;   
    $notel1y = $cy+sin(deg2rad( $start/2+$end/2 ))*$cw/2;   
    $notel2x = $cx+cos(deg2rad( $start/2+$end/2 ))*($cw/2+10);   
    $notel2y = $cy+sin(deg2rad( $start/2+$end/2 ))*($cw/2+10);   
    imageline($im,$notel1x,$notel1y,$notel2x,$notel2y,$black);   
    $str = strlen( $pts[$i] ) ? $pts[$i]."(".(substr($points[$
i]/$total*100,0,5))."%)" : ($i+1)."(".(substr($points[$i]/$to
tal*100,0,5))."%)";   
    if ($notel2x > $notel1x ) {   
        $notel3x = $notel2x+10;   
        $notestrx = $notel2x+10;   
    }else {   
        $notel3x = $notel2x-10;   
        $notestrx = $notel2x-10-ImageFontWidth(2)*strlen($str);   
    }   
    $notel3y = $notel2y;   
    $notestry = $notel2y-10;   
    imageline($im,$notel2x,$notel2y,$notel3x,$notel3y,$black);   
    imagestring($im,2,$notestrx,$notestry,$str,$black);   
}   
  
$wf5 = ImageFontWidth(5);   
$t_len = $wf5 * strlen($title);   
//echo $cw.' '.$ch;die();   
imagestring($im, 5, $w/2-$t_len/2, 5, $title, $black);   
// flush image   
header('Content-type: image/gif');   
imagegif($im);   
imagedestroy($im);   
?>
标签:

给我留言