如何在Laravel中移动base64解码图像(到目录)?

时间:2016-01-19 12:19:15

标签: php laravel laravel-5.1

我有一个表单,我在base64encode中上传图片。

<input type=hidden value=.....(base64encode string) />

在Laravel的方法中(我处理输入数据)我有

$data = Request::all(); // get all input data
$img = base64_decode($data["img"]); // my decoded image

如何在(任何)目录中移动此解码图像?在Laravel的手册中,我读到了

$request->file('photo')->move($destinationPath);

但我不知道如何在我的情况下使用它。

2 个答案:

答案 0 :(得分:1)

如果有人仍在寻找这是我的解决方案 我一直在寻找一种在本地(在浏览器中)优化图像然后上传它的方法; 这就是我在laravel应用程序中收到它的方式:

Route::post('myroute', function(Illuminate\Http\Request $data){

   $base64_str = substr($data->image, strpos($data->image, ",")+1); // get the image code
   $image = base64_decode($base64_str); // decode the image
   file_put_contents('/path/newImage.JPG',$image); // move the image to the desired path with desired name and extension  

)};

答案 1 :(得分:0)

$imageName = rand(11111, 99999) . '_' . time() . '.jpg';
$path = 'uploads/'.$directory_name."/". $imageName;
Storage::disk('local')->put($path, base64_decode($image));

试试这个,你可以从你的配置/文件系统更改/创建磁盘