lundi 29 juillet 2019

how to inserting and update image Summernote in laravel 5

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.

i tried this code first :

$detail=$request->input('konten');
      $dom = new \DomDocument();
      libxml_use_internal_errors(true);
      $dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
      $images = $dom->getElementsByTagName('img');
      foreach($images as $k => $img){

          $data = $img->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->removeAttribute('src');
          $img->setAttribute('src', $image_name);
      }
      $detail = $dom->saveHTML();

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 public name "img/blog" but make new directory on MAIN folder, so when i show on view that image not show up because wrong directory the result on img src is img/blog/namefile.png, that must be https://somedomain.com/img/blog/namefile.png to show the image.

and i tried some library name "Intervention\Image\ImageManagerStatic" and this the code :

$detail=$request->input('konten');
      $dom = new \DomDocument();
      $dom->loadHtml( mb_convert_encoding($detail, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
      $images = $dom->getElementsByTagName('img');
        foreach($images as $img){
            $src = $img->getAttribute('src');
            if(preg_match('/data:image/', $src)){
                // get the mimetype
                preg_match('/data:image\/(?<mime>.*?)\;/', $src, $groups);
                $mimetype = $groups['mime'];

                $filename = uniqid();
                $filepath = "/img/blog/$filename.$mimetype"; 

                $image = Image::make($src)
                  ->encode($mimetype, 50)  
                  ->save($filepath);

                $new_src = asset($filepath);
                $img->removeAttribute('src');
                $img->setAttribute('src', $new_src);
            }
        }
      $detail = $dom->saveHTML();

but the result is the image can't be 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 same error.

//first trial

$detail=$request->input('konten');
      $dom = new \DomDocument();
      libxml_use_internal_errors(true);
      $dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
      $images = $dom->getElementsByTagName('img');
      foreach($images as $k => $img){

          $data = $img->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->removeAttribute('src');
          $img->setAttribute('src', $image_name);
      }
      $detail = $dom->saveHTML();

//second trial

$detail=$request->input('konten');
      $dom = new \DomDocument();
      $dom->loadHtml( mb_convert_encoding($detail, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
      $images = $dom->getElementsByTagName('img');
        foreach($images as $img){
            $src = $img->getAttribute('src');
            if(preg_match('/data:image/', $src)){
                // get the mimetype
                preg_match('/data:image\/(?<mime>.*?)\;/', $src, $groups);
                $mimetype = $groups['mime'];

                $filename = uniqid();
                $filepath = "/img/blog/$filename.$mimetype"; 

                $image = Image::make($src)
                  ->encode($mimetype, 50)  
                  ->save($filepath);

                $new_src = asset($filepath);
                $img->removeAttribute('src');
                $img->setAttribute('src', $new_src);
            }
        }
      $detail = $dom->saveHTML();

i expect the image uploaded to the right directory and can be show on the view



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire