//点击触发logoUp function logoUp(obj) { wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function(res) { localId = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 for(i = 0; i < localId.length; i++) { wx.uploadImage({ localId: localId[i].toString(), // 需要上传的图片的本地ID,由chooseImage接口获得 isShowProgressTips: 1, // 默认为1,显示进度提示 success: function(res) { var serverId = ''; serverId += res.serverId + ","; // 返回图片的服务器端ID //请求自己的服务器通过serverId获取微信服务器中的真实图片 $.post(location.href, { "type": "preview", "mediaids": serverId }, function(res) { for(var i = 0; i < res.img_s.length; i++) { $(obj).empty(); $(obj).append('<img class="thumb" src='+"{$siteroot}"+res.img_s[i]+' data='+res.img_s[i]+'>'); } }) } }); } } }); }
$mediaid=$_GPC['mediaids']; $mediaids = substr($mediaid,0,-1); $filename=substr($this->downloadImage($mediaids),0,-1); $filenames = explode(',',$filename); if($_GPC['type']=="preview"){ header('content-type:application/json;charset=utf8'); $res['img_s'] = $filenames; exit(json_encode($res)); }
//下载图片 public function downloadImage($mediaids) { global $_W; $uniacid = $_W['uniacid']; load()->func('file'); $account = WeAccount::create($_W['account']); $mediaarray=explode(",",$mediaids); $filenames=""; foreach($mediaarray as $mediaid){ $filename = 'FMFetchi'.date('YmdHis').random(16); $access_token = $account->fetch_token(); $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$mediaid"; $fileInfo = $this->downloadWeixinFile($url); $updir = '../attachment/images/'.$uniacid.'/'.date("Y").'/'.date("m").'/'; if(!is_dir($updir)){ mkdirs($updir); } //$filename= $updir.$filename.".jpg"; //个人新增替换解决不同后缀问题 $filename= $updir.$filename.cType2suffix($fileInfo['content_type']); $this->saveWeixinFile($filename, $fileInfo["body"]); $filenames.=$filename.","; } return $filenames; } public function downloadWeixinFile($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $package = curl_exec($ch); $httpinfo = curl_getinfo($ch); curl_close($ch); $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package)); return $imageAll; } public function saveWeixinFile($filename, $filecontent) { $local_file = fopen($filename, 'w'); if (false !== $local_file){ if (false !== fwrite($local_file, $filecontent)) { fclose($local_file); } } } //个人新增 //通过Content-Type转后缀 function cType2suffix($ctype){ switch ($ctype) { case "image/gif" : $suffix = '.gif'; break; case "image/jpeg" : $suffix = '.jpg'; break; case "image/png" : $suffix = '.png'; break; default : $suffix = '.jpg'; break; } return $suffix; }
实现方式代码来源于微擎付费阅读模块。
如果本文对你有帮助,欢迎打赏本站