微信公众号生成推广海报 thinkphp5开发微信二维码
作者:广州小程序开发 时间:2018-04-09 11:43
很多微信公众号,点击菜单或者回复关键词就能生成维信二维码海报,海报上有粉丝头像和二维码,这个要如何实现?
这里就以输入关键词触发返回海报为例
如下代码 $poster_path为海报路径 $qrcode_path 为生成海报保存的目录 $fromUsername 为openid
if($keyword =='我的海报') {
$media_id = $this->Create_img($poster_path,
$qrcode_path,$fromUsername);
//$media_id ='Thbrcg_2tqz-OV9liwHl9cvw8coOWg0R8DDdBKB6dJPiWv8Wdu7JExGf6z30e6Xz';
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml> ";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'image',$media_id);
exit($resultStr);
}
Create_img 方法为生成海报方法 生成海报后返回一个media_id 通过media_id返回图片
Create_img 方法详解
public function Create_img($poster_path,$qrcode_path,$openid){
if($openid){
$res = db('users')->where('openid',$openid)->find();
$qrcode=@file_get_contents($qrcode_path.$openid.".jpg");
if(!$qrcode){
//var_dump($res['avatar']);exit;
$wx=db('wx_user')->where(array('wxid'=>'gh_25349be8c131'))->find();//读取微信配置参数
$access_token = $this->getWxAccessToken($wx['appid'],$wx['appsecret']);
$tagid ="101_".$res['id'];
$url = $this->getQrCode($access_token,$tagid);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
$qr_content = ob_get_contents();
header('Content-type: image/jpg');
ob_end_clean();
//缩放二维码大小为需要的大小,并将二维码加入到海报中
$thumb = imagecreatetruecolor(250, 250);
//获取源文件资源句柄。接收参数为图片流,返回句柄
$source = imagecreatefromstring($qr_content);
//将源文件剪切全部域并缩小放到目标图片上,前两个为资源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, 250, 250, 430, 430);
//创建图片的实例,接收参数为图片
$dst_qr = @imagecreatefromstring(file_get_contents($poster_path));
// $dst_qr1 = @imagecreatefromstring(file_get_contents($res['avatar']));
//var_dump($res['avatar']);exit;
//加水印
// imagecopy($dst_qr, $dst_qr1, 395, 58, 0, 0, 64, 64);
imagecopy($dst_qr,$thumb, 146, 173, 0, 0, 250, 250);
// imagecopy($dst_qr,$res['avatar'], 320, 180, 0, 0, 200, 200);
//销毁
imagedestroy($thumb);
ob_start();//启用输出缓存,暂时将要输出的内容缓存起来
imagejpeg($dst_qr,$qrcode_path.$openid.".jpg",160);//输出
$poster = ob_get_contents();//获取刚才获取的缓存
ob_end_clean();//清空缓存
imagedestroy($dst_qr);
$tmp_path = $qrcode_path.$openid.".jpg";
//$headimgurl = substr($res['avatar'],0,-1).'64';
$headimgurl = substr($res['avatar'],0,-3).'64';
$this->avatar($headimgurl,$tmp_path);
$post_data['media'] = '@'.$qrcode_path.$openid.".jpg";
$result=$this->uploadMedia($access_token,"image",$post_data);
// var_dump($result);exit;
if($result) {
//$media_id=$result->media_id;
$media_id=json_decode($result, true);
}
}else{
$wx=db('wx_user')->where(array('wxid'=>'gh_25349be8c131'))->find();//读取微信配置参数
$access_token = $this->getWxAccessToken($wx['appid'],$wx['appsecret']);
$post_data['media'] = '@'.$qrcode_path.$openid.".jpg";
$result=$this->uploadMedia($access_token,"image",$post_data);
//var_dump($result);exit;
if($result) {
$media_id=json_decode($result, true);
// var_dump($media_id);exit;
}
}
return $media_id['media_id'];
// var_dump($media_id['media_id']);
}
}
getQrCode为生成自定义二维码方法
function getQrCode($access_token,$tagid){
//获取ticked 票据
//全局票据ACCESS
// $access_token = $this->getWxAccessToken();
// echo $access_token;exit();
//{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
$postArr = array(
// 'expire_seconds' => 2592000,
'action_name' =>"QR_LIMIT_STR_SCENE",
'action_info' =>array(
'scene'=>array('scene_str'=>$tagid),
),
);
$post_Json = json_encode($postArr);
// var_dump($post_Json);exit;
$res=$this->http_curl($url,$post_Json);
$result = json_decode($res, true);
$ticket = $result['ticket'];
//使用ticket获取二维码图片
$url ="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".UrlEncode($ticket);
return $url;
}
avatar 方法和生成海报方法类似,这样就基本就可以啦
欢迎转载 广州山水居科技 www.shanshuijv.com