PHP使用DG生成表格并填充相应颜色
可给某一行加色
也可以给某一个单元格加色
废话不多说 直接上源码
public function create($params)
{
$base = [
'border' => 10,//图片外边框
'file_path' => $params['file_path'],//图片保存路径
'title_height' => 25,//报表名称高度
'title_font_size' => 16,//报表名称字体大小
'font_ulr' => realpath(__DIR__.'/wryh.ttf'),//字体文件路径
'text_size' => 12,//正文字体大小
'row_hight' => 30,//每行数据行高
];
$save_path = $base['file_path'] . $params['file_name'];
//如果表说明部分不为空,则增加表图片的高度
if(!empty($params['table_explain'])){
$base['title_height'] = $base['title_height'] * count($params['table_explain']);
}
//计算图片总宽
$w_sum = $base['border'];
foreach ($params['field_width'] as $key => $value) {
//图片总宽
$w_sum += $value;
//计算每一列的位置
$base['column_x_arr'][$key] = $w_sum;
}
$base['img_width'] = $w_sum + $base['border'] * 2-$base['border'];//图片宽度
$base['img_height'] = ($params['row']+1) * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//图片高度
$border_top = $base['border'] + $base['title_height'];//表格顶部高度
$border_bottom = $base['img_height'] - $base['border'];//表格底部高度
$img = imagecreatetruecolor($base['img_width'], $base['img_height']);//创建指定尺寸图片
$bg_color = imagecolorallocate($img, 255,255,255);//设定图片背景色
$borderbg_color = imagecolorallocate($img, 242,244,42);//设定图片背景色
$text_coler = imagecolorallocate($img, 0, 0, 0);//设定文字颜色
$border_coler = imagecolorallocate($img, 0, 0, 0);//设定边框颜色
imagefill($img, 0, 0, $bg_color);//填充图片背景色
//先填充一个黑色的大块背景
imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//画矩形
//再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//画矩形
foreach($params['rowy'] as $r){
$startx = $base['border'] + 2 ;
$starty = $base['border'] + $base['title_height'] + 2+($r-1)*30;
$endx = $base['img_width'] - $base['border'] - 2;
$endy = $base['border'] + $base['title_height'] + 2 + $r*30;
imagefilledrectangle($img, $startx, $starty, $endx, $endy, $borderbg_color);//画矩形
}
foreach($params['clo'] as $c){
$startx = $base['border'] + $base['column_x_arr'][$c['x']-2]-10 ;
$starty = $base['border'] + $base['title_height'] + 2+($c['y']-1)*30;
$endx = $base['border'] + $base['column_x_arr'][$c['x']-1]-10;
$endy = $base['border'] + $base['title_height'] + 2+($c['y'])*30-3;
imagefilledrectangle($img, $startx, $starty, $endx, $endy, $borderbg_color);//画矩形
}
//画表格纵线 及 写入表头文字
$sum = $base['border'];
foreach($base['column_x_arr'] as $key => $x){
imageline($img, $x, $border_top, $x, $border_bottom,$border_coler);//画纵线
$this_title_box = imagettfbbox($base['text_size'], 0, $base['font_ulr'], $params['table_header'][$key]);
$title_x_len = $this_title_box[2] - $this_title_box[0];
imagettftext($img, $base['text_size'], 0, $sum + (($x-$sum)/2 - $title_x_len/2), $border_top + ($base['row_hight']+$base['text_size'])/2, $text_coler, $base['font_ulr'], $params['table_header'][$key]);//写入表头文字
$sum += $params['field_width'][$key];
}
//画表格横线
foreach($params['data'] as $key => $item){
$border_top += $base['row_hight'];
//画横线
imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler);
$this_first = imagettfbbox($base['text_size'], 0, $base['font_ulr'], $key);
$first_len = $this_first[2] - $this_first[0];
imagettftext($img, $base['text_size'], 0, $params['field_width'][0]/2 - $first_len/2+$base['border'], $border_top + ($base['row_hight']+$base['text_size'])/2, $text_coler, $base['font_ulr'], $key);//写入序号
$sub = 0;
$sum = $params['field_width'][0]+$base['border'];
foreach ($item as $value){
$sub++;
$this_title_box = imagettfbbox($base['text_size'], 0, $base['font_ulr'], $value);
$title_x_len = $this_title_box[2] - $this_title_box[0];
imagettftext($img, $base['text_size'], 0, $sum + (($base['column_x_arr'][$sub]-$sum)/2 - $title_x_len/2), $border_top + ($base['row_hight']+$base['text_size'])/2, $text_coler, $base['font_ulr'], $value);//写入data数据
$sum += $params['field_width'][$sub];
}
}
//计算标题写入起始位置
$title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
$title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
$title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 为文字高度
$save_path = $base['file_path'] . $params['file_name'];
if(!is_dir($base['file_path']))//判断存储路径是否存在,不存在则创建
{
mkdir($base['file_path'],0777,true);
}
//居中写入标题
imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width)/2, 30, $text_coler, $base['font_ulr'], $params['title']);
//设置图片左上角信息
$a_hight = 20;
if(!empty($params['table_explain'])){
foreach ($params['table_explain'] as $key => $value) {
imagettftext($img, $base['text_size'], 0, 10, 20+$a_hight, $text_coler, $base['font_ulr'], $value);
$a_hight += 20;
}
}
imagepng($img,$save_path);//输出图片,输出png使用imagepng方法,输出gif使用imagegif方法
echo '<img src="/'.$save_path.'"/>';
}
public function actionTest(){
//表格数据
$data = [
["stat_date" => "日期", "ah_hour" => "时间", "project_name" => "二级项目", "ad_name" => "账户名", "cost" => "消耗"],
["stat_date" => '2021-03-30', "ah_hour" => '19:54', "project_name" => '小熊美术搜索1',"ad_name"=>'杭州小伴熊科技有限公司-小熊搜索1','cost'=>'121.34'],
["stat_date" => '2021-03-31', "ah_hour" => '19:55', "project_name" => '小熊美术搜索2',"ad_name"=>'杭州小伴熊科技有限公司-小熊搜索2','cost'=>'122.34'],
["stat_date" => '2021-03-32', "ah_hour" => '19:56', "project_name" => '小熊美术搜索3',"ad_name"=>'杭州小伴熊科技有限公司-小熊搜索3','cost'=>'123.34'],
];
//图片左上角汇总说明数据,可为空
$table_explain = [
0 => '[日期]'.date('Y-m-d H:i:00'),
1 =>' ',
];
//表头信息
$table_header = [
"","日期","时间","二级项目","账户名称","消耗","展示","点击","点击率","转化数",
"转化成本","转化率","优化师",
];
//每个格子的宽度,可根据数据长度自定义
$field_width = ['60','100','100','130','300','100','100','100','100','100','100','100','120'];
$clo[] = ['x'=>2, 'y'=>2];
$clo[] = ['x'=>5, 'y'=>2];
//参数
$params = [
'rowy' => ['1','4'],
'clo' => $clo,
'row' => count($data), //数据的行数
'file_name' => 'test.png', //保存的文件名
'title' => "美术宝 小熊美术",
'table_time' => date("Y-m-d H:i:s"),
'data' => $data,
'table_explain' => $table_explain,
'table_header' => $table_header,
'field_width' => $field_width,
'file_path' => './upload/image/' //文件保存路径
];
//调用方法
$this->create($params);
}