2014-11-06 103 views
6

我通過Ajax向base64中的控制器發送png圖像文件。我已經測試並確定控制器已收到ID,但仍無法將其保存到公用文件夾。Laravel:將Base64 .png文件保存到控制器的公用文件夾中

這裏是我的控制器

public function postTest() { 
     $data = Input::all(); 

     //get the base-64 from data 
     $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1); 

     //decode base64 string 
     $image = base64_decode($base64_str); 
     $png_url = "product-".time().".png"; 
     $path = public_path('img/designs/' . $png_url); 

     Image::make($image->getRealPath())->save($path); 
     // I've tried using 
     // $result = file_put_contents($path, $image); 
     // too but still not working 

     $response = array(
      'status' => 'success', 
     ); 
     return Response::json($response ); 
} 

回答

4

這是一個容易犯的錯誤。

您正在使用public_path不正確。它應該是:

$path = public_path() . "/img/designs/" . $png_url; 

此外,我會避免你的方法發送圖像。查看錶單中的正確上載並使用Laravel的Input :: file方法。

+0

感謝您的迴應!我總是這樣使用public_path(),它的工作正常。我認爲這個問題也是Input :: file方法。所以現在我放棄了使用它:P。檢查我的答案! – Expl0de 2014-11-06 18:12:44

+0

路徑不是問題,也不是必需的,只有'$ path =「/ img/designs /」。 $ png_url'就夠了。 – 2014-11-06 18:28:43

0

我已經完成了!我將$data->base64_image更改爲$_POST['base64_image']。並且使用$result = file_put_contents($path, $image);而不是Image::make($image->getRealPath())->save($path);但是這看起來並不像一個簡單的方法。我有另一種看起來更優雅的方式,請告訴我!

+0

你的問題是'$ data-> base64_image',它應該是'$ data ['base64_image']',而不是檢查我的答案。 – 2014-11-06 18:26:28

0

其實,Input::all()所以你有以下返回輸入的array

$data = Input::all(); 

現在你$data是一個數組不是一個對象,所以你試圖訪問圖像像一個對象:

$data->base64_image 

所以,它不工作。你應該嘗試使用:

$image = $data['base64_image']; 

因爲它是(base64_image)訪問從$_POST然後Input::file('base64_image')不會因爲工作Input::file('base64_image')檢查$_FILES陣列,它是不是有你的情況。

+0

感謝您指出這一點!我用'$ result = file_put_contents($ path,$ image)來使用你的方法; '現在png圖像已經建成。但文件不可讀。 '$ data ['base64_image']'已經是一個可保存的文件嗎? – Expl0de 2014-11-06 18:38:56

+0

可能不是,你使用'$ base64_str'嗎? – 2014-11-06 18:47:57

+1

是的,但仍然無法讀取。僅供參考,base64_image看起來像這樣「data:image/png:base64,iVBORwOkG .....」。'$ img = $ data ['fileo'];' – Expl0de 2014-11-06 19:01:22

2
$data = Input::all(); 
$png_url = "perfil-".time().".jpg"; 
$path = public_path() . "/img/designs/" . $png_url; 
$img = $data['fileo']; 
$img = substr($img, strpos($img, ",")+1); 
$data = base64_decode($img); 
$success = file_put_contents($path, $data); 
print $success ? $png_url : 'Unable to save the file.'; 
+0

';' $ data ['fileo']代表什麼? – 2016-08-17 14:25:20

6

干預形象得到使用file_get_content功能的二進制數據: 參考:http://image.intervention.io/api/make

你的控制器應該是這個樣子:

public function postTest() { 
    $data = Input::all(); 
    $png_url = "product-".time().".png"; 
    $path = public_path().'img/designs/' . $png_url; 

    Image::make(file_get_contents($data->base64_image))->save($path);  
    $response = array(
     'status' => 'success', 
    ); 
    return Response::json($response ); 
} 
0

我在做什麼用的基本方式

$file = base64_decode($request['profile_pic']); 
      $folderName = '/uploads/users/'; 
      $safeName = str_random(10).'.'.'png'; 
      $destinationPath = public_path() . $folderName; 
      file_put_contents(public_path().'/uploads/users/'.$safeName, $file); 

      //save new file path into db 
      $userObj->profile_pic = $safeName; 
     } 
0

我的解決方案是:

public function postTest() { 
     $data = Input::all(); 

     //get the base-64 from data 
     $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1); 

     //decode base64 string 
     $image = base64_decode($base64_str); 
     Storage::disk('local')->put('imgage.png', $image); 
     $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix(); 
     echo $storagePath.'imgage.png'; 
     $response = array(
      'status' => 'success', 
     ); 
     return Response::json($response ); 
} 
相關問題