网站有演示,可以去看看
调用方法
for ($i = 0; $i < 22; $i++) {
$data[] = [
$i,$i,$i,$i,$i%10?0:false,$i,$i,$i,$i%10?0:null,$i,$i,$i
];
}
createTableImgHelper::createImage($data, [], '', 12, '#444', '#999', '#f9f9f9');
createTableImgHelper::echoImage();exit;
帖两张效果图
<?php
/*
* Copyright (c) 2018-2020 http://www.levme.com All rights reserved.
*
* 创建时间:2020-09-11 12:53
*
* 项目:basic-2.0.26 - $ - createTable.php
*
* 作者:liwei Levme.com <675049572@qq.com>
*/
namespace app\modules\lotterys\widgets\lottimgWidget;
use app\gii\generators\crud\Generator;
use app\modules\lotterys\tjLotteryHelper;
use levmecom\aalevme\levHelpers;
use yii\helpers\ArrayHelper;
class createTableImgHelper extends createChartImg {}
class createChartImg
{
public static $font = __DIR__ . '/static/ttf/simsun.ttf';
public static $im = null;
public static $imgWidth = null;//图像宽高自动计算,你只能通过调节$leftPadding $upPadding开改变它
public static $imgHeight = null;
/**
* false=空行=rowspan 表示占行(与下一行对应格合并)
* null=空列=colspan 表示占列(与下一列对应格合并)
* null与false不能相阾,因为不科学
* @param array $data
* @param array $dataColor [2=>[2=>['bg'=>[2,levHelpers::hexToRgb('F4606C'),['r'=>255,'g'=>255,'b'=>255]]]]]
* @param string $title
* @param int $txtSize
* @param string $bgColor
* @param string $brColor
* @param string $txtColor
* @param int $calibHeight
* @param int $outLine
* @param int $margin
* @param int $border
* @param int $leftPadding
* @param int $upPadding
* @param int $titleSize
* @param bool $align
* @param string $font
*/
public static function createImage($data, $dataColor = [], $title = '', $txtSize = 20, $bgColor = '', $brColor = '', $txtColor = '', $pointLine = ['ff9600', 2], $calibHeight = 0, $outLine = 2, $margin = 5, $border = 1, $leftPadding = 0, $upPadding = 0, $titleSize = 0, $align = true, $font = '') {
// $data = [
// [false, '好列', null, null, '占两列', null, '占两列', false],//false 表示占行(与下一行对应格合并)
// ['2', null, '占两列', null, '占两列', '占两列', '占两行', false],//null 表示占列(与下一列对应格合并)
// [false, '好列', '1', '好列',null, '占两列', false, false], //null与false不能相阾,因为不科学
// ['好列', '好列', '好列',null, '占两列', '占两列', '12345', false],
// [null, '占两列', null, '占两列', '好列', '占两列', '好列', '好列'],
// [null, '占两列', null, '占两列', '好列', '占两列', '好列', '好列'],
// [null, '占两列', null, '占两列', '好列', '占两列', '好列', '好列'],
// [null, '占两列', null, '占两列', '好列', '占两列', '好列', '好列'],
// [null, '占两列', null, '占两列', '好列', '占两列', '好列', '好列'],
// ];
// $dataColor = [
// 1=>[
// 2=>[
// 'bg'=>[
// 2,//1:整个td格背景;2:椭圆形背景
// levHelpers::hexToRgb('fff'),//td背景色
// ['r'=>255,'g'=>0,'b'=>0],//td字体色
// 'point' => [2,2],//连线点(行号,列号)
// ],
// ]
// ],
// 2=>[
// 2=>[
// 'bg'=>[
// 2,
// levHelpers::hexToRgb('F4606C'),
// ['r'=>255,'g'=>255,'b'=>255],
// 'point' => [],
// ],
// ]
// ],
// ];
ini_set('memory_limit', '10240M');
$font = $font ?: static::$font;
$data && $data = array_values($data);
$dataRow = count($data);
$upPadding = $upPadding ?: round($txtSize / 1.6);//上下内边距
$leftPadding = $leftPadding ?: round($txtSize / 1.6);//左内边距
$titleSize = $titleSize ?: round($txtSize * 1.2);//标题字体大小
$pointBorder = $pointLine[1] ?: $border;
$rowHeight = $txtSize + 2 + $upPadding*2 + $border;//行高
//计算标题写入起始位置
if ($title) {
$ttfBox = imagettfbbox($titleSize, 0, $font, $title);
$titleWidth = $ttfBox[2] - $ttfBox[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
$titleHeight = $titleSize + 2 + $upPadding*2;
}else {
$titleWidth = $titleHeight = 0;
}
foreach ($data as $dk => $v) {
$k = -1;
foreach ($v as $r) {
$k++;
if ($r !== null && $r !== false) {
$ttfBox = imagettfbbox($txtSize, 0, $font, $r);
$colWidth[$k][] = $ttfBox[2] - $ttfBox[0] + $leftPadding*2 + $border;
}else {
$colWidth[$k][] = 0;
}
}
}
//取最大文字宽度
foreach ($colWidth as $k => $v) {
$colMaxWidth[$k] = max($v);
}//print_r($colMaxWidth);
$imgWidth = array_sum($colMaxWidth) + $margin*2 + $outLine*2 - $border;//图片宽度
$imgHeight = $rowHeight * $dataRow + $margin*2 + $titleHeight + $outLine*2 - $border;//图片高度
static::$imgWidth = $imgWidth;
static::$imgHeight = $imgHeight;
$xCol = $margin + $outLine;
$yRow = $margin + $outLine + $titleHeight;
$columnLines = [];
foreach ($colMaxWidth as $k => $v) {
$columnLines[$k] = array_sum(array_slice($colMaxWidth, 0, $k+1));
}
$colors = tjLotteryHelper::colors(true);
$pointLineColor = $pointLine[0] ? levHelpers::hexToRgb($pointLine[0]) : $colors[29];
$bgColor = $bgColor ? levHelpers::hexToRgb($bgColor) : $colors[17];
$brColor = $brColor ? levHelpers::hexToRgb($brColor) : $colors[16];
$txtColor = $txtColor ? levHelpers::hexToRgb($txtColor) : $colors[16];//print_r($txtColor);
$img = imagecreatetruecolor($imgWidth, $imgHeight);//创建指定尺寸图片
$pointLineColor = imagecolorallocate($img, $pointLineColor['r'], $pointLineColor['g'], $pointLineColor['b']);
$bgColor = imagecolorallocate($img, $bgColor['r'], $bgColor['g'], $bgColor['b']);//设定图片背景色
$txtColor = imagecolorallocate($img, $txtColor['r'], $txtColor['g'], $txtColor['b']);//设定文字颜色
$brColor = imagecolorallocate($img, $brColor['r'], $brColor['g'], $brColor['b']);//设定边框颜色
imagefill($img, 0, 0, $bgColor);//填充图片背景色
//先填充一个黑色的大块背景
imagefilledrectangle($img, $margin, $margin + $titleHeight, $imgWidth - $margin, $imgHeight - $margin, $brColor);//画矩形
//再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
imagefilledrectangle($img, $margin + $outLine, $margin + $titleHeight + $outLine, $imgWidth - $margin - $outLine, $imgHeight - $margin - $outLine, $bgColor);//画矩形
//imagealphablending($img, false);
//居中写入标题
$title && imagettftext($img, $titleSize, 0, ($imgWidth - $titleWidth) / 2, $titleHeight + $upPadding, $txtColor, $font, $title);
//画表格横线
$xkong = $ykong = [];
foreach ($data as $dk => $item) {
$yRow += $rowHeight;
$item = array_values($item);
foreach ($item as $key => $value) {
$max = $colMaxWidth[$key];
$x = $columnLines[$key] + $xCol;
if ($border) {
$value !== false && static::imagelinethick($img, $x - $max, $yRow, $x, $yRow, $brColor, $border);//画横线
$value !== null && static::imagelinethick($img, $x, $yRow-$rowHeight, $x, $yRow, $brColor, $border);//画纵线
}
if ($value !== false && $value !== null) {
$colk = $colWidth[$key][$dk] ?: $max;
$_max = $colWidth[$key][$dk] ? $max - $leftPadding - $outLine : 0;
$xf = $x - $_max - $border - $xkong[$key];
$align && $xf += ($max - $border - $colk + $xkong[$key]) / 2;//居中
$yf = $yRow - $upPadding + $calibHeight - $ykong[$dk][$key]/2 - $border;
if (!empty($dataColor[$dk][$key]['bg'])) {
$tdBg = $dataColor[$dk][$key]['bg'];
$tdFontColor = empty($tdBg[1]) ? $txtColor : imagecolorallocate($img, $tdBg[1]['r'], $tdBg[1]['g'], $tdBg[1]['b']);
$tdBgColor = empty($tdBg[2]) ? $bgColor : imagecolorallocate($img, $tdBg[2]['r'], $tdBg[2]['g'], $tdBg[2]['b']);
if ($tdBg[0] == 1) {
//画一矩形并填充
imagefilledrectangle($img, $x - $max - $xkong[$key] + $border, $yRow - $rowHeight + $border - $ykong[$dk][$key], $x - $border, $yRow - $border, $tdBgColor);
}elseif ($tdBg[0] == 2) {
$circleWidth = $max + $xkong[$key] - $border - $leftPadding * 2;
$circleHeight = $rowHeight - $border - $upPadding;
$circleWidth < $circleHeight * 2 && $circleWidth = $circleHeight;
//画一圆并填充
imagefilledellipse($img, $x - $max / 2 - $xkong[$key] / 2, $yf - $rowHeight / 2 + $upPadding + $border*2, $circleWidth, $circleHeight, $tdBgColor);
}
if (!empty($tdBg['point'])) {
if (!empty($tdBg['point']['to'])) {
static::imagelinethick($img, $tdBg['point']['to']['x'], $tdBg['point']['to']['y'], $xf + $border*2, $yf-$upPadding*2+$border, $pointLineColor, $pointBorder);//连线
}
if (!empty($tdBg['point'][0])) {//设置连线终点
$dataColor[$tdBg['point'][0]][$tdBg['point'][1]]['bg']['point']['to'] = ['x'=>$xf+$leftPadding/2, 'y'=>$yf+$upPadding/2];
}
}
imagettftext($img, $txtSize, 0, $xf, $yf, $tdFontColor, $font, $value);
}else {
imagettftext($img, $txtSize, 0, $xf, $yf, $txtColor, $font, $value);
}
$xkong[$key] = 0;
$ykong[$dk][$key] = 0;
}else if ($value === null) {
$xkong[$key+1] += $xkong[$key] + $max;
$xkong[$key] = 0;
}else if ($value === false) {
$ykong[$dk+1][$key] += $ykong[$dk][$key] + $rowHeight;
$ykong[$dk][$key] = 0;
}
}
}
//$red = imagecolorallocate($img, 255, 0, 0);
//imagefill($img, $margin, $margin, $red);
//imageantialias($img, true);
//区域填充到指定颜色的边界线色为止
//imagefilltoborder($img, $margin + $outLine, $margin + $titleHeight + $outLine + $upPadding*2, $brColor, $bgColor);
static::$im = $img;
}
/**
* @param array $data 内容数据
* @param array $header 头部数据
* @param string $title 标题
* @param int $txtSize 内容文字大小
* @param int $calibHeight 垂直距中校准
* @param int $outLine 外边框
* @param int $margin 外边距
* @param bool $border
* @param int $leftPadding
* @param int $upPadding
* @param int $titleSize
* @param bool $align
* @param string $font
*/
public static function tableImg($data = [], $header = [], $title = '', $txtSize = 24, $calibHeight = 0, $outLine = 2, $margin = 10, $border = true, $leftPadding = 0, $upPadding = 0, $titleSize = 0, $align = true, $font = '')
{
ini_set('memory_limit', '10240M');
$font = $font ?: static::$font;
$data && $data = array_values($data);
$header && $header = array_values($header);
$dataRow = count($data);
$colLine = count($header) - 1;
$rowLine = $dataRow + 1;
$border = $border ? 1 : 0;
$upPadding = $upPadding ?: round($txtSize / 5);//上下内边距
$leftPadding = $leftPadding ?: round($txtSize / 3);//左内边距
$titleSize = $titleSize ?: round($txtSize * 1.2);//标题字体大小
//计算标题写入起始位置
if ($title) {
$ttfBox = imagettfbbox($titleSize, 0, $font, $title);
$titleWidth = $ttfBox[2] - $ttfBox[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
$titleHeight = $ttfBox[1] - $ttfBox[7] + $upPadding*2;//左下角 Y 位置- 左上角 Y 位置 为文字高度
}else {
$titleWidth = $titleHeight = 0;
}
//计算文字宽度
foreach ($header as $k => $r) {
$ttfBox = imagettfbbox($txtSize, 0, $font, $r);
$colWidth[$k][] = ($ttfBox[2] - $ttfBox[0]) + $leftPadding*2 + $border;
$rowHeight[0][] = ($ttfBox[1] - $ttfBox[7]) + $upPadding*2 + $border;
}
$rows[0] = max($rowHeight[0]);
foreach ($data as $dk => $v) {
$k = -1;
$dk+= 1;
foreach ($v as $r) {
$k++;
$ttfBox = imagettfbbox($txtSize, 0, $font, $r);
$colWidth[$k][] = $ttfBox[2] - $ttfBox[0] + $leftPadding*2 + $border;
$rowHeight[$dk][] = $ttfBox[1] - $ttfBox[7] + $upPadding*2 + $border;
}
$rows[$dk] = max($rowHeight[$dk]);
}//print_r($rowHeight);
//取最大文字宽度
foreach ($colWidth as $k => $v) {
$colMaxWidth[$k] = max($v);
}//print_r($rows);
$imgWidth = array_sum($colMaxWidth) + $margin*2 + $outLine*2 - $border;//图片宽度
$imgHeight = array_sum($rows) + $margin*2 + $titleHeight + $outLine*2;//图片高度
static::$imgWidth = $imgWidth;
static::$imgHeight = $imgHeight;
$tabHeaderHeight = $margin + $titleHeight + $outLine;//表格顶部高度
$tabFooterHeight = $imgHeight - $margin;//表格底部高度
$columnLines = [];
foreach ($colMaxWidth as $k => $v) {
$columnLines[$k] = array_sum(array_slice($colMaxWidth, 0, $k+1));
}
$colors = tjLotteryHelper::colors(true);
$bgColor = $colors[0];
$img = imagecreatetruecolor($imgWidth, $imgHeight);//创建指定尺寸图片
$bgColor = imagecolorallocate($img, $bgColor['r'], $bgColor['g'], $bgColor['b']);//设定图片背景色
$txtColor = imagecolorallocate($img, 44, 44, 44);//设定文字颜色
$brColor = imagecolorallocate($img, 111, 111, 111);//设定边框颜色
imagefill($img, 0, 0, $bgColor);//填充图片背景色
//先填充一个黑色的大块背景
imagefilledrectangle($img, $margin, $margin + $titleHeight, $imgWidth - $margin, $imgHeight - $margin, $brColor);//画矩形
//再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
imagefilledrectangle($img, $margin + $outLine, $margin + $titleHeight + $outLine, $imgWidth - $margin - $outLine, $imgHeight - $margin - $outLine, $bgColor);//画矩形
//居中写入标题
$title && imagettftext($img, $titleSize, 0, ($imgWidth - $titleWidth) / 2, $titleHeight, $txtColor, $font, $title);
//画表格纵线 及 写入表头文字
foreach ($columnLines as $key => $x) {
$x += $margin + $outLine;
$border && $key < $colLine &&
imageline($img, $x, $tabHeaderHeight, $x, $tabFooterHeight, $brColor);//画纵线
$x = $x - $colMaxWidth[$key] + $leftPadding;
$align && $x+= ($colMaxWidth[$key] - $colWidth[$key][0])/2;//居中
$y = $tabHeaderHeight + $rows[0]/2 + ($rowHeight[0][$key] - $upPadding*2)/2 - $border;//垂直居中
//写入表头文字
imagettftext($img, $txtSize, 0, $x, $y, $txtColor, $font, $header[$key]);
}
//画表格横线
foreach ($data as $dk => $item) {
$dk += 1;
$tabHeaderHeight += $rows[$dk-1];
$border && $dk < $rowLine &&
imageline($img, $margin, $tabHeaderHeight, $imgWidth - $margin, $tabHeaderHeight, $brColor);
$key = -1;
foreach ($item as $value) {
$key++;
$x = $columnLines[$key] + $margin - $colMaxWidth[$key] + $leftPadding + $outLine;
$align && $x+= ($colMaxWidth[$key] - $colWidth[$key][$dk])/2;//居中
$y = $tabHeaderHeight + $rows[$dk]/2 + ($rowHeight[$dk][$key] - $upPadding*2)/2 + $border + $calibHeight;
//写入data数据
imagettftext($img, $txtSize, 0, $x, $y, $txtColor, $font, $value);
}
}
//$red = imagecolorallocate($img, 255, 0, 0);
//imagefill($img, $margin, $margin, $red);
//imageantialias($img, true);
//区域填充到指定颜色的边界线色为止
//imagefilltoborder($img, $margin + $outLine, $margin + $titleHeight + $outLine + $upPadding*2, $brColor, $bgColor);
static::$im = $img;
}
/**
* @param array $data
* @param array $lm
* @param int $kuan 宽
* @param int $jiange 间隔
* @param int $zuo 左留空
* @param int $you 右留空
* @param int $shang 上留空
* @param int $xia 下留空
*/
public static function columnImg($data = [], $lm = [], $title = '', $kuan = 15, $jiange = 7, $zuo = 40, $you = 10, $shang = 40, $xia = 30) {
//图像最小宽度
$minWidth = 350;
//图像高
$imgHeight = 250;
//标题高度
$titleHeight = $title ? 25 : 0;
$titleSize = 11;
$dataCount = count($data);
//得到最大值
$max = max($data);
//计算图像宽度
$imgWith = $zuo + $you + $dataCount * ($jiange + $kuan);
if ($imgWith <$minWidth) {
$k = ($minWidth - $imgWith) / $dataCount / 2;
$jiange += $k;
$kuan += $k;
$imgWith = $minWidth;
}
$image = imagecreatetruecolor($imgWith, $imgHeight + $titleHeight);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);//填充图片背景色
//色柱颜色
$data_yanse = [];
$colors = tjLotteryHelper::colors();
$count = count($colors);
foreach ($colors as $color) {
$rs = levHelpers::hexToRgb($color);
$data_yanse[] = imagecolorallocate($image, $rs['r'], $rs['g'], $rs['b']);
}
//坐标轴颜色
$xyColor = imagecolorallocate($image, 120, 120, 120);
//横轴
imageline ( $image, $zuo, $imgHeight - $xia, $imgWith - $you/2, $imgHeight - $xia, $xyColor);
//纵轴
imageline ( $image, $zuo, $shang/2, $zuo, $imgHeight-$xia, $xyColor);
$titleWH = self::getTextWidthHeight($titleSize, static::$font, $title);
//居中写入标题
imagettftext($image, $titleSize, 0, ($imgWith - $titleWH['w'])/2, $imgHeight + $titleHeight/2, $xyColor, static::$font, $title);
//纵轴刻度
$kedu = intval($imgHeight/50);
$zuojg = min(intval($zuo/strlen($max)), 20);
for ($i = 0; $i < $kedu; $i++) {
$pre = $i/$kedu;
imageline ( $image, $zuo, $shang+($imgHeight-$shang-$xia)*$pre, $zuo+5, round($shang+($imgHeight-$shang-$xia)*$pre), $xyColor);
imagestring ( $image, 3, $zuojg, $shang+($imgHeight-$shang-$xia)*$pre,round($max*(1-$pre)), $xyColor);
}
//画数据柱
for($i=0;$i<$dataCount;$i++){
//柱高
$zhugaodu = round(($imgHeight - $shang - $xia) * $data[$i] / $max);
//画数据柱
imagefilledrectangle( $image,$zuo+$jiange+$i*($kuan+$jiange),$shang+($imgHeight-$shang-$xia)-$zhugaodu,$zuo+$jiange+$i*($kuan+$jiange)+$kuan,($imgHeight-$xia)-1 ,$data_yanse[$i%$count]);
//标注数据柱上方数据值
imagestring ( $image, 2, $zuo+$jiange+$i*($kuan+$jiange)+2,$shang+($imgHeight-$shang-$xia)-$zhugaodu-15,$data[$i], $xyColor);
//横轴数据值
imagestring ( $image, 3, $zuo+$jiange+$i*($kuan+$jiange)+2,$imgHeight-$shang+15,$lm[$i], $xyColor);
}
//$image = imagerotate($image, -90, 0, 1);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
public static function lineImg() {
$a="200,120,400,200,340,500,450";
$b="Jan,Feb,Mar,Apr,May,Jue";
$jiange=40;//折点间隔
$zuo=40;//左侧留空
$you=40;//右侧留空
$shang=40;//上留空
$xia=30;//下留空
$zuidashujuzhi=1;//初始化纵轴最大数据值
if ($a=="") die("error id:a0");
if ($b=="") die("error id:b0");
$data=explode(",",$a);
$lm=explode(",",$b);
//得到最大值
for ($i=0;$i<count($data);$i++){
if(!is_numeric($data[$i])) die("error id:1");
if($data[$i]>$zuidashujuzhi) $zuidashujuzhi=$data[$i];
}
//计算图像宽度
$imgWith=500;//根据数据设定宽度用则改为$imgWith=$zuo+$you+$jiange+count($data)*($jiange);
//图像高
$imgHeight=150;
//存储折点高度的数组
$zdgaodu = array();
$image = imagecreatetruecolor($imgWith,$imgHeight);
$white = imagecolorallocate($image, 250, 250, 250);
//色柱颜色
$data_yanse =array(
imagecolorallocate($image, 0x97, 0xbd, 0x00),
imagecolorallocate($image, 0x00, 0x99, 0x00),
imagecolorallocate($image, 0xcc, 0x33, 0x00),
imagecolorallocate($image, 0xff, 0xcc, 0x00),
imagecolorallocate($image, 0x33, 0x66, 0xcc),
imagecolorallocate($image, 0x33, 0xcc, 0x33),
imagecolorallocate($image, 0xff, 0x99, 0x33),
imagecolorallocate($image, 0xcc, 0xcc, 0x99),
imagecolorallocate($image, 0x99, 0xcc, 0x66),
imagecolorallocate($image, 0x66, 0xff, 0x99)
);
//虚线颜色
$xuyanse = imagecolorallocate($image, 230, 230, 230);
//坐标轴颜色
$zuobiao_yanse = imagecolorallocate($image, 0x00, 0x00, 0x00);
//横轴
imageline ( $image, $zuo, $imgHeight-$xia, $imgWith-$you/2, $imgHeight-$xia, $zuobiao_yanse);
//纵轴
imageline ( $image, $zuo, $shang/2, $zuo, $imgHeight-$xia, $zuobiao_yanse);
//纵轴刻度,纵轴上共标注4个点,所以这里分别计算即可
imageline ( $image, $zuo, $shang, $zuo+6, $shang, $zuobiao_yanse);
imagestring ( $image, 3, $zuo/4, $shang,round($zuidashujuzhi), $zuobiao_yanse);
imageline ( $image, $zuo, $shang+($imgHeight-$shang-$xia)*1/4, $zuo+6, round($shang+($imgHeight-$shang-$xia)*1/4), $zuobiao_yanse);
imagestring ( $image, 3, $zuo/4, $shang+($imgHeight-$shang-$xia)*1/4,round($zuidashujuzhi*3/4), $zuobiao_yanse);
imageline ( $image, $zuo, $shang+($imgHeight-$shang-$xia)*2/4, $zuo+6, $shang+($imgHeight-$shang-$xia)*2/4, $zuobiao_yanse);
imagestring ( $image, 3, $zuo/4, $shang+($imgHeight-$shang-$xia)*2/4,round($zuidashujuzhi*2/4), $zuobiao_yanse);
imageline ( $image, $zuo, $shang+($imgHeight-$shang-$xia)*3/4, $zuo+6, $shang+($imgHeight-$shang-$xia)*3/4, $zuobiao_yanse);
imagestring ( $image, 3, $zuo/4, $shang+($imgHeight-$shang-$xia)*3/4,round($zuidashujuzhi*1/4), $zuobiao_yanse);
//得到每个折点的高度
for($i=0;$i<count($data);$i++){
array_push ($zdgaodu, round($imgHeight-($imgHeight-$shang-$xia)*$data[$i]/$zuidashujuzhi));
}
//创建折线图
for ($i=0; $i<count($data)-1; $i++){
imageline($image, $zuo+$i*$jiange, $zdgaodu[$i]-$xia, $zuo+($i+1)*$jiange, $zdgaodu[$i+1]-$xia, $data_yanse);
}
//创建虚线
for ($i=1; $i<count($data)-1; $i++){
imageline($image, $zuo+$i*$jiange, $zdgaodu[$i]-$xia+2, $zuo+$i*$jiange, $imgHeight-$xia-2, $xuyanse);
}
//标注数据值
for($i=0;$i<count($data);$i++){
imagestring ( $image, 2, $zuo+$i*$jiange+2,$zdgaodu[$i]-$xia-15,$data[$i], $zuobiao_yanse);
imagestring ( $image, 3, $zuo+$i*$jiange,$imgHeight-$shang+15,$lm[$i], $zuobiao_yanse);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
/**
* 画一条粗线
* @param $image
* @param $x1
* @param $y1
* @param $x2
* @param $y2
* @param $color
* @param int $thick
* @return bool
*/
public static function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
/* 下面两行只在线段直角相交时好使
imagesetthickness($image, $thick);
return imageline($image, $x1, $y1, $x2, $y2, $color);
*/
if ($thick == 1) {
return imageline($image, $x1, $y1, $x2, $y2, $color);
}
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2) {
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
}
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
$a = $t / sqrt(1 + pow($k, 2));
$points = array(
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
);
imagefilledpolygon($image, $points, 4, $color);
return imagepolygon($image, $points, 4, $color);
}
/**
* 获取文字的宽高
* @param $txtSize
* @param $font
* @param $text
* @return array
*/
public static function getTextWidthHeight($txtSize, $font, $text) {
$ttfBox = imagettfbbox($txtSize, 0, $font, $text);
return [
'w' => $ttfBox[2] - $ttfBox[0], //右下角 X 位置 - 左下角 X 位置 为文字宽度
'h' => $ttfBox[1] - $ttfBox[7], //左下角 Y 位置- 左上角 Y 位置 为文字高度
];
}
public static function createHtmlTab($data) {
$trs = '';
foreach ($data as $k => $tr) {
$trs .= '<tr>';
foreach ($tr as $dk => $td) {
if ($td === null) {
$col = empty($col) ? 2 : $col+1;
$k && $data[$k-1][$dk] === false && $col -= 1;
continue;
}
if (!empty($tds[$dk]) && $k <= $tds[$dk][1] && $dk == $tds[$dk][0]) {
$k == $tds[$dk][1] && $tds[$dk] = null;
continue;
}
if (empty($tds[$dk]) && $td === false) {
$tdrs = static::getCols($data, $k, $dk);
$td = $tdrs[0];
$tds[$dk] = [$dk, $tdrs[1]];
$span = ' rowspan="'.($tds[$dk][1]-$k+1).'"';
}else {
$span = $col >1 ? ' colspan="'.$col.'"' : '';
$col = 0;
}
$trs .= '<td'.$span.'>'.$td.'</td>';
}
$trs .= '</tr>';
}
return $trs;
}
public static function getCols($data, $k1, $dk1) {
if (!empty($data[$k1+1][$dk1])) {
return [$data[$k1+1][$dk1], $k1+1];
}
foreach ($data as $k => $tr) {
if ($k > $k1) {
$tr = array_values($tr);
foreach ($tr as $dk => $td) {
if ($dk == $dk1 && $td !== false && $td !== null) return [$td, $k];
}
}
}
return null;
}
/**
* 保存图像到指定文件名
* @param $imgPath
*/
public static function saveImage($imgPath, $mime = 'jpg', $quality = 80) {
if ($mime == 'jpg') {
imagejpeg(static::$im, $imgPath, $quality);//$quality = 0~100
imagedestroy(static::$im);
}else {
imagepng(static::$im, $imgPath, $quality);//$quality = 0~9
imagedestroy(static::$im);
}
}
/**
*直接输出图像
*/
public static function echoImage($mime = 'jpg', $quality = 80) {
if ($mime == 'jpg') {
header('Content-type: image/jpeg');
imagejpeg(static::$im, null, $quality);//$quality = 0~100
imagedestroy(static::$im);
}else {
header('Content-type: image/png');
imagepng(static::$im, null, $quality);//$quality = 0~9
imagedestroy(static::$im);
}
}
}