-
-
Save ratulcse10/693dbf3770db7cc110b7f582f293bd84 to your computer and use it in GitHub Desktop.
Summernote post or update a content with image in Laravel controller . Supports any utf content. Note:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function domPost($content) | |
{ | |
libxml_use_internal_errors(true); | |
$dom = new \DomDocument(); | |
$dom->loadHtml('<?xml encoding="utf-8" ?>'.$content); // must include this to avoid font problem | |
$images = $dom->getElementsByTagName('img'); | |
if(count($images)>0) | |
{ | |
foreach($images as $k => $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 = "uploads/$filename.$mimetype"; | |
// if you use intervention image package | |
$image = Image::make($src) | |
->encode($mimetype, 100) # encode file to the specified mimetype | |
->save(public_path($filepath)); | |
// if you use intervention package end | |
// if you not use intervention package start | |
$destinationPath = public_path().'/uploads/'; | |
$filename = $file->getClientOriginalName(); | |
$src->move($destinationPath, $filename); | |
// if you not use intervention package end | |
$new_src = asset($filepath); | |
$img->removeAttribute('src'); | |
$img->setAttribute('src', $new_src); | |
} | |
} | |
} | |
# modified entity ready to store in database | |
$content = $dom->saveHTML($dom->documentElement); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment