如何在 Laravel 中存储图像而不是 base64

How do I store the images instead of base64 in Laravel

美好的一天。拜托,我需要你的帮助。构建一个 laravel 网站,其中 tinymce is/was 在一些文本区域中实现。挑战在于,如果图像在编辑器中上传,它们将存储为 base64 编码。这会减慢服务器速度。我不得不在我的数据库中将我的数据类型更改为 longtext。我如何存储图像而不是 base64?以及如何读取存储的图像。

我的代码如下所示 我的控制器

public function create(Request $request){


        $categories = BlogCategory::all();
        $tags = Tag::all();

        if($request->isMethod('post')){

            //dd($request);
            $data = $request->except('name');
            $post = new Post;
            //Title
            $post->title = $request->title;
            //Slug
            $post->publish_date = new Carbon;
            $slug = $this->createSlug($request->title);
            $post->slug = $slug;

            //Category
            if($request->category_id == "Choose Category")
            {
                Session::flash('failure','Please Select A Category To Proceed!');
                return redirect()->back();
            }else{
                $post->category_id = $request->category_id;
            }


            //Body
            $post->body = $request->body;

            //Author
            if(isset($request->author)){
                $post->author = $request->author;
                $post->author_slug = Str::slug($post->author,'-');
            }else{
                $post->author = "";
                $post->author_slug = "";
            }

            //User ID
            $post->user_id = Auth::user()->id;

            //Keywords
            if(isset($request->keywords)){
                $post->keywords = $request->keywords;
            }else{
                $post->keywords = "";
            }

            //Description
            if(isset($request->description)){
                $post->description = $request->description;
            }else{
                $post->description = "";
            }

            //Publish
            if(isset($request->publish)){
                if($request->publish == 'draft'){
                    $post->publish = 0;
                }elseif($request->publish == 'publish'){
                    $post->publish = 1;
                    $post->publish_date = new Carbon;
                }
            }

            //Comment
            if(isset($request->comments)){
                if($request->comments = "on"){
                    $post->comment = 1;
                }
            }

            //Image
            if($request->hasFile('image')){
                $img_temp = $request->file('image');
                if($img_temp->isValid()){

                    $extension = $img_temp->getClientOriginalExtension();
                    $filename = 'mohvisuals'.rand(111,9999).'.'.$extension;
                    $large_image_path = 'images/backend_images/posts/large/'.$filename;
                    $medium_image_path = 'images/backend_images/posts/medium/'.$filename;


                    //Resize Images
                    Image::make($img_temp)->save($large_image_path);
                    Image::make($img_temp)->fit(500,400)->save($medium_image_path);

                    //Store Images
                    $post->image =$filename;
                }
            }

            $post->save();
            $post->tags()->sync($request->tags,false);
            Session::flash('success',' Post Created Successfully!');
            return redirect()->back();

        }

        return view('back_end.blog.posts.create')->with(compact('categories','tags'));

 }

你的 title/description 说了一些东西,但你的代码说了另外一些东西。

  • 要在数据库中存储文件,列类型必须是BINARY/BLOB。

  • 为了在数据库中存储文件名,在磁盘上存储文件,列类型应该是相对于最大文件名长度。

不要将文件转换为 base64,除非它们很小,因为它们的大小会增加大约 x3 倍。

要将文件 存储在 数据库中,您可以使用此代码。在你的控制器中:

if ($request->hasFile('file'))
{
    // If you want to resize the image, use the following method to get temporary file's path.
    $request->file('file')->getPathName(); 

    // `get()` retrieves file's content in binary mode
    $post->image = request->file('file')->get(); 
}