PHP利用GD库将微信小程序二维码和用户头像拼接且用户在微信服务号回复指定内容将拼接的二维码返回

//首先得确认当前框架是否有GD库    用phpinfo()查看 如果有继续进行
//获取用户推广二维码(中间是用户头像的)
    public function getUserMiddle(){
        $request =$this->request;
        $this->selfcall = true;
        $memberinfo = $this->getMemberInfo($request);
		//获取二维码
        $img_url=$this->getUserMiniCode();
        //获取用户头像
        $useravatar = DB::table('miniapp')
            ->where('userid',$memberinfo['userid'])
            ->first();
        //用户头像链接
        $url=$useravatar['avatarUrl'];
		$user_img=$this->_circleImg($img_url, $url);
		$savePath = "F:\wwwroot\cacos\statics\images\wechatImg\head_user\/{$useravatar['userid']}".".png";
		imagePNG($user_img, $savePath);
		imagedestroy($user_img);
        $arr = array('ret'=>1,
            'msg'=>'success'
        );
        echo json_encode($arr);
    }
	 /**
     * 获取圆 
     */
    private function _circleImg($backPath, $headPath)
    {
        $head_img = imagecreatefromstring(file_get_contents($headPath)); //从流中新建一个图像

        list($w, $h) = getimagesize($headPath);
        $w           = $h = min($w, $h);//取最小的宽高
        $back_img = imagecreatefromstring(file_get_contents($backPath));
        //imagesavealpha($img, true);

        // 拾取一个完全透明的颜色,最后一个参数127为全透明
        //$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);

        //imagefill($img, 0, 0, $bg);
        $r = $w / 2; // 圆的半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($head_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r)))
                    imagesetpixel($back_img, $x+74, $y+74, $rgbColor);//在中间描绘
            }
        }
        imagedestroy($head_img);
        return $back_img;
    }

//指定内容返回
public function message(){
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$fromUsername = $postObj->FromUserName;
		$keyword = strtolower(trim(iconv("UTF-8","UTF-8",$postObj->Content)));
		if(trim($keyword) == '推荐码'){
                    //获取用户信息
                    $userid="";
                    $user_info=$this->member_db->get_one(to_sqls(array('openid'=>$fromUsername))."and telephone <> ''");
                    //获取用户token
                    if($user_info){
                        $query = "select * from co_weixin_token where id=(select max(id) from co_weixin_token where val like '%{$user_info['telephone']}%')";
                        $this->weixin_db->query($query);
                        $token = $this->weixin_db->fetch_array();
                        if($token){
                            $a=my_curl_wx_api('https://api.cacos.com/user/getUserMiddle?token='.$token[0]['key'],'POST');
                            $user_mini_info=$this->mini_db->get_one(array('phoneNumber'=>$user_info['telephone']));
                            $userid=$user_mini_info['userid'];
                        }
                    }else{
                        echo sprintf($textTpl,'请重新登录~~~');exit;
                    }
                    if($userid==0){
                        echo sprintf($textTpl,'你还没注册哦~~~');exit;
                    }
                    $access_token=$this->get_wx_access_token();
                    $mediaid = $this->upload_media(PHPCMS_PATH.'statics/images/wechatImg/head_user/'.$userid.'.png');
                    $dataimg = '{"touser":"'.$fromUsername.'", "msgtype":"image", "image":{"media_id":"'.$mediaid.'"}}';
                    $post_token_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
                    $imgreturn = $this->my_curl_wx_api($post_token_url, 'POST', $dataimg);
                    if($imgreturn['errcode']){
                        echo sprintf($textTpl, $imgreturn['errcode']);
                    }
                    echo "";
                    exit;
                }

}
public function upload_media($path){		
		$uploadUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->accesstoken."&type=image";	
		$obj = new CurlFile($path);	
		$obj->setMimeType("image/jpeg");		
		$data['media'] = $obj; 
		$imgInfo = my_curl_wx_api($uploadUrl, 'POST', $data);
		return $imgInfo['media_id']; 		
	}

function my_curl_wx_api($url, $type = 'POST', $data = array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    if($data){
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
	if(isset($data['file']) && $data['file']){
		curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false);
	}
    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result, true);
}
public function get_wx_access_token()
    {
        //将access_token存在session/cookie中
        //如果access_token在session中并没有过期
        if (isset($_SESSION['access_token']) && $_SESSION['expire_time'] > time()) {
            return $_SESSION['access_token'];
        }
        //如果access_token不存在或者已经过期,重新获取access_token
        else {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;
            $arr = $this->http_curl($url);
            //存到session中
            $access_token             = $arr['access_token'];
            $_SESSION['access_token'] = $arr['access_token'];
            $_SESSION['expire_time']  = time() + 7200;
            return $access_token;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值