[英]How to update an article with images using summernote in Laravel
I've implemented summernote in Laravel and uploading an article with images works as expected.我已经在 Laravel 中实现了 Summernote,并按预期上传了一篇带有图片的文章。 On updating an article it works when an article doesn't have an image.在更新文章时,它可以在文章没有图像时起作用。 But for an article with an image(s), it throws and error, Undefined array key
.但是对于带有图像的文章,它会抛出错误Undefined array key
。
This is the highlighted line with the error on the browser, list($type, $data) = explode(';', $data);
这是浏览器上突出显示的错误行, list($type, $data) = explode(';', $data);
. . The line is the second line inside the loop
in the update()
method.该行是update()
方法中loop
内的第二行。
My store method我的店铺方法
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$content = $request->body;
$dom = new \DomDocument();
$dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imageFile = $dom->getElementsByTagName('img');
foreach ($imageFile as $item => $image) {
$data = $image->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$imgeData = base64_decode($data);
$image_name = "/upload/" . time() . $item . '.png';
$path = public_path() . $image_name;
file_put_contents($path, $imgeData);
$image->removeAttribute('src');
$image->setAttribute('src', $image_name);
}
$content = $dom->saveHTML();
$post = Post::create([
'title' => $request->title,
'body' => $content
]);
// dd($post->toArray());
return redirect(route('posts.index'));
}
My update method我的更新方法
public function update(Request $request, Post $post)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$content = $request->body;
libxml_use_internal_errors(true);
$dom = new \DomDocument();
$dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | libxml_use_internal_errors(true));
$imageFile = $dom->getElementsByTagName('img');
foreach ($imageFile as $item => $image) {
$data = $image->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$imgeData = base64_decode($data);
$image_name = "/upload/" . time() . $item . '.png';
$path = public_path() . $image_name;
file_put_contents($path, $imgeData);
$image->removeAttribute('src');
$image->setAttribute('src', $image_name);
}
$content = $dom->saveHTML();
$post->title = $request->title;
$post->body = $content;
$post->save();
return redirect()->back();
}
When updating your article, you dont have to convert the images that were already converted (from blob to link-to-file)更新文章时,您不必转换已转换的图像(从 blob 到链接到文件)
public function update(Request $request, Post $post)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$content = $request->body;
libxml_use_internal_errors(true);
$dom = new \DomDocument();
$dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | libxml_use_internal_errors(true));
$imageFile = $dom->getElementsByTagName('img');
foreach ($imageFile as $item => $image) {
$data = $image->getAttribute('src');
if (strpos($data, ';') === false) {
continue;
}
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$imgeData = base64_decode($data);
$image_name = "/upload/" . time() . $item . '.png';
$path = public_path() . $image_name;
file_put_contents($path, $imgeData);
$image->removeAttribute('src');
$image->setAttribute('src', $image_name);
}
$content = $dom->saveHTML();
$post->title = $request->title;
$post->body = $content;
$post->save();
return redirect()->back();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.