如何在 Laravel 5.3 中解码 base64

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41690241/
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 15:08:09  来源:igfitidea点击:

How to decode base64 in laravel 5.3

phplaravelapibase64laravel-5.3

提问by Mr Robot

i am working on an API for mobile application, from mobile end i'm getting the image in base64format and inserting it in profile_imagecolumn in my database

我正在开发用于移动应用程序的 API,从移动端获取图像base64格式并将其插入profile_image到我的列中database

in my web i want to display that image, so how do i decode it

在我的网络中,我想显示该图像,那么我如何对其进行解码

<div class="card horizontal card-driver-details">
    <div class="card-image card-circular">
        <img src="{{ $driver->profile_image}} "> //i want to display here
    </div>
    <div class="card-stacked">
        <div class="card-content">
            <p><b>Name:</b>{{ $driver->first_name }} {{ $driver->last_name }}</p>
            <p><b>Phone:</b>{{ $driver->phone_number }}</p>
            <p><b>Address:</b>{{ $driver->addess_city_village }} {{ $driver->address_state }}</p>
        </div>
    </div>
</div>

thank you

谢谢你

回答by Hassaan

Try

尝试

base64_decode($driver->profile_image);

回答by Arun Code

Try this one.

试试这个。

{{ base64_decode($driver->profile_image) }}

This will decode the profile_imageusing base 64 and then print the results.

这将profile_image使用 base 64解码,然后打印结果。

回答by Mayank Pandeyz

Try this:

尝试这个:

$image = base64_decode('base_64_image_string');
$fp = fopen('path_to_where_you_want_to_save_image','wb+');
fwrite($fp,$image);
fclose($fp);

回答by Mr Robot

i tried with the following code but din't work

我尝试使用以下代码但不起作用

base64_decode($driver->profile_image);

then did some browsing about this then find out now all the updated browser support data:image/jpeg/png/gif/etc

然后做了一些浏览然后现在找出所有更新的浏览器支持 data:image/jpeg/png/gif/etc

so now i am able to display the image

所以现在我可以显示图像了

$driver->profile_image

回答by basith

All types of file can be solved by using this ,

使用这个可以解决所有类型的文件,

  $image_64 = $data['photo_url']; //your base64 encoded data

  $extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1];   // .jpg .png .pdf

  $replace = substr($image_64, 0, strpos($image_64, ',')+1); 

// find substring fro replace here eg: data:image/png;base64,

 $image = str_replace($replace, '', $image_64); 

 $image = str_replace(' ', '+', $image); 

 $imageName = Str::random(10).'.'.$extension;

 Storage::disk('public')->put($imageName, base64_decode($image));