//兼容苹果手机的SDK微信上传图片
wx.config({
// debug: true,
appId: '<?php echo $confinfo["appId"];?>',
timestamp: '<?php echo $confinfo["timestamp"];?>',
nonceStr: '<?php echo $confinfo["nonceStr"];?>',
signature: '<?php echo $confinfo["signature"];?>',
jsApiList: [
'checkJsApi',
'chooseImage',
'uploadImage',
'downloadImage',
'previewImage'
]
});
wx.ready(function () {
var images = {
localId: [],
serverId: [],
downloadId: []
};
document.querySelector('#selectImage').onclick =function () {
wx.chooseImage({
success: function (res) {
images.localId = res.localIds;
setCookie("localId",res.localIds);
jQuery(function(){
var i=images.localId.length;
var iCount=setInterval(function(){
if(i>0){
var n=images.localId[i-1];
wx.uploadImage({
localId: images.localId[i-1],
success: function (res) {
images.serverId.push(res.serverId);
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}else{
clearInterval(iCount);
}
i--;
}, 800);
});
}
});
};
//俩种处理方法 1直接上传到七牛
require_once APP_ROOT.'/vendor/config.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
$accessToken = $this->get_accesstoken($pt['appid'], $pt['appsecret']);
$media_id = $pic;
$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=".$pic."";
$images[]=qiniu_imgurl($url);
/*
*
* @desc URL安全形式的base64编码
* @param string $str
* @return string
*/
function urlsafe_base64_encode($str){
$find = array("+","/");
$replace = array("-", "_");
return str_replace($find, $replace, base64_encode($str));
}
/**
* generate_access_token
*
* @desc 签名运算
* @param string $access_key
* @param string $secret_key
* @param string $url
* @param array $params
* @return string
*/
function generate_access_token($access_key, $secret_key, $url, $params = ''){
$parsed_url = parse_url($url);
$path = $parsed_url['path'];
$access = $path;
if (isset($parsed_url['query'])) {
$access .= "?" . $parsed_url['query'];
}
$access .= "\n";
if($params){
if (is_array($params)){
$params = http_build_query($params);
}
$access .= $params;
}
$digest = hash_hmac('sha1', $access, $secret_key, true);
return $access_key.':'.urlsafe_base64_encode($digest);
}
function send($url, $header = '') {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER,1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
$con = curl_exec($curl);
if ($con === false) {
echo 'CURL ERROR: ' . curl_error($curl);
} else {
return $con;
}
}
function qiniu_imgurl($url){
$access_key = Config::AK;
$secret_key = Config::SK;
$fetch = urlsafe_base64_encode($url);
$name= date('YmdHis', time()) . substr($msec, 2, 4).".jpg";
$to = urlsafe_base64_encode(Config::BUCKET_IMG_NAME.':'.$name); //格式为 news2v:123.jpg
$url = 'http://iovip.qbox.me/fetch/'. $fetch .'/to/' . $to;
$access_token = generate_access_token($access_key, $secret_key, $url);
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: QBox '. $access_token;
$con = send('iovip.qbox.me/fetch/'.$fetch.'/to/'.$to, $header);
return Config::IMG_DOMAIN.$name;
}
//2php页面 下载到本地
$accessToken = $this->get_accesstoken($pt['appid'], $pt['appsecret']);
$media_id = $pic;
// 要存在你服务器哪个位置?
$targetName = "data/upload/".time().rand("1000","9999").".jpg";
$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=".$pic."";
$haha=$this->getImage($url,$targetName,'');
function getImage($url,$filename='',$type=0){
if($url==''){return false;}
if($filename==''){
$ext=strrchr($url,'.');
if($ext!='.gif' && $ext!='.jpg'){return false;}
$filename=time().$ext;
}
//文件保存路径
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
}
$size=strlen($img);
//文件大小
$fp2=@fopen($filename,'a');
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
//兼容苹果手机的SDK微信上传图片 下载到服务器或者上传到七牛
最新推荐文章于 2021-04-01 18:29:31 发布