如何将base64转换为Laravel 5.4中的图像?

时间:2018-01-08 15:20:14

标签: laravel

我正在Laravel 5.4中开发api。我将收到base64格式的图像。如何在Laravel中将base64转换为图像?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

use Illuminate\Support\Facades\Storage;

function createImageFromBase64(Request $request)
{
   $file_data = $request->input('image_file');
   $file_name = 'image_' . time() . '.png'; //generating unique file name;

   if ($file_data != "") { // storing image in storage/app/public Folder
       Storage::disk('public')->put($file_name, base64_decode($file_data));
   }
}

答案 1 :(得分:0)

此解决方案将处理所有图像类型

 $image = $request->input('image'); // image base64 encoded
 preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
 $image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
 $image = str_replace(' ', '+', $image);
 $imageName = 'image_' . time() . '.' . $image_extension[1]; //generating unique file name;
 Storage::disk('public')->put($imageName,base64_decode($image));