.net 保存base64 图片_ThinkPHP base64上传图片

最近我们项目需要一台图片服务器存储用户上传的图片,我们使用base64处理和存储图片,下面简单介绍一下如何使用 PHP 上传图片到 服务器,并获取相应图片的返回信息 可以参考一下

dcaec9c0e6641b4cf8f26448e9298e57.png

一:图片上传判断

<?php /*** base64图片上传* @param $base64_img* @return array*/private static function img_upload($base64_img){
$base64_img = trim($base64_img);
$up_dir = '../../../upload/images/interface/';if(!file_exists($up_dir)){
mkdir($up_dir,0777);
}if(preg_match('/^(data:s*image/(w+);base64,)/', $base64_img, $result)){
$type = $result[2];if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
$new_file = $up_dir.date('YmdHis_').method::getRandChar().'.'.$type;if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
$img_path = str_replace('../../..', '', $new_file);
return array('code' => 1, 'msg' => "图片上传成功", 'url' => $img_path);
}
return array('code' => 2, 'msg' => "图片上传失败");
}//文件类型错误
return array('code' => 4, 'msg' => "文件类型错误");
}//文件错误
return array('code' => 3, 'msg' => "文件错误");
}
 二:图片上传到服务器

// 关键在这里!

$post_data = file_get_contents($realpath); // raw_post方式

// 如果是是一个数组,则content_type自动为multipart/form-data

// $post_data = array(

// 'file' => "@$realpath"

// );

$headers = array();

$headers[] = 'Content-Type:'.$ext; // 还有这里!

curl_setopt($ch, CURLOPT_URL, $zimg_upload_url);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$info = curl_exec($ch);

curl_close($ch);

$json = json_decode($info, true);

$signature = $json['info']['md5'];

$imgurl = $zimg_domain . $signature;

return array('imgurl'=>$imgurl,'imgcode'=>$signature);

$post_data = file_get_contents($realpath); // raw_post方式

// 如果是是一个数组,则content_type自动为multipart/form-data

// $post_data = array(

// 'file' => "@$realpath"

// );

$headers = array();

$headers[] = 'Content-Type:'.$ext; // 还有这里!

curl_setopt($ch, CURLOPT_URL, $zimg_upload_url);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$info = curl_exec($ch);

curl_close($ch);

$json = json_decode($info, true);

$signature = $json['info']['md5'];

$imgurl = $zimg_domain . $signature;

return array('imgurl'=>$imgurl,'imgcode'=>$signature);

三:判断

$upFile = $_FILES['file'];

/**

* 创建文件夹函数,用于创建保存文件的文件夹

* @param str $dirPath 文件夹名称

* @return str $dirPath 文件夹名称

*/

function creaDir($dirPath){

$curPath = dirname(__FILE__);

$path = $curPath.''.$dirPath;

if (is_dir($path) || mkdir($path,0777,true)) {

return $dirPath;

}

}

//判断文件是否为空或者出错

if ($upFile['error']==0 && !empty($upFile)) {

$dirpath = creaDir('upload');

$filename = $_FILES['file']['name'];

$queryPath = './'.$dirpath.'/'.$filename;

//move_uploaded_file将浏览器缓存file转移到服务器文件夹

if(move_uploaded_file($_FILES['file']['tmp_name'],$queryPath)){

echo $queryPath;

}

}

?>

80d2df4ccbb634a321850d5267c58315.png

点击上传图片并发送后, 可以看到页面上显示出图片, 查看本地文件夹可以看到文件也已储存到服务器.

在客户端实现异步上传的关键在于FormData,关于这部分这里有详细介绍: FormData()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值