php微信拍照图库js接口,ThinkPHP微信实例——JSSDK图像接口多张图片上传下载并将图片流写入本地...
最多可添加9張作品
發布
wx.config({
debug: false,
appId: '{$signPackage["appid"]}',
timestamp: {$signPackage["timestamp"]},
nonceStr: '{$signPackage["noncestr"]}',
signature: '{$signPackage["signature"]}',
jsApiList: [
// 所有要調用的 API 都要加到這個列表中
'chooseImage','previewImage','uploadImage','downloadImage'
]
});
wx.ready(function () {
// 5.3 上傳圖片
$('.uploadImage').on('click', function () {
wx.chooseImage({
success: function (res) {
var localIds = res.localIds;
syncUpload(localIds);
}
});
});
var syncUpload = function(localIds){
var localId = localIds.pop();
wx.uploadImage({
localId: localId,
isShowProgressTips: 1,
success: function (res) {
var serverId = res.serverId; // 返回圖片的服務器端ID
var str = $('#img_str').val()+serverId+',';
$('.imglist').append("");
$('#img_str').val(str);
//其他對serverId做處理的代碼
if(localIds.length > 0){
syncUpload(localIds);
}
if($('.imglist img').size() >= 9) {
$("#upload_button").hide();
}
},
fail: function (res) { alert(JSON.stringify(res)); }
});
};
});
$(".sb").click(function(){
var imglist = $(".imglist").html();
if(imglist == "" || imglist == null) {
alert("請添加圖片");
return false;
} else {
if($(".imglist").find('img').size() >9) {
alert("圖片只允許上傳9張!");
return false;
}
var img_str = $('#img_str').val();
}
$.post("{:U('Test/wxupload')}", {img_str:img_str},function(data){
window.location.href = "__URL__/test";
});
});
后臺處理 testController.php
public function test() {
$signPackage = $this->getSignPackage();
$this->assign('signPackage', $signPackage);
$this->display('test');
}
public function wxupload() {
header('Content-type:application/json;chartset=utf-8');
$img_str = I('post.img_str', '', 'string');
$uploadROOT = realpath(THINK_PATH.'../Public/');//定義保存路徑
$uploadPath = $uploadROOT."/Upload/".date('Y-m-d')."/";//為方便管理圖片 保存圖片時 已時間作一層目錄作區分
if(!file_exists($uploadPath)) mkdir($uploadPath, 0775);
$savepath = '/Public/Upload/' . date('Y-m-d').'/';
$img_str = rtrim($img_str, ',');
$img_arr = explode(',', $img_str);
$str = ''; //sql語句字符串
$imgs = array();
foreach($img_arr as $v) {
$imgs[] = $this->doWechatPic($v);
}
$str = ''; //sql語句字符串
foreach($imgs as $v) {
$str .= "(NULL," . $this->_user_info_id . "," . $publish_id . ",'" . $v . "')" . ",";
}
$sql = "INSERT INTO img(`id`,`userid`,`publish_id`,`img_url`) values" . rtrim($str, ',');
$res = M()->query($sql);
if($res) {
$data = array('errcode' => 0, 'msg' => '成功!');
} else {
$data = array('errcode' => 1, 'msg' => '失敗!');
}
exit( JSON($data));
}
/*
* 從微信服務器獲取圖片流
*/
public function doWechatPic($serverId){//media_id=jlJs_iQIOA-TKLuhk4nCdPEdXnJ6paIeToO8vr-WUGvz05-6i5n498EzI232xSxn
$media_id = $serverId;//提交過來的serverId即$media_id
$access_token = $this->_get_wx_access_token_address( false );//獲取access_token值
$pic_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
$filebody = file_get_contents($pic_url);//通過接口獲取圖片流
$filename = uniqid().'.jpg'; //定義圖片名字及格式
return $this->saveFile($filename, $filebody);
}
/*
* 定義文件路徑,寫入圖片流
*/
public function saveFile($filename, $filecontent){
$uploadROOT = realpath(THINK_PATH.'../Public/');//定義保存路徑
$uploadPath = $uploadROOT."/Upload/".date('Y-m-d')."/";//為方便管理圖片 保存圖片時 已時間作一層目錄作區分
if(!file_exists($uploadPath)) mkdir($uploadPath, 0775);
$upload_dir = '/Public/Upload/' . date('Y-m-d'); //保存路徑,以時間作目錄分層
$savepath = '.'.$upload_dir.'/'.$filename;
if(file_put_contents($savepath, $filecontent)){//寫入圖片流生成圖片
return $upload_dir."/".$filename;//返回圖片路徑
}else{
exit(JSON('save failed'));
}
}
需要注意的是 access_token,signPackage參數不能錯誤
總結
以上是生活随笔為你收集整理的php微信拍照图库js接口,ThinkPHP微信实例——JSSDK图像接口多张图片上传下载并将图片流写入本地...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 两张表差集_mysql中两张
- 下一篇: linux系统创建操作系统用户,linu