liexusong
新手上路

UID 89192
精华
0
积分 40
帖子 20
金钱 40 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2007-3-11
状态 离线
|
一个动态生成的条型图片
<?
//设置图片基本属性
$GraphWidth = 400;
$GraphHeight = 200;
$GraphScale = 2;
$GraphFont = 5;
$GraphData = array("99","75","15","66","22");
$GraphLabel = array("Beef","Chicken","Pork","Lamb","Lobster");
//create image
$image = imagecreate($GraphWidth,$GraphHeight);
//allocate colors
$colorBody = imagecolorallocate($image,111,111,111);
$colorGrid = imagecolorallocate($image,222,222,222);
$colorBar = imagecolorallocate($image,123,123,123);
$colorText = imagecolorallocate($image,32,121,42);
//fill background
imagefill($image,0,0,$colorBody);
//draw vertical grid line
$GridLabelWidth = imagefontwidth($GraphFont) * 3 + 1;
imageline($image,$GridLabelWidth,0,$GridLabelWidth,$GraphHeight-1,$colorGrid);
//draw horizontal grid lines
for($index = 0 ; $index < $GraphHeight ; $index += $GrapgHeight / 10)
{
imagedashedline($image,0,$index,$GraphWidth - 1,$index,$colorGrid);
//draw label
imagestring($image,$GraphFont,0,$index,round(($GraphHeight - $index)/$GraphScale),$colorText);
}
//add bottom line
imageline($image,0,$GraphHeight -1,$GraphWidth - 1,$GraphHeight - 1,$colorGrid);
//draw each bar
$BarWidth = (($GraphWidth - $GridLabelWidth)/count($GraphData)) - 10;
for($index = 0 ; $index < count($GraphData);$index++){
//draw bar
$BarTopX = $GridLabelWidth +( ($index + 1) * 10)+ ($index * $BarWidth);
$BarBottomX = $BarTopX + $BarWidth;
$BarBottomY = $GraphHeight -1;
$BarTopY = $BarBottomY - ($GraphData[$index] * $GraphScale);
imagefilledrectangle($image,$BarTopX,$BarTopY,$BarBottomX,$BarBottomY,$colorBar);
//draw label
$LabelX = $BarTopX + (($BarBottomX - $BarTopX)/2) - (imagefontheight($GraphFont)/2);
$LabelY = $BarBottomY - 10;
imagestringup($image,$GraphFont,$LabelX,$LabelY,"$GraphLabel[$index] : $GraphData[$index]",$colorText);
}
//output image
header("content-type:image/png");
imagepng($image);
?>
|
|