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

In this post we will give you information about Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64. Hear we will give you detail about Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64And how to use it also give you demo for it if it is necessary.

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.

InterventionImageImageServiceProvider::class

Now add facade for this package to $aliases.

'Image' => InterventionImageFacadesImage::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. <divclass="col-xs-12 col-sm-12 col-md-12">
  3. <divclass="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.     <divclass="col-xs-12 col-sm-12 col-md-12 text-center">
  9. <buttontype="submit"class="btn btn-primary">Submit</button>
  10. </div>
  11. {!! Form::close() !!}
{!! Form::open(array('route' => 'summernote.postForm','method'=>'post','class'=>'post-reply','files'=>true)) !!}    <div >        <div >            <strong>Message:</strong>            {!! Form::textarea('message', NULL, array('placeholder'=>'Write your comment','rows'=>3,'class'=>'form-control summernote')) !!}        </div> 	</div> 	<div >        <button type="submit" >Submit</button>    </div>{!! 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'));
Route::get('summernote-image-upload',array('as'=>'summernote.getForm','uses'=>'FileController@getSummernote')) ;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 functiongetSummernote(){
  2. returnview('files.summernoteform');
  3. }
  4. public functionpostSummernote(Request $request){
  5. $this->validate($request,[
  6. 'message'=>'required'
  7. ]);
  8. $message=$request->input('message');
  9. $product=new Product;
  10. $dom=newDomDocument();
  11. $dom->loadHtml($message, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  12. $images=$dom->getElementsByTagName('img');
  13. // foreach <img> in the submited message
  14. foreach($imagesas$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
  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. }
    public function getSummernote(){        return view('files.summernoteform');    }    public function postSummernote(Request $request){        $this->validate($request, [            'message' => 'required'        ]);        $message=$request->input('message');        $product=new Product;        $dom = new DomDocument();        $dom->loadHtml($message, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);            $images = $dom->getElementsByTagName('img');                                // foreach <img> in the submited message        foreach($images as $img){            $src = $img->getAttribute('src');                        // if the img source is 'data-url'            if(preg_match('/data:image/', $src)){                                // get the mimetype                preg_match('/data:image/(?<mime>.*?);/', $src, $groups);                $mimetype = $groups['mime'];                                // Generating a random filename                $filename = uniqid();                $filepath = "/summernoteimages/$filename.$mimetype";                    // @see                 $image = Image::make($src)                  // resize if required                  /* ->resize(300, 200) */                  ->encode($mimetype, 100)  // encode file to the specified mimetype                  ->save(public_path($filepath));                                $new_src = asset($filepath);                $img->removeAttribute('src');                $img->setAttribute('src', $new_src);            } // <!--endif        } // <!-        $product->message = $dom->saveHTML();        $product->save();    }

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. });
$('.summernote').summernote({      height: 200 });

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');
$image = Image::make('myimage.jpg')->resize(200, 100);return $image->response('jpg');
Don’t forget to add traits in your controller

use InterventionImageImageManagerStatic as Image;

Include Summernote library in your master template.

  1. {{ HTML::style('assets/css/libs/summernote/summernote.css')}}
{{ HTML::style('assets/css/libs/summernote/summernote.css') }}
  1. {{ HTML::script('assets/js/libs/summernote/summernote.min.js')}}
{{ HTML::script('assets/js/libs/summernote/summernote.min.js') }}

Hope this code and post will helped you for implement Laravel Intervention Summernote Editor to get upload File(image) URL instead of Base64. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  71  =  79

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US