2014-11-06 105 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 ); 
} 
相关问题