1
 <img  src="{{$post->image}}"  width="140" height="140">

this is the line which i use to get image from database to view file

i use bellow controller to get this image

public function show($id)
{

    $post=Clients::find($id);
    return view('pet.shw',['post'=>$post,'pets'=>$post->pets]); }

and also migration file is shown bellow

public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->string('ename');
        $table->string('mobile');
        $table->string('mail');
        $table->binary('image') ; //image that i wanna use 
        $table->mediumText('Discription');
        $table->timestamps();
    });
}

this approach was not worked view shows like this how can i solve this problem,suggest the new approaches better than that, i can't use public/storage folder because of all the images should be saved in database

1
  • can you post $post->image example value ? Commented Dec 26, 2018 at 15:16

1 Answer 1

1

Try this

$img = base64_encode($post->image);

<img src="data:image/jpg;charset=utf8;base64,<?php echo $img ?>"/>

Or you can just replace the image line with the following

<img src="data:image/jpg;charset=utf8;base64,{{base64_encode($post->image)}}"/>

To dynamically set the mime type make a helper function and use it

function constructImageFromBinary($file, $mime) 
{  
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';charset=utf8;base64,' . $base64);
}

and use the help function in blade template as shown below

<img src="@php echo constructImageFromBinary($post->image,'image/png'); @endphp"/>
Sign up to request clarification or add additional context in comments.

1 Comment

be careful using data:image/jpg this is assuming all uploaded image have mime type of image/jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.