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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48153166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:13:15  来源:igfitidea点击:

How to convert base64 to image in Laravel 5.4?

laravel

提问by Simon Shrestha

I am developing api in Laravel 5.4. I will receive the image in base64 format. How can I convert the base64 to image in Laravel?

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

回答by Simon Shrestha

I found the solution:

我找到了解决方案:

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));
   }
}

回答by Yehia Salah

this solution will deal with all image types

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

 $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));