替換編碼頭 preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_img,$res); $base64_img=base64_decode(str_replace($res[1],'', $base64_img)); 存儲圖片 $ans=Storage::disk('user_img')->put($new_file, $base64_img); 或 if (file_put_contents($new_file,base64_decode(str_replace($res[1],'', $imgBase64))))
1 //一、上傳base64位的圖片:用戶的頭像 2 public function uploader_img_base64(Request $request){ 3 4 //用戶id 5 $u_id=Auth::guard('user')->user()->id; 6 if(!$u_id){ 7 return ['valid' => 0, 'message' => '用戶信息過時,請刷新頁面重試!!']; 8 } 9 10 $base64_img=$request->input('base64_img'); 11 $base64_img=urldecode($base64_img); 12 //獲取原圖片的相對路徑 13 $old_user_img_url=$request->input('old_user_img_url'); 14 $position=stripos($old_user_img_url,'user_img/'); 15 if(strlen($old_user_img_url)>$position+9) 16 $old_user_img_url_relative=substr($old_user_img_url,$position+9); 17 18 preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_img,$res); 19 20 21 if (isset($res[2])) { 22 //獲取圖片類型 23 $type = $res[2]; 24 25 //圖片保存路徑 26 $new_file = ''.date('Ym',time()).'/'; 27 //$new_file = "attachment/user_img/".date('Ymd',time()).'/'; 28 29 //圖片名字 30 $new_file = $new_file.str_random(10).mt_rand(10000,99999).'.'.$type; 31 $base64_img=base64_decode(str_replace($res[1],'', $base64_img)); 32 //dd($base64_img); 33 $ans=Storage::disk('user_img')->put($new_file, $base64_img); 34 35 if ($ans) { 36 $ans2=Storage::disk('user_img')->delete($old_user_img_url_relative.''); 37 //dd($ans2); 38 //$img_url=asset('attachment/user_img/'.$new_file); 39 $img_url='attachment/user_img/'.$new_file; 40 //修改用戶的頭像 41 User::where('id',$u_id)->update(['picture'=>$img_url]); 42 43 //unlink($old_user_img_url_relative.''); 44 return ['valid' => 1, 'message' => $img_url]; 45 } else { 46 return ['valid' => 0, 'message' => '上傳失敗請重試!']; 47 } 48 49 }else{ 50 return ['valid' => 0, 'message' => '上傳失敗請重試!!']; 51 } 52 53 }
轉自或參考:laravel 存儲前端上傳base64圖片
https://blog.csdn.net/qq_23953793/article/details/103064293php
用postman模擬前端Android 上傳圖文信息
html
PHP laravel後端代碼前端
public function store(Request $request) { $moment = new Moment(); $moment->title = $request->get('title'); $moment->date = $request->get('date'); $moment->description = $request->get('description'); $moment->user_id = $request->get('user_id'); $image = $request->get('image_base64'); // your base64 encoded $image = str_replace('data:image/png;base64,', '', $image); $image = str_replace(' ', '+', $image); $imagePath= 'images/'.str_random(10) . '.png'; Storage::disk('public')->put($imagePath, base64_decode($image)); $moment->image_base64 = $imagePath; $moment->save(); return response()->json([ 'code' => 1, 'message' => 'success', 'date' => '' ]); }
數據庫:
目錄存儲:
laravel
轉自或參考:php將base64字符串轉換爲圖片
https://www.cnblogs.com/yexiaocangji/p/7690688.html數據庫
一些插件產生的圖片數據是base64格式的,好比cropper,當咱們要上傳到服務器想要保存爲圖片文件的時候,能夠用如下方法json
//獲取圖片base64字符串 $imgBase64 = $_REQUEST['img']; if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$imgBase64,$res)) { //獲取圖片類型 $type = $res[2]; //圖片保存路徑 $new_file = "static/images/".date('Ymd',time()).'/'; if (!file_exists($new_file)) { mkdir($new_file,0755,true); } //圖片名字 $new_file = $new_file.time().'.'.$type; if (file_put_contents($new_file,base64_decode(str_replace($res[1],'', $imgBase64)))) { $msg['msg'] = 'ok'; echo json_encode($msg); } else { $msg['msg'] = 'no'; echo json_encode($msg); } }