如何使用PHP香草或Laravel保存和更新来自summernote的图像
如何使用PHP保存和更新summernote图像 Summernote图像位于base64中,因此需要解码、上载,然后在将Summernote内容保存到DB之前,使用图像上载目录修改Summernote内容的图像src数据。因为不建议保存图像信息数据库 当我在DB中保存上传图像的相同路径目录img/location时,当我尝试从DB编辑summernote内容时,summernote无法显示图像如何使用PHP香草或Laravel保存和更新来自summernote的图像,php,laravel,base64,summernote,Php,Laravel,Base64,Summernote,如何使用PHP保存和更新summernote图像 Summernote图像位于base64中,因此需要解码、上载,然后在将Summernote内容保存到DB之前,使用图像上载目录修改Summernote内容的图像src数据。因为不建议保存图像信息数据库 当我在DB中保存上传图像的相同路径目录img/location时,当我尝试从DB编辑summernote内容时,summernote无法显示图像{{$dbData->content} 此外,您需要在解码之前检查img src是否真的是base64
{{$dbData->content}
此外,您需要在解码之前检查img src是否真的是base64图像,因为在编辑已保存的内容时,现有的img src将不会是base64图像,除非新的图像尚未上载,因为现有的img src已经解码并上载
//checking if request is set
if (isset($request->long_description)) {
$detail=$request->long_description;
//Prepare HTML & ignore HTML errors
$dom = new \domdocument();
$dom->loadHtml($detail, LIBXML_NOWARNING | LIBXML_NOERROR);
//identify img element
$images = $dom->getelementsbytagname('img');
//loop over img elements, decode their base64 source data (src) and save them to folder,
//and then replace base64 src with stored image URL.
foreach($images as $k => $img){
//collect img source data
$data = $img->getattribute('src');
//checking if img source data is image by detecting 'data:image' in string
if (strpos($data, 'data:image')!==false){
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
//decode base64
$data = base64_decode($data);
//naming image file
$image_name= time().rand(000,999).$k.'.png';
// image path (path) to use upload file to
$path = 'img/location/'. $image_name;
//image path (path2) to save to DB so that summernote can display image in edit mode (When editing summernote content) NB: the difference btwn path and path2 is the forward slash "/" in path2
$path2 = '/img/location/'. $image_name;
file_put_contents($path, $data);
modify image source data in summernote content before upload to DB
$img->removeattribute('src');
$img->setattribute('src', $path2);
}
else {
// not base64 img
}
}
// final variable to store in DB
$detail = $dom->savehtml();
}