合成图片生成的动态效果图:
//合成图片
public function merge_picture()
{
$param = request()->param();
if (empty($param['customer_id'])) {
return $this->api_des_encrypt('', '参数缺失', 0);
}
$customer_id = $param['customer_id'];
if (empty($param['name'])) {
return $this->api_des_encrypt('', '参数缺失!', 0);
}
//$log = new SimpleLog('merge_picture2');
//$log->write_log($param);
$customer = Db::name('customer')->where('customer_id', $customer_id)->find();
//$log->write_log($customer);
if (!$customer) {
return $this->api_des_encrypt('', '参数不合法', 0);
}
//获取action表及tag表的数据
$data = $this->get_data($customer_id);
//$log->write_log('--------------action表及tag表的数据-----------------');
//$log->write_log($data);
//$action_list = $data['action'];
//$tag_list = $data['tag'];
//后端图片存储url
$url = 'http://xgs521.com';
//文件名
$filename = $param['name'];
$background_img =
ROOT_PATH
. 'public/static/bg.png';
$filename = $this->make_image($param['customer_id'], $background_img, '', $data, $filename, 1, 1, $url);
return $filename;
}
//获取动态数据
private function get_data($customer_id)
{
//$log = new SimpleLog('get_data');
//$log->write_log('----------customer_id------------');
//$log->write_log($customer_id);
$action = Db::name('action')->distinct(true)
->field('id,action_id,customer_id,name,type,article_id,article_name,create_time')
->where('customer_id', $customer_id)
->order('create_time desc')
->limit(19)
->select();
$actionArr = [];
foreach ($action as $value) {
$create_time = $value['create_time'];
$startTime = explode(' ', $create_time)[0];
$endTime = $startTime . ' 23:59:59';
$actionList = $this->is_exist($action, $startTime, $endTime);
// $actionList = Db::name('action')->distinct(true)
// ->field('id,action_id,customer_id,name,type,article_id,article_name,create_time')
// ->where('customer_id', $customer_id)->whereBetween('create_time', [$startTime, $endTime])->select();
$actionTypeArr = array_column($actionList, 'type');
foreach ($actionTypeArr as $item) {
$actionList2 = Db::name('action')->distinct(true)
->field('id,action_id,customer_id,name,type,article_id,article_name,create_time')
->where('customer_id', $customer_id)->whereBetween('create_time', [$startTime, $endTime])->where('type', $item)->order('create_time desc')->select();
$data = $this->get_key($startTime, $create_time);
$actionList2 = $this->str_replace_arr($actionList2, $data);
$actionArr[strtotime($startTime)][$item] = $actionList2;
}
}
$max = 20;
$rows = 0;
$new_action_arr = [];
foreach ($actionArr as $k => $v) {
$max--;
$day = 0;
if ($max <= 0) {
break;
}
foreach ($v as $kk => $vv) {
foreach ($vv as $ks => $vs) {
$max--;
if ($max <= 0) {
break 3;
} else {
$day++;
$rows++;
$new_action_arr[$k][$kk][$ks] = $vs;
if ($day <= 1) {
$new_action_arr[$k][$kk][$ks]['today_point'] = 1;
}
}
}
}
}
//dump($new_action_arr);
$tag = Db::name('tag')->distinct(true)
->field('id,name,type,count,weight')
->where('customer_id', $customer_id)
->order('create_time desc')
->limit(6)
->select();
$tagArr = [];
foreach ($tag as $key => $value) {
$value['weight'] = $value['weight'] . '(权重)';
$value['count'] = $value['count'] . '(次数)';
$str = $value['name'] . '(' . $value['type'] . ')';
$value['name'] = $str;
$tagArr[$key] = $value;
}
//调用python接口获取留资概率
/*$csv = $this->lzgl($customer_id);
$log->write_log('----------csv------------');
$log->write_log($csv);
$model_id = 1;
$system = shell_exec('ssh root@172.18.141.227 python3 /var/www/dev/pdvm-php/shaue_model/profile_model/predict/predict_prob.py ' . $model_id . ' ' . $customer_id);
$log->write_log('----------system------------');
$log->write_log($system);
$str = str_replace("'",'',$system);
$str = str_replace("{",'',$str);
$str = str_replace("}",'',$str);
$log->write_log($system);
$sys_arr = explode(',',$str);
$info = [];
foreach ($sys_arr as $v) {
if (false !== strpos($v, 'prob')) {
$info['percent'] = trim(explode(':', $v)[1]);
} else {
$sg = explode(':', $v);
$info['suggest'][trim($sg[0])] = $this->str_rep(trim($sg[1]), 14);
}
}
$log->write_log($info);*/
$lz = [];
/*$lz['suggest'] = [
'这里是十五个字这里是十五个字字',
'这里是十五个字这里是十五个字字',
'这里是十五个字这里是十五个字字字字',
'这里是十五个字这里是十五个字字字字',
'这里是十五个字这里是十五个字字字字',
];
$lz['suggest'] = $this->str_rep($lz['suggest'], 14);
$lz['percent'] = 45;
$lz['suggest_percent'] = 'public/static/suggest_percent.png';*/
return ['action' => $new_action_arr, 'tag' => $tagArr, 'lz' => $lz, 'action_count' => $rows];
}
//$image_1是已经创建好的图像
private function merge_pic($image_1, $img, $x, $y, $img_type = 1)
{
//创建图片对象
/*if($background_img_type == 1){
$image_1 = imagecreatefrompng($background_img);
}else{
$image_1 = imagecreatefromjpeg($background_img);
}*/
if (!empty($img)) {
if ($img_type == 1) {
$image_2 = imagecreatefrompng($img);
} else {
$image_2 = imagecreatefromjpeg($img);
}
list($point_w, $point_h) = getimagesize($img);
}
//合成图片 imagecopymerge(目标图像,被拷贝的源图像,目标图像开始 x 坐标,目标图像开始 y 坐标,拷贝图像开始 x 坐标,图像开始 y 坐标,(从 src_x 开始)拷贝的宽度,(从 src_y 开始)拷贝的高度,图像合并程度)
//imagecopymerge($image_1, $image_2, 49, 111, 100, 100, imagesx($image_2), imagesy($image_2), 100);
if (!empty($img)) {
imagecopy($image_1, $image_2, $x, $y, 0, 0, $point_w, $point_h);
}
}
//留资概率生成csv文件
private function lzgl($customer_id)
{
//$log = new SimpleLog('lzgl');
$model = Db::name('models')->where('status', 1)->where('id', 1)->find();
//$log->write_log($model);
if (!$model) {
return false;
}
//$redis = RedisCache::
get_instance
();
//$key = 'customer_model_id:' . $model['id'];
$json_encode = json_encode($model);
$redis->setex($key, 36000, $json_encode);
$time = $model['time'];
$endTime = date('Y-m-d H:i:s', strtotime('now'));
$startTime = date('Y-m-d H:i:s', strtotime('-' . $time . ' month', strtotime($endTime)));
$arr = model_data_process($model, $startTime, $endTime, $customer_id);
//$log->write_log($arr);
$fileName = 'CSTT_CUSTOMER_' . $model['id'] . '_' . $customer_id . '.csv';
$handle = fopen(
ROOT_PATH
. '/cstt/' . $fileName, "w");
fputcsv($handle, $arr['head']);
foreach ($arr['data'] as $key => $value) {
//$log->write_log($value);
fputcsv($handle, $value);
}
fclose($handle);
return true;
}
清理图片及空文件夹
//遍历文件夹,并删除空文件夹
private function list_file($date)
{
//1、首先先读取文件夹
$temp = scandir($date);
//$log = new SimpleLog('list_file');
//$log->write_log($temp);
//var_dump($temp);
$res = [];
//遍历文件夹
foreach ($temp as $v) {
$a = $date . '/' . $v;
if (is_dir($a)) {//如果是文件夹则执行
if ($v == '.' || $v == '..') {//判断是否为系统隐藏的文件.和.. 如果是则跳过否则就继续往下走,防止无限循环再这里。
continue;
}
$file = $this->list_file($a);//因为是文件夹所以再次调用自己这个函数,把这个文件夹下的文件遍历出来
if (empty($file)) {
//说明当前文件夹是空的
rmdir($a); //该函数只能删除空文件夹
} else {
$res = array_merge($res, $file);
}
} else {
$res[] = $a;
}
}
return $res;
}
//清理图片及空文件夹
public function del_picture()
{
$time = time();
$path =
ROOT_PATH
. 'public/static/img';
//$log = new SimpleLog('del_picture');
$files = $this->list_file($path);
//$log->write_log($files);
foreach ($files as $k => $v) {
$ctime = filectime($v);
if ($ctime + 86400 < $time) {
//$log->write_log($v);
//删除掉创建时间超过一天的文件
$res = unlink($v);
//$log->write_log($res);
}
}
$this->list_file($path);
return 'success';
}