base64图片上传处理方式

前台传图片的base64格式,后台处理方式
//处理图片信息 返回对应的路径
public function uploadBaseIma($imgArr){
$result = array();
//将路径直接设置在uploads下
if($imgArr){
foreach($imgArr as $k => $v){
if($v){
//判断上传的是否为原地址
$a = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $v);
if($a){
//是就存储不变,将文件路径存储
$b = substr($v,36);
$result[$k] = $b;
}else{
//不是就上传图片
$address = $this->baseChangeImg($v);
$result[$k] = $address;
}
}else {
return array();
}
}
return $result;
}
}
//base64转化为本地图片
public function baseChangeImg($base)
{
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base, $result)) {
$type = $result[2];
$path = '/base' . date('Ymd', time()) . "/";
$new_file = public_path('uploads') . $path;
if (!file_exists($new_file)) {
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$name = uniqid() . ".{$type}";
$new_file = $new_file . $name;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base)))) {
return $path . $name;
} else {
return false;
}
}
}

猜你喜欢

转载自www.cnblogs.com/lvfish/p/9178628.html
今日推荐