【问题标题】:How to retrieve images from a database using Laravel如何使用 Laravel 从数据库中检索图像
【发布时间】:2019-11-10 23:13:09
【问题描述】:

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

我已经在 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');
    }
}

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

【问题讨论】:

  • 您是否在图片文件夹中检查了该文件是否存在
  • 我在里面放了一些图片,但它们只是虚拟图片。但是我已经在我的数据库表中插入了一个我想要检索的图像。
  • 您将图像的文件名存储在数据库中还是图像二进制文件中?
  • 在我的数据库中。它有二进制类型
  • 您在视图的哪个位置导入图像块数据?顺便说一句,事情没那么简单。您必须指示浏览器正在处理什么样的数据(图像/jpeg,...)要考虑的另一件事是,从数据库中呈现图像比保存在驱动器上的图像花费的时间要长得多。

标签: php laravel phpmyadmin


【解决方案1】:

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

试试这个:

控制器

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

查看

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

【讨论】:

  • 您好,谢谢您的回答。 $g 是出乎意料的,它说它需要一个变量。 $image 是一个未定义的变量。我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 2016-05-27
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 2016-10-01
  • 2013-03-17
相关资源
最近更新 更多