ShareController.php
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/6/8
* Time: 18:59
*/
namespace app\portal\controller;
use cmf\controller\WeChatBaseController;
use think\Db;
class ShareController extends WeChatBaseController
{
/**
* 我的推广页面
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(){
//判断用户是否填写手机号
$user_id = cmf_get_current_user_id();
$my_user = Db::name('my_user')->where('uid',$user_id)->find();
$user = Db::name('user')->where('id',$user_id)->find();
if(empty($my_user)){
$this->error('查询为空','','','');
}
// if(empty($my_user['phone'])){
// $this->error('跳转补全信息页面','','','');
// }
//判断有没有生成过
if(!file_exists('./upload/poster/poster_'.$user_id.".png")){
dump(123);
$url = $this->get_url($user_id,$my_user['status']);
$webPath = 'qrcode/share_'.$user_id.'.png';
$savePath = './upload/'.$webPath;
//生成带参二维码
if(!file_exists($savePath)){
$prcode_url = $this->get_code($user_id,$url);
}
//保存头像
$avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']);
if($avatar['code']==1){
$this->error($avatar['msg']);
}
$savePath = './upload/poster';
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg");
$image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png");
$image = \think\Image::open(ROOT_PATH."public/white.png");
$image->water(ROOT_PATH."public/upload/qrcode/share_".$user_id.".png",[195,725],100)
->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100)
->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100)
->save('./upload/poster/poster_'.$user_id.".png");
}
$share_image = cmf_get_image_url('poster/poster_'.$user_id.".png");
$this->assign('share_image',$share_image);
return $this->fetch();
}
/**
* 分享链接
* @param $user_id
* @param $status
* @return string
*/
public function get_url($user_id,$status){
//http://xkeasy.w.bronet.cn/portal/index/index/my_user_id/用户id/status_id/身份id
$url = url('portal/index/index',['my_user_id'=>$user_id,'status_id'=>$status]);
return $url;
}
/**
* 生成二维码
* @param $user_id
* @param $qrData
* @return bool|string
*/
public function get_code($user_id,$qrData){
/*存储本地*/
$webPath = 'qrcode/';
$savePath = './upload/'.$webPath;
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$saveName = "share_".$user_id;
$qrLevel = 'H';
$qrSize = '8';
$savePrefix = "";
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix,$saveName)){
$pic = $webPath .$savePrefix. $saveName . '.png';
return $pic;
}else{
return false;
}
}
/**
* 下载微信头像保存到本地
* @param $url
* @param $filename
* @return array
*/
function getImage($url,$filename){
$save_dir='./upload/avatar/';
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return ['code'=>1,'msg'=>'图片路径错误'];
}
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}else{
return ['code'=>1,'msg'=>'图片转换失败'];
}
$img_content=$imgBase64Code;//图片内容
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
$type = $result[2];
$new_file = $save_dir.$filename.'.'.$type;
$save_path= 'avatar/'.$filename.'.'.$type;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
return ['code'=>0,'file_path'=>$new_file,'save_path'=>$save_path];
}else{
return ['code'=>1,'msg'=>'图片保存失败'];
}
}else{
return ['code'=>1,'msg'=>'图片格式错误'];
}
}
}