【问题标题】:How do I store the images instead of base64 in Laravel如何在 Laravel 中存储图像而不是 base64
【发布时间】:2020-06-13 15:43:55
【问题描述】:

美好的一天。拜托,我需要你的帮助。建立一个 laravel 网站,其中 tinymce 在一些文本区域中实现。挑战在于,如果将图像上传到编辑器中,它们将存储为 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'));

 }

【问题讨论】:

    标签: javascript php laravel base64 tinymce


    【解决方案1】:

    您的标题/描述说明了一些内容,但您的代码说明了其他内容。

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

    • 要将文件名存储在数据库中并将文件存储在磁盘上,列类型应为相对于最大文件名长度的VARCHAR。

      李>

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2017-07-22
      相关资源
      最近更新 更多