Home > Blockchain >  How to update an article with images using summernote in Laravel
How to update an article with images using summernote in Laravel

Time:06-23

I've implemented summernote in Laravel and uploading an article with images works as expected. 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.

This is the highlighted line with the error on the browser, list($type, $data) = explode(';', $data);. The line is the second line inside the loop in the update() method.

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();
    }

CodePudding user response:

When updating your article, you dont have to convert the images that were already converted (from blob to link-to-file)

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();
}
  • Related