Summernote image upload

后端 未结 5 1505
花落未央
花落未央 2020-11-27 09:51

I have a problem with editor Summernote. I want to upload images into a catalog on the server. I have some script:



        
5条回答
  •  悲&欢浪女
    2020-11-27 10:31

    Summernote converts your uploaded images to a base64 encoded string by default, you can process this string or as other fellows mentioned you can upload images using onImageUpload callback. You can take a look at this gist which I modified a bit to adapt laravel csrf token here. But that did not work for me and I had no time to find out why! Instead, I solved it via a server-side solution based on this blog post. It gets the output of the summernote and then it will upload the images and updates the final markdown HTML.

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Storage;
    
    Route::get('/your-route-to-editor', function () {
        return view('your-view');
    });
    
    Route::post('/your-route-to-processor', function (Request $request) {
    
           $this->validate($request, [
               'editordata' => 'required',
           ]);
    
           $data = $request->input('editordata');
    
           //loading the html data from the summernote editor and select the img tags from it
           $dom = new \DomDocument();
           $dom->loadHtml($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);    
           $images = $dom->getElementsByTagName('img');
           
           foreach($images as $k => $img){
               //for now src attribute contains image encrypted data in a nonsence string
               $data = $img->getAttribute('src');
               //getting the original file name that is in data-filename attribute of img
               $file_name = $img->getAttribute('data-filename');
               //extracting the original file name and extension
               $arr = explode('.', $file_name);
               $upload_base_directory = 'public/';
    
               $original_file_name='time()'.$k;
               $original_file_extension='png';
    
               if (sizeof($arr) ==  2) {
                    $original_file_name = $arr[0];
                    $original_file_extension = $arr[1];
               }
               else
               {
                    //the file name contains extra . in itself
                    $original_file_name = implode("_",array_slice($arr,0,sizeof($arr)-1));
                    $original_file_extension = $arr[sizeof($arr)-1];
               }
    
               list($type, $data) = explode(';', $data);
               list(, $data)      = explode(',', $data);
    
               $data = base64_decode($data);
    
               $path = $upload_base_directory.$original_file_name.'.'.$original_file_extension;
    
               //uploading the image to an actual file on the server and get the url to it to update the src attribute of images
               Storage::put($path, $data);
    
               $img->removeAttribute('src');       
               //you can remove the data-filename attribute here too if you want.
               $img->setAttribute('src', Storage::url($path));
               // data base stuff here :
               //saving the attachments path in an array
           }
    
           //updating the summernote WYSIWYG markdown output.
           $data = $dom->saveHTML();
    
           // data base stuff here :
           // save the post along with it attachments array
           return view('your-preview-page')->with(['data'=>$data]);
    
    });
    

提交回复
热议问题