2017-05-31 40 views
0

我正在寫一個android應用程序的web服務。在手機上,我發送了需要解碼並保存爲jpg/jpeg/png(任意一種)格式的圖像。如何解碼並保存作爲base64字符串數組發送到服務器目錄的多個圖像?

將發送的數據將是一個base64數組,我需要在後端處理它,並將其保存到服務器目錄中供將來參考。

要保存圖片我的代碼看起來像這樣。 (這只是一個測試方法)

public function testing(Request $request){ 
    $i=0; 
    foreach($request['images'] as $name) 
    { 
     $i +=1; 

     $imagename= 'Image'.$i.'.jpeg'; 
     $destinationPath = public_path('images'); 
     $path = 'images/'.$imagename; 
     $res = file_put_contents($path, base64_decode($name)); 
    } 
} 

然而,此代碼將創建JPEG擴展的圖像時,我嘗試打開它只是給我這個: enter image description here

我想我失去了一些東西。

[上面的代碼是剛剛從上傳應用正確的文件的演示。]

回答

0

對不起,打擾。我通過添加一些代碼來修復它。

$i=0; 
    foreach($request['images'] as $name) 
    { 

     list($type, $name) = explode(';', $name); 
     list(, $name)  = explode(',', $name); 
     $data = base64_decode($name); 
     $i +=1; 

     $imagename= 'Image'.$i.'.jpeg'; 
     $destinationPath = public_path('images'); 
     $path = 'images/'.$imagename; 
     $res = file_put_contents($path, $data); 

     /*$saveInlistingimages = DB::insert('INSERT INTO listingimages (Listing, Image) values (?, ?)', [$lastId, $path]);*/ 

    } 
相關問題