jssdk上传和下载微信媒体资源
jssdk下载微信服务器的资源
下载图片
public function downloadImg($type=".png"){
$access_token = $this->get_access_token();
$media_id = I('media_id');
// 要存在你服务器哪个位置?
$targetName = './Uploads/Weixin/'.date('Y_m_d').'/';//以日期来创建一层目录
$filename = time();
$filepath = C('Site_Url').'Uploads/Weixin/'.date('Y_m_d').'/'.$filename;
if(!file_exists($targetName)){
mkdir($targetName, 0777, true);
}
$timg = $targetName.$filename;
$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
$ch = curl_init();
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$fp = fopen($timg.$type,'wb'); //'wb'表示已二进制方式打开新建
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FILE,$fp);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($hander,CURLOPT_TIMEOUT,60);
$outpot = curl_exec($ch);
curl_close($ch);
fclose($fp);
if( curl_errno($ch) ){
$res = array (
"code" => 1,
"msg" => "下载失败",
"data" => '',
"media_id"=>$media_id,
);
$this->ajaxReturn($res);
}else{
$this->tpic($timg);
$res = array (
"code" => 0,
"msg" => "成功",
"data" => $filepath,
"media_id"=>$media_id,
"test1"=>$timg.$type
);
$this->ajaxReturn($res);
}
} 图片加水印并压缩
public function tpic($timg){
//压缩图片+水印
$image = new \Think\Image();
$image->open($timg.'.png')->thumb(200, 200,3)->save($timg.'_s.png');
$image->open($timg.'.png')->water('./Public/images/logos.png',9,50)->save($timg.'.png');
} 下载音频文件
public function downloadFile($type=".amr"){
$access_token = $this->get_access_token();
$media_id = I('media_id');
$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE); //需要response header
curl_setopt($ch, CURLOPT_NOBODY, FALSE); //需要response body
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($hander,CURLOPT_TIMEOUT,60);
$response = curl_exec($ch);
//分离header与body
$header = '';
$body = '';
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
}
curl_close($ch);
//文件名
if (preg_match('/filename="(.*?)"/', $header, $arr)) {
$targetName = './Uploads/Weixin/'.date('Y_m_d').'/';//以日期来创建一层目录
$filename = time();
$filepath = C('Site_Url').'Uploads/Weixin/'.date('Y_m_d').'/'.$filename;
if(!file_exists($targetName)){
mkdir($targetName, 0777, true);
}
$timg = $targetName.$filename;
if (file_put_contents($timg, $body)) {
$res = array (
"code" => 0,
"msg" => "成功",
"data" => $filepath,
"media_id"=>$media_id,
"test1"=>$timg.$type
);
$this->ajaxReturn($res);
}else{
$res = array (
"code" => 1,
"msg" => "下载失败",
"data" => '',
"media_id"=>$media_id,
);
$this->ajaxReturn($res);
}
}
} 下载base64图片
public function uploadbase64Img($thumb=TRUE,$thumbtype=3) {
$data =$_POST['data'];
$data = trim($data);
if(empty($data)){
echo '{"code":0,"msg":"图片数据为空!"}';
exit();
}
$imgname = uniqid () . '.png';; //图片保存名称
$new_file = 'uploads/'. date('Ymd').'/';
if (!file_exists($new_file)) {
mkdir($new_file, 0777, true);
}
$base64_image_content = chunk_split($data);
if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$data, $result)) {
$type = $result[2];
$new_file = './'.$new_file. $imgname;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
$imgattr=getimagesize($new_file);//校验下图片是否生成
if ($imgattr) {
imageWaterMark ( $new_file, $wx ['name'] );
}else{
echo '{"code":0,"msg":"数据转图片失败!"}';
exit();
}
if (!empty ($thumb)) {
$image = new Image(); // 实例化图片类对象
if ($imgattr) {
$image = $image->open ( $new_file );
$image_thumb = $image->thumb ( 260, 260, $thumbtype);
$thunbname = str_replace('.png', '_t.png', $imgname);
$imgpath_thumb = 'uploads/'. date('Ymd').'/'. $thunbname;
$image->save ( $imgpath_thumb );
}
}
} else {
echo '{"code":0,"msg":"上传失败!"}';
exit();
}
}
if ($new_file) {
$res ['code'] = 0;
$res ['msg'] = '成功';
$res ['data'] = $new_file;
}else{
$res ['code'] = 101;
$res ['msg'] = '失败';
}
echo json_encode ( $res );
}