首页 > 解决方案 > 如何使用 Laravel 从数据库中检索图像

问题描述

目前,我正在尝试从数据库表中检索图像,但没有成功。我已经制作了一个表格,其中插入了一个图像,以便我可以检索它。

我在 Stack Overflow 中寻找我的问题的答案。有类似的问题,但答案不是我要找的。我也在 Laracast 网站上寻找过答案,但这些答案并没有提供解决方案。

我的观点:

@extends ('Layout')
@section('title')
    Galerij
@endsection

@section('content')


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">``</script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">    </script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"> 
   </script>
    </head>
    <body>

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="data:image/png;base64,{{ $image }}" alt="Los Angeles" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum2.jpg')}}" alt="Chicago" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum3.jpg')}}" alt="New York" width="1100" height="500">
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

</body>
</html>

@endsection

我的模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class gallerij extends Model
{
    protected $fillable=[
    'afbeelding', 'id'
    ];
}

我的 create_gallerij__table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGallerijTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('gallerij', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->binary('afbeelding');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('gallerij');
    }
}

希望我已经为您提供了足够的信息来帮助我找到解决方案。

标签: phplaravelphpmyadmin

解决方案


要显示二进制文件,您必须使用 base64_encode() 函数。

尝试这个 :

控制器

$g = Gallerij::find(1);
$image = chunk_split(base64_encode($g->afbeelding));

看法

<img src="data:image/png;base64,{{ $image }}">

推荐阅读