首页 > 解决方案 > 从字符串中提取base64图像并以表格形式发布

问题描述

我正在尝试在我的Laravel项目中使用我的表单上传图像。我有一个图像裁剪器,可以将图像保存为data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/..

裁剪器发送一个JSON包含base64文件编码字符串的字符串,我需要将JSON字符串解析为一个对象,然后提取base64字符串并将其转换为文件对象。

Image Intervention用于上传过程

控制器功能

public function store(Request $request)

    {

        // save post in db

        $post = new Post;

        $post->title = $request->title;
        $post->body = $request->body;
        $post->user_id = auth()->id();



        if ($request->hasFile('featimage')) {
            $image = $request->file('featimage');
            $filename = time() . '.' . $image->getCLientOriginalExtension();

            $image = trim($image);
            $image = str_replace('data:image/png;base64,', '', $image);
            $image = str_replace('data:image/jpg;base64,', '', $image);
            $image = str_replace('data:image/jpeg;base64,', '', $image);
            $image = str_replace('data:image/gif;base64,', '', $image);
            $image = str_replace(' ', '+', $image);

            $imagedata = base64_decode($image);
            //Set image whole path here 

            $location = public_path('images/uploads/' . $filename);
            Image::make($image)->save($location);
            $post->image = $filename;
        }

        $post->save();

        $post->tags()->sync($request->tags, false);


        Session::flash('success', 'The blog post was saved successfully!');

        return redirect()->route('posts.show', $post->id);

                    }

看法

<form class="form-horizontal" action="{{route('posts.store')}}" method="POST" enctype="multipart/form-data">{{ csrf_field() }}

<fieldset class="form-group">
<label for="featimage">Upload Image</label>
<input type="file" class="form-control slim" data-ratio="4:3" name="featimage">

<label class="col-md-2 col-form-label">Post Body</label>
<textarea type="textarea" class="form-control" rows="10" name="body"></textarea>
</fieldset>

<button class="btn btn-primary btn-block" name="submit">Save</button>
<button class="btn btn-danger btn-block" name="submit">Cancel</button>

</form>    

我读过其他类似的帖子,我显然遗漏了一些东西,但我只是不知道是什么。

标签: phplaravellaravel-blade

解决方案


你可以试试这个功能。

    function uploadImage($base_64_string,$image_name)
{

    preg_match("/^data:image\/(.*);base64/i", $base_64_string, $match);
    $extension_array = explode('/', (explode(';', $base_64_string))[0]);
    $extension = end($extension_array);

    $img = $base_64_string;
    if ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg'){
        $img = str_replace('data:image/' . $extension . ';base64,', '', $img);}
    elseif ($extension == 'mp4' || $extension == 'mov'){
        $img = str_replace('data:video/' . $extension . ';base64,', '', $img);
    }
    $img = str_replace(' ', '+', $img);

    $data = base64_decode($img);

    $fileNameI = $image_name.'_'.rand().time() . '.' . $extension;

//Uploading Image To S3 Bucket
    $s3 = Storage::disk('s3-image');
    $filePath = Config::get('common.Image_Folder') . $fileNameI;
    $s3->put($filePath, $data, 'public');

    return $fileNameI;
}

如果您没有 Bucket,您可以将图像存储在本地目录中。


推荐阅读