调用微信jssdk图像接口进行图片上传,并将图像保存到自己的服务器。

首先引入微信的jssdk包,具体引入方法就请查看官方文档如下,就不详细解说了。
“https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115”;

需要用到的接口有’chooseImage’,‘uploadImage’,‘downloadImage’,三个接口,当出现如下图证明成功调用jssdkjssdk权限
在html的“script”中写方法

function album(){
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function (res) {
            var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
            // alert(localIds);
            wx.uploadImage({
                localId: localIds.toString(), // 需要上传的图片的本地ID,由chooseImage接口获得
                isShowProgressTips: 1, // 默认为1,显示进度提示
                success: function (data) {
                    mediaId = data.serverId; // 返回图片的服务器端ID
					//console.log(mediaId);
                    $.ajax({
                        "type":"post",
                        "url":"{:url('index/avatar')}",
                        "dataType":"json",
                        "data":{
                            "mediaId":mediaId,
						},
                        "success":function(res){
                            // alert(JSON.stringify(res));
                            if(res.status==1){
                                mui.toast(res.msg);
                                mui('#upload_list').popover('hide', document.getElementById("upload_images")); //隐藏
                                $("#loadimg").load(location.href + " #loadimg");
							}else {
                                mui.toast(res.msg);
                                mui('#upload_list').popover('hide', document.getElementById("upload_images")); //隐藏
                                $("#loadimg").load(location.href + " #loadimg");
							}
                        }
                    });
                },
                fail: function (error) {
                    alert(JSON.stringify(error));

                }
            });
        }
    });
}

在php文件中如下:

public function avatar(){if($this->request->isPost()){
            $mediaId = $this->request->param("mediaId");
            if($mediaId){
                $accessToken = $this->weChat()->access_token; // EasyWeChat\Core\AccessToken 实例
                $token = $accessToken->getToken(); // token 字符串
                $access_token = $token['access_token']; // token 字符串
                $upload_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$mediaId";
            
                $result = get_by_curl_upload($upload_url);
                $rs = db('user')
                    ->where('id', $info['id'])
                    ->setField('avatar', $result);

                if($rs){
                    return json(['status'=>1,'msg'=>'修改成功!']);
                }else{
                    return json(['status'=>0,'msg'=>'修改失败!']);
                }

            }
        }
}

调用的公共方法 get_by_curl_upload如下:

function get_by_curl_upload($url){
	    $dephp_14 = curl_init($url);
	    curl_setopt($dephp_14, CURLOPT_HEADER, 0);
	    curl_setopt($dephp_14, CURLOPT_NOBODY, 0);
	    curl_setopt($dephp_14, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($dephp_14, CURLOPT_SSL_VERIFYHOST, FALSE);
	    curl_setopt($dephp_14, CURLOPT_RETURNTRANSFER, 1);
	    $dephp_15 = curl_exec($dephp_14);
	    $dephp_16 = curl_getinfo($dephp_14);
	    curl_close($dephp_14);
	    $dephp_17 = array_merge(array('header' => $dephp_16), array('body' => $dephp_15));
	    switch ($dephp_17['header']['content_type']){
	        case 'image/pjpeg' : case 'image/jpeg' : $dephp_18 = '.jpg';
	        break;
	        case 'image/gif' : $dephp_18 = '.gif';
	            break;
	        case 'image/png' : $dephp_18 = '.png';
	            break;
	    }
	
	    $dephp_19 = date('mdHis') . mt_rand(1000,9999) . $dephp_18;
	    $dephp_21 = '要保存的图片路径/';
	    $dephp_22 = ROOT_PATH.'public/upload/'.$dephp_21;
	    $dephp_23 = $dephp_22 . $dephp_19;
	    $dephp_24 = $dephp_17['body'];
	  	$dephp_25 = true;
	    if(!is_dir($dephp_22)) {
	        $dephp_25 = mkdir($dephp_22, 0777, true);
	    }
	  	//echo $dephp_22;exit;
	    if(false !== $dephp_25){
	        $dephp_26 = fopen($dephp_23, 'w');
	      	
	        if (false !== $dephp_26){
	            if (false !== fwrite($dephp_26, $dephp_24)){
	                fclose($dephp_26);
	                $dephp_27 = $dephp_21 . $dephp_19;
	                return $dephp_27;
	            }
	        }else{
	            return 0;
	        }
	    }else{
	        return 0;
	    }
    }

如上为调用微信图像上传的所有代码。

你可能感兴趣的:(调用微信jssdk图像接口进行图片上传,并将图像保存到自己的服务器。)