php写统计投票数,php画投票统计图 - huolong的个人空间 - OSCHINA - 中文开源技术交流社区...
//Vote.class.php
/***********************************
*投票结果条形统计图
*
*@作者:王永强
*@时间 :2015年05月11日
*@邮箱:1442022614@qq.com
*
*注意:需要将$this->strTTF改成指定的字体文件路径地址(后缀名为.ttf)
**********************************/
class Vote{
//投票的对象
private $arrCandidate;
//票数和
private $nTotal;
//统计图标题
private $strTitle = 'the result of this voting';
//图像宽度
private $nWidth = 500;
//图像高度
private $nHeight;
//条形图宽度
private $nBarWidth;
//条形图高度
private $nBarHeight = 40;
//标题高度
private $nTitleHeight = 50;
//条形图间距离
private $nBarSpacing;
//标题大小
private $nTitleSize = 16;
//正文大小
private $nMainSize = 12;
//投票对象名称所占宽度
private $nNameWidth = 80;
//左右padding大小
private $nPadding = 10;
//条形图开始坐标
private $nX;
private $nY;
//部件间距
private $nIndent = 10;
//1%100的条形图长度
private $nBarUnit;
//百分比宽度
private $nPercentWidth = 50;
//票数宽度
private $nNumberWidth = 50;
//图像资源
private $srcImage;
//图像背景色
private $nBgColor;
//图像背景色rgb值
private $arrBgColor = array(255, 255, 255);
//文本色
private $nTextColor;
//文本色rgb值
private $arrTextColor = array(0, 0, 0);
//百分比颜色
private $nPercentColor;
//百分比颜色rgb值
private $arrPercentColor = array(0, 0, 0);
//直线颜色
private $nLineColor;
//直线颜色rgb值
private $arrLineColor = array(0, 0, 0);
//条形图颜色
private $nBarColor;
//条形图颜色rgb值
private $arrBarColor = array(0, 64, 128);
//票数颜色
private $nNumberColor;
//票数颜色rgb值
private $arrNumberColor = array(255, 78, 243);
//标题颜色
private $nTitleColor;
//标题颜色rgb值
private $arrTitleColor = array(255, 255, 255);
//名称颜色
private $nNameColor;
//名称颜色rgb值
private $arrNameColor = array(0, 0, 0);
//TrueTypeFont
private $strTTF = '/usr/share/fonts/truetype/fonts-japanese-gothic.ttf';
//输出图像类型
private $strImageType = 'png';
/**
*构造函数,初始化主要参数
*/
public function __construct($arrCandidate, $strTitle=null){
if (!isset($arrCandidate) || empty($arrCandidate)) {
die('必须传入投票的对象');
}
$this->arrCandidate = $arrCandidate;
if (isset($strTitle)) {
$this->strTitle;
}
//初始化参数
$this->init();
}
/**
*析构函数,销毁图像
*/
public function __destruct(){
if (!empty($this->srcImage)) {
imagedestroy($this->srcImage);
}
}
/**
*输出条形统计图
*
*@param $strFileUrl 将图像保存到该目录路径下,没有后缀名
*/
public function show($strFileUrl=null){
//更新成员变量
$this->init();
//画轮廓
imagefilledrectangle($this->srcImage, 0, 0, $this->nWidth, $this->nHeight, $this->nBgColor);
imagerectangle($this->srcImage, 0, 0, $this->nWidth-1, $this->nHeight-1, $this->nLineColor);
//画标题部分
$this->paintTitle();
//画名称部分
$this->paintName();
//画条形图部分
$this->paintBar();
//画百分比部分
$this->paintPercent();
//输出图像或保存图像
$imageFunc = 'image' . $this->strImageType;
if (isset($strFileUrl)) {
$imageFunc($this->srcImage, $strFileUrl . '.' . $this->strImageType);
echo 'you have paint the image to the path :', $strFileUrl, '.', $this->strImageType;
return ;
}
header('Content-type: image/' . $this->strImageType);
$imageFunc($this->srcImage);
}
/**
*设置成员变量时自动调用
*
*@param $strName 成员变量
*@param $mixArgs 参数
*/
public function __set($strName, $mixArgs){
$this->$strName = $mixArgs;
}
/**
*初始化参数(这些参数用户无法改变),,或者当设置了变量时,更新变量
*/
private function init(){
//宽度高度
$this->nBarSpacing = $this->nBarHeight / 2;
$this->nHeight = $this->nTitleHeight + count($this->arrCandidate) * ($this->nBarHeight + $this->nBarSpacing);
$this->nX = $this->nPadding + $this->nNameWidth + $this->nIndent;
$this->nY = $this->nTitleHeight;
$this->nBarWidth = $this->nWidth - $this->nX - $this->nPadding - $this->nPercentWidth;
$this->nBarUnit = $this->nBarWidth / 100;
//票数和
$this->nTotal = 0;
foreach ($this->arrCandidate as $value) {
$this->nTotal += $value['count'];
}
//实例化图像句柄
$this->srcImage = imagecreatetruecolor($this->nWidth, $this->nHeight);
//颜色
$this->nBgColor = imagecolorallocate($this->srcImage, $this->arrBgColor[0], $this->arrBgColor[1], $this->arrBgColor[2]);
$this->nTextColor = imagecolorallocate($this->srcImage, $this->arrTextColor[0], $this->arrTextColor[1], $this->arrTextColor[2]);
$this->nPercentColor = imagecolorallocate($this->srcImage, $this->arrPercentColor[0], $this->arrPercentColor[1], $this->arrPercentColor[2]);
$this->nLineColor =imagecolorallocate($this->srcImage, $this->nLineColor[0], $this->nLineColor[1], $this->nLineColor[2]);
$this->nBarColor = imagecolorallocate($this->srcImage, $this->arrBarColor[0], $this->arrBarColor[1], $this->arrBarColor[2]);
$this->nNumberColor = imagecolorallocate($this->srcImage, $this->arrNumberColor[0], $this->arrNumberColor[1], $this->arrNumberColor[2]);
$this->nNameColor = imagecolorallocate($this->srcImage, $this->arrNameColor[0], $this->arrNameColor[1], $this->arrNameColor[2]);
}
/**
*画标题部分
*/
private function paintTitle(){
$textBox = imagettfbbox($this->nTitleSize, 0, $this->strTTF, $this->strTitle);
$textWidth = $textBox[2] - $textBox[0];
$textHeight = abs($textBox[7] - $textBox[1]);
$textAboveLine = abs($textBox[7]);
$x = ($this->nWidth - $textWidth) / 2;
$y = ($this->nY - $textHeight) / 2 + $textAboveLine;
imagettftext($this->srcImage, $this->nTitleSize, 0, $x, $y, $this->nTitleColor, $this->strTTF, $this->strTitle);
}
/**
*画名称部分
*/
private function paintName(){
//循环画出所有名称
$y = $this->nY;
$x = $this->nPadding;
foreach ($this->arrCandidate as $value) {
$y += $this->nBarHeight / 2;
imagettftext($this->srcImage, $this->nMainSize, 0, $x, $y, $this->nNameColor, $this->strTTF, $value['name']);
$y += $this->nBarHeight / 2 + $this->nBarSpacing;
}
}
/**
*画条形图部分
*/
private function paintBar(){
$tempY = $this->nY;
foreach ($this->arrCandidate as $value) {
//着色部分
$x1 = $this->nX;
$y1 = $tempY;
$x2 = $this->nX + $this->nBarUnit * $value['count'] * 100 / $this->nTotal;
$y2 = $tempY + $this->nBarHeight;
imagefilledrectangle($this->srcImage, $x1, $y1, $x2, $y2, $this->nBarColor);
//条形图轮廓
$x2 = $this->nX + $this->nBarWidth;
$y2 = $tempY + $this->nBarHeight;
imagerectangle($this->srcImage, $x1, $y1, $x2, $y2, $this->nLineColor);
//票数部分
$x = $this->nX + $this->nBarWidth - $this->nNumberWidth;
$y = $tempY + $this->nBarHeight / 2;
imagettftext($this->srcImage, $this->nMainSize, 0, $x, $y, $this->nNumberColor, $this->strTTF, $value['count'] . '/' . $this->nTotal);
$tempY += $this->nBarHeight + $this->nBarSpacing;
}
}
/**
*画百分比部分
*/
private function paintPercent(){
$x = $this->nX + $this->nBarWidth + $this->nIndent;
$y = $this->nY;
foreach ($this->arrCandidate as $value) {
$percent = intval(100 * $value['count'] / $this->nTotal);
$y += $this->nBarHeight / 2;
imagettftext($this->srcImage, $this->nMainSize, 0, $x, $y, $this->nPercentColor, $this->strTTF, $percent . '%');
$y += $this->nBarHeight / 2 + $this->nBarSpacing;
}
}
}
?>
///test.php
require('Vote.class.php');
$data = array(
array(
'name' => 'zhansan',
'count' => 14
),
array(
'name' => 'lisi',
'count' => 16
),
array(
'name' => 'wangwu',
'count' => 20
)
);
$vote = new Vote($data);
$vote->strTitle = 'the result of this voting';
$vote->arrBgColor = array(33, 33, 33);
//输出到指定文件
//$vote->show('./gd');
//直接输出到浏览器
$vote->show();
?>