将Base64编码转换为图像并存储在服务器上(laravel)

问题描述 投票:-1回答:1

祝你好。拜托,我需要你的帮助。建立一个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个回答
0
投票

您的标题/说明中有一些内容,但您的代码中有其他内容。

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

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

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

要存储文件

in

数据库,可以使用此代码。在您的控制器内部: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(); }
© www.soinside.com 2019 - 2024. All rights reserved.