我如何从数据库中获取图像以查看 laravel 中的文件

How i get image from database to view file in laravel

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

这是我用来从数据库获取图像以查看文件的行

我使用波纹管控制器来获取此图像

public function show($id)
{

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

迁移文件如下所示

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

这种方法行不通view shows like this 我怎样才能解决这个问题,建议比这更好的新方法,我不能使用 public/storage 文件夹,因为所有图像都应该保存在数据库中

试试这个

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

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

或者您可以将图像行替换为以下内容

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

要动态设置 mime 类型,请创建辅助函数并使用它

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

并使用blade模板中的帮助功能,如下所示

<img src="@php echo constructImageFromBinary($post->image,'image/png'); @endphp"/>