title: how to inserting and update image Summernote in laravel 5
body: <p>I use summernote as a WYSIWYG on my web, but I get a problem that I can't find in google(not solve my problem), which is how to input images in my text-area using summernote and display them in view I have used various methods as below but still produce errors, I hope I can get the answer in this amazing StackOverflow.</p>
  
  <p>I tried this code first :</p>
  
  <pre><code>$detail=$request-&gt;input('konten');
        $dom = new \DomDocument();
        libxml_use_internal_errors(true);
        $dom-&gt;loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $images = $dom-&gt;getElementsByTagName('img');
        foreach($images as $k =&gt; $img){
  
            $data = $img-&gt;getAttribute('src');
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $data = base64_decode($data);
            $image_name= "/img/blog" . time().$k. '.png';
            $path = public_path() . $image_name;
            file_put_contents($path, $data);
            $img-&gt;removeAttribute('src');
            $img-&gt;setAttribute('src', $image_name);
        }
        $detail = $dom-&gt;saveHTML();
  </code></pre>
  
  <p>and I save it to database, this work no error overall but the problem is, in my laravel directory the "laravel-5" environment not a default, I make folder name "main" for the "laravel" and that folder "main" become one directory in public, the problem is, $path = "public_path(). $image_name" not upload to the right directory on the public name "img/blog" but make a new directory on the MAIN folder, so when I show on the view that image did not show up because wrong directory the result on img src is img/blog/namefile.png, that must be <a href="https://somedomain.com/img/blog/namefile.png" rel="nofollow noreferrer">https://somedomain.com/img/blog/namefile.png</a> to show the image.</p>
  
  <p>and I tried some library name <code>Intervention\Image\ImageManagerStatic</code>
  and this the code :</p>
  
  <pre><code>$detail=$request-&gt;input('konten');
        $dom = new \DomDocument();
        $dom-&gt;loadHtml( mb_convert_encoding($detail, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $images = $dom-&gt;getElementsByTagName('img');
          foreach($images as $img){
              $src = $img-&gt;getAttribute('src');
              if(preg_match('/data:image/', $src)){
                  // get the mimetype
                  preg_match('/data:image\/(?&lt;mime&gt;.*?)\;/', $src, $groups);
                  $mimetype = $groups['mime'];
  
                  $filename = uniqid();
                  $filepath = "/img/blog/$filename.$mimetype"; 
  
                  $image = Image::make($src)
                    -&gt;encode($mimetype, 50)  
                    -&gt;save($filepath);
  
                  $new_src = asset($filepath);
                  $img-&gt;removeAttribute('src');
                  $img-&gt;setAttribute('src', $new_src);
              }
          }
        $detail = $dom-&gt;saveHTML();
  </code></pre>
  
  <p>but the result is the image can't write to the directory, I don't know why, I tried the first one delete the public_path() and change to the URL, and have the same error.</p>
  
  <pre><code>//first trial
  
  $detail=$request-&gt;input('konten');
        $dom = new \DomDocument();
        libxml_use_internal_errors(true);
        $dom-&gt;loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $images = $dom-&gt;getElementsByTagName('img');
        foreach($images as $k =&gt; $img){
  
            $data = $img-&gt;getAttribute('src');
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $data = base64_decode($data);
            $image_name= "/img/blog" . time().$k. '.png';
            $path = public_path() . $image_name;
            file_put_contents($path, $data);
            $img-&gt;removeAttribute('src');
            $img-&gt;setAttribute('src', $image_name);
        }
        $detail = $dom-&gt;saveHTML();
  
  //second trial
  
  $detail=$request-&gt;input('konten');
        $dom = new \DomDocument();
        $dom-&gt;loadHtml( mb_convert_encoding($detail, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $images = $dom-&gt;getElementsByTagName('img');
          foreach($images as $img){
              $src = $img-&gt;getAttribute('src');
              if(preg_match('/data:image/', $src)){
                  // get the mimetype
                  preg_match('/data:image\/(?&lt;mime&gt;.*?)\;/', $src, $groups);
                  $mimetype = $groups['mime'];
  
                  $filename = uniqid();
                  $filepath = "/img/blog/$filename.$mimetype"; 
  
                  $image = Image::make($src)
                    -&gt;encode($mimetype, 50)  
                    -&gt;save($filepath);
  
                  $new_src = asset($filepath);
                  $img-&gt;removeAttribute('src');
                  $img-&gt;setAttribute('src', $new_src);
              }
          }
        $detail = $dom-&gt;saveHTML();
  </code></pre>
  
  <p>I expect the image uploaded to the right directory and can be shown on the view</p>