Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64

Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64

Sometime you upload image in database from summernote input text editor directly then you will ever notice then image upload into database in format of Base62.

Upload in Base62 format is not the right way because there are number of characters that represent binary data in an ASCII string format.

I will tell you here how you can get image url and how can you store image path in database instead of Base64 string.

Use "intervention/image": "dev-master" package to work with image handling.

So first update composer to include library of intervention image.

After update composer, add service provider in the $providers for this package.

Intervention\Image\ImageServiceProvider::class

Now add facade for this package to $aliases.

'Image' => Intervention\Image\Facades\Image::class
Step1: Form

Create a form with textarea input field and add a class summernote to make summernote editor.

  1. {!! Form::open(array('route' => 'summernote.postForm','method'=>'post','class'=>'post-reply','files'=>true)) !!}
  2. <div class="col-xs-12 col-sm-12 col-md-12">
  3. <div class="form-group">
  4. <strong>Message:</strong>
  5. {!! Form::textarea('message', NULL, array('placeholder'=>'Write your comment','rows'=>3,'class'=>'form-control summernote')) !!}
  6. </div>
  7.     </div>
  8.     <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  9. <button type="submit" class="btn btn-primary">Submit</button>
  10. </div>
  11. {!! Form::close() !!}
Step2: Add route

Now add these two route in your routes.php to call controller method for getting summernote form and post summernote data image upload into database.

  1. Route::get('summernote-image-upload',array('as'=>'summernote.getForm','uses'=>'FileController@getSummernote')) ;
  2. Route::post('summernote-image-upload',array('as'=>'summernote.postForm','uses'=>'FileController@postSummernote')) ;
Step3: Create Controller function

Now create a method where i use intervention image facade to work with image.

  1. public function getSummernote(){
  2. return view('files.summernoteform');
  3. }
  4. public function postSummernote(Request $request){
  5. $this->validate($request, [
  6. 'message' => 'required'
  7. ]);
  8. $message=$request->input('message');
  9. $product=new Product;
  10. $dom = new DomDocument();
  11. $dom->loadHtml($message, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  12. $images = $dom->getElementsByTagName('img');
  13. // foreach <img> in the submited message
  14. foreach($images as $img){
  15. $src = $img->getAttribute('src');
  16. // if the img source is 'data-url'
  17. if(preg_match('/data:image/', $src)){
  18. // get the mimetype
  19. preg_match('/data:image\/(?<mime>.*?)\;/', $src, $groups);
  20. $mimetype = $groups['mime'];
  21. // Generating a random filename
  22. $filename = uniqid();
  23. $filepath = "/summernoteimages/$filename.$mimetype";
  24. // @see http://image.intervention.io/api/
  25. $image = Image::make($src)
  26. // resize if required
  27. /* ->resize(300, 200) */
  28. ->encode($mimetype, 100) // encode file to the specified mimetype
  29. ->save(public_path($filepath));
  30. $new_src = asset($filepath);
  31. $img->removeAttribute('src');
  32. $img->setAttribute('src', $new_src);
  33. } // <!--endif
  34. } // <!-
  35. $product->message = $dom->saveHTML();
  36. $product->save();
  37. }

Now add script to change setting of summernote editor, there are so many settings that you can use.

  1. $('.summernote').summernote({
  2. height: 200
  3. });

By using Intervention image library, you can resize your image in any size. See the code below to resize image.

  1. $image = Image::make('myimage.jpg')->resize(200, 100);
  2. return $image->response('jpg');

Phone: (+91) 8800417876
Noida, 201301