/**
* 我的海报
*/
public function poster()
{
$info = Users::select('poster')->find($this->uid);
if($info['poster'])
{
$info['poster'] = $_SERVER['APP_URL'].$info['poster'];
return sendSuccess('ok',200,['info'=>$info]);
}
//生成海报
else {
$result = $this->getWxcode($this->uid);
Users::where(['id'=>$this->uid])->update(['poster'=>$result]);
$info2 = Users::select('poster')->find($this->uid);
$info2['poster'] = $_SERVER['APP_URL'].$info2['poster'];
return sendSuccess('ok',200,['info'=>$info2]);
}
}
/**
* 合并图片,生成推广码图片
*/
public function user_poster($wechat_code)
{
header("Content-type: image/png");
$img_1 = $_SERVER['APP_URL'].'code_img/poster_bg.png';
$img_2 = $_SERVER['APP_URL'].$wechat_code;
$image_1 = imagecreatefrompng($img_1);
$image_2 = imagecreatefrompng($img_2);
$image_3 = imagecreatetruecolor(imagesx($image_1),imagesy($image_1));
imagecopymerge($image_3,$image_1,0,0,0,0,imagesx($image_1),imagesy($image_1),100);
list($width,$height) = getimagesize($img_2); // 获取 b.jpg 图片的信息
imagecopyresampled($image_3, $image_2, 207, 575, 0, 0, 330, 330,$width, $height);
$pic_name = md5(uniqid()).".png";
$path = 'code_img/user_poster/'.$pic_name;
imagejpeg($image_3,$path,50);
imagedestroy($image_3);
return $path;
}
/**********生成微信小程序二维码**************/
public function getWxAccessToken(){
$appid = config('wechat.mini_program')['default']['app_id'];
$appsecret = config('wechat.mini_program')['default']['secret'];
if(session('access_token_'.$appid) && session('expire_time_'.$appid)>time()){
return session('access_token_'.$appid);
}else{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$access_token = $this->makeRequest($url);
$access_token = json_decode($access_token['result'],true);
session('access_token_'.$appid,$access_token);
session('expire_time_'.$appid,time()+7000);
return $access_token;
}
}
/**
* 发起http请求
* @param string $url 访问路径
* @param array $params 参数,该数组多于1个,表示为POST
* @param int $expire 请求超时时间
* @param array $extend 请求伪造包头参数
* @param string $hostIp HOST的地址
* @return array 返回的为一个请求状态,一个内容
*/
public function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '')
{
if (empty($url)) {
return array('code' => '100');
}
$_curl = curl_init();
$_header = array(
'Accept-Language: zh-CN',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
);
// 方便直接访问要设置host的地址
if (!empty($hostIp)) {
$urlInfo = parse_url($url);
if (empty($urlInfo['host'])) {
$urlInfo['host'] = substr(DOMAIN, 7, -1);
$url = "http://{$hostIp}{$url}";
} else {
$url = str_replace($urlInfo['host'], $hostIp, $url);
}
$_header[] = "Host: {$urlInfo['host']}";
}
// 只要第二个参数传了值之后,就是POST的
if (!empty($params)) {
curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($_curl, CURLOPT_POST, true);
}
if (substr($url, 0, 8) == 'https://') {
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($_curl, CURLOPT_URL, $url);
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);
if ($expire > 0) {
curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间
curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
}
// 额外的配置
if (!empty($extend)) {
curl_setopt_array($_curl, $extend);
}
$result['result'] = curl_exec($_curl);
$result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
$result['info'] = curl_getinfo($_curl);
if ($result['result'] === false) {
$result['result'] = curl_error($_curl);
$result['code'] = -curl_errno($_curl);
}
curl_close($_curl);
return $result;
}
public function getWxcode($uid){
$ACCESS_TOKEN=$this->getWxAccessToken();
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN['access_token'];
$post_data=
array(
'scene' => "poster_uid=$uid",//34%2CS853EE4QRP
'is_hyaline' => true,
'width' => 330,
'line_color' => ['r'=>299,'g'=>3,'b'=>99],
);
$post_data = json_encode($post_data);
$data = $this->send_post($url,$post_data);
$result = $this->data_uri($data,'image/png');
$result2 = $this->base64_image_content($result,'code_img/user_poster/');
$result3 = $this->user_poster($result2);
return $result3;
}
/**
* [将Base64图片转换为本地图片并保存]
* @param [Base64] $base64_image_content [要保存的Base64]
* @param [目录] $path [要保存的路径]
*/
function base64_image_content($base64_image_content,$path){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
if(!file_exists($path)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($path, 0700);
}
$new_file = $path.time().rand(1000,9999).".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return $new_file;
}else{
return false;
}
}else{
return false;
}
}
/**
* 消息推送http
* @param $url
* @param $post_data
* @return bool|string
*/
protected function send_post( $url, $post_data ) {
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
//header 需要设置为 JSON
'content' => $post_data,
'timeout' => 60
//超时时间
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}
//二进制转图片image/png
public function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}