【问题标题】:Laravel File Storage: How to store (decoded) base64 image?Laravel 文件存储:如何存储(解码)base64 图像?
【发布时间】:2018-12-20 03:49:06
【问题描述】:

如何使用Laravel's filesytem (File Storage)方法存储base64图像?

例如,我可以像这样解码 base64 图像:

base64_decode($encoded_image);

但所有 Laravel 存储文件的方法都可以接受 Illuminate\Http\FileIlluminate\Http\UploadedFile 实例。

所以我想我必须将 base64 图像(或解码的 base64 图像)转换为 Illuminate\Http\FileIlluminate\Http\UploadedFile,但是如何?

【问题讨论】:

    标签: php laravel base64 laravel-filesystem laravel-storage


    【解决方案1】:

    只需使用put 来存储编码的内容:

    Storage::put('file.jpg', $encoded_image);
    

    它所做的只是包装file_put_contents

    然后再读出来:

    $data = base64_decode(Storage::get('file.jpg'));
    

    你猜对了,它包裹了file_get_contents

    【讨论】:

    • 我们也可以对pdf文件使用base64编码吗?
    【解决方案2】:

    你可以像这样使用 laravel 文件存储上传你的 base64 图像

        $base64_image = $request->input('base64_image'); // your base64 encoded     
        @list($type, $file_data) = explode(';', $base64_image);
        @list(, $file_data) = explode(',', $file_data); 
        $imageName = str_random(10).'.'.'png';   
        Storage::disk('local')->put($imageName, base64_decode($file_data));
    

    希望对你有帮助

    【讨论】:

    • 你可以去掉disk('local'),因为默认值是'local'Storage::put($imageName, base64_decode($file_data));
    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2016-09-18
    • 2018-08-28
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多