1

How to extract base64 image from text? I tried to use simple html dom library but couldn't extract it from text. I need to extract each base64 and convert it to an image then save it to public folder.

Code:

  $post = \App\Post::find(45);
    $dom = HtmlDomParser::str_get_html( $post->content );
    foreach ($dom->find('data:image') as $element) {
        dd($element);
    }
<p style="text-align:right"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to ma</p>

<p style="text-align:right">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB .... == </p>
Fateh Alrabeai
  • 660
  • 3
  • 17
  • 35
  • imo, no need for simple html dom, you can use `DOMDocument`, in PHP already built-in – Kevin Oct 19 '20 at 07:05
  • 1
    Does this answer your question? [Laravel return image preview from base64](https://stackoverflow.com/questions/48496474/laravel-return-image-preview-from-base64) – Ballard Oct 19 '20 at 07:07
  • @Ballard it needs to be a two part solution, the first is to extract the base64 image in the paragraph tag first, iterate all the results and use the conversion which your answer contains – Kevin Oct 19 '20 at 07:20

2 Answers2

1

I think this might be a duplicate, depending on if you want to save the image to your server first or not, please see answer by Kerwin or linktoahref

Laravel return image preview from base64

$image = imagecreatefromstring(base64_decode($results->getBase64Image()));
header('Content-type: image/png');
return imagejpeg($image);
Ballard
  • 869
  • 11
  • 25
  • I don't have problem with converting the image or saving it. All I want is to know how I can extract base64 images from text in post content which is between p tags . – Fateh Alrabeai Oct 19 '20 at 07:30
  • I tried this code but not working the way I want because i get this error : member function getBase64Image() on string – Fateh Alrabeai Oct 19 '20 at 07:32
0

The solution to my question is :

  1. Install paquettg/php-html-parser package.
  2. Install intervention/image.
  3. use the following code:
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use PHPHtmlParser\Dom;

$posts = Post::where('content_edited',0)->orderBy('id','asc')->take(100)->get();

foreach ($posts as $post)
{
    $dom = new Dom;
    $dom->loadStr($post->content);
    $imgs = $dom->find('img');

    if ($imgs->count() > 0 )
    {

        $i =0 ;
        foreach ($imgs as $img)
        {

            $image_src = $img->src;
            $result = Str::contains($image_src, 'data:image');
            if ($result)
            {
                preg_match("/data:image\/(.*?);/", $image_src, $image_extension);
                $image_src = preg_replace('/data:image\/(.*?);base64,/', '', $image_src); // remove the type part
                $image_src = str_replace(' ', '_', $image_src);
                $imageName = 'image_' .$i.Factory::create()->uuid . '.' . $image_extension[1];
                Image::make(base64_decode($image_src))->save(public_path('/photos/inline/') . $imageName);
                $tag = $img->getTag();
                $tag->setAttribute('src','/photos/inline/'.$imageName);
                $post->content_edited = 1;

            }
            $i++;
        }
        $post->content_edited = 1;
        $post->content = $dom;
        $post->save();
    }else{

        $post->content_edited = 1;
        $post->save();
    }

}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Fateh Alrabeai
  • 660
  • 3
  • 17
  • 35