建立文件Gd.php

<?php
//获取数据
$shuju=split(",",$_GET['shuju']);
//定义画布宽度
$tukuan=300;
//定义画布高度
$tugao=300;
//定义角度数组
$jiaodu = array();
//创建画布
$image = imagecreate($tukuan,$tugao);
//计算数据总和
for($i=0;$i<count($shuju);$i++){
$total += $shuju[$i];
}
//再次遍历,计算色块角度并存入数组
for($i=0;$i<count($shuju);$i++){
array_push($jiaodu,round(360*(int)$shuju[$i]/$total));
}
//定义扇形颜色
$color[0] = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$color[1] = imagecolorallocate($image, 194, 214, 91);
$color[2] = imagecolorallocate($image, 232, 217, 64);
$color[3] = imagecolorallocate($image, 162, 166, 169);
$color[4] = imagecolorallocate($image, 109, 194, 223);
$color[5] = imagecolorallocate($image, 192, 219, 240);
$color[6] = imagecolorallocate($image, 231, 159, 183);
$colorline= imagecolorallocate($image, 0, 139, 103);
//定义文本颜色
$colortext= imagecolorallocate($image, 255, 0, 0);

//圆心横坐标
$yx=($tukuan)/2;
//圆心纵坐标
$yh=($tugao)/2;
//椭圆的宽
$w=$tukuan/2;
//椭圆的高
$h=$tugao/2;
//定义开始初始角度
$kaishi=0;
//定义结束初始角度
$jieshu=0;
for ($i = 0; $i < count($shuju); $i++) {
$kaishi=$kaishi+0;
$jieshu=$kaishi+$jiaodu[$i];
//循环画椭圆并填充
imagefilledarc($image, $yx, $yh, $w, $h, $kaishi, $jieshu, $color[$i]+1, IMG_ARC_PIE);
//找到扇形弧的中点坐标
$linestartx = $yx+($w/2)*cos((pi()/180)*((($jieshu-$kaishi)/2)+$kaishi));
$linestarty = $yh+($w/2)*sin((pi()/180)*((($jieshu-$kaishi)/2) +$kaishi));
$lineendx = $yx+($w/2+10)*cos((pi()/180)*((($jieshu-$kaishi)/2)+$kaishi));
$lineendy = $yh+($w/2+30)*sin((pi()/180)*((($jieshu-$kaishi)/2) +$kaishi));
if($lineendx >= $yx)
{
  $line2x = $lineendx+50;
  $aaa=$line2x-$lineendx-50;
}
else
{
  $line2x = $lineendx-50;
  $aaa=$line2x-$lineendx;
}
imageline($image,$linestartx,$linestarty,$lineendx,$lineendy,$color[$i]+1);
imageline($image,$lineendx,$lineendy,$line2x,$lineendy,$color[$i]+1);
$baifenbi=round($jiaodu[$i]/360*100, 0)."%";
imagestring($image,4,$lineendx + $aaa,$lineendy-20,"$baifenbi($shuju[$i])",$colortext);
$kaishi+=$jiaodu[$i];
$jieshu+=$jiaodu[$i];
}
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

调用页面pic.php

<img src="Gd.php?shuju=33,22,22,45,25,76"/>
  • Tags:

发表评论/留言

为了您的身体健康、潇洒、美丽、帅气,请不要灌水。

/ 快捷键:Ctrl+Enter