簡體   English   中英

如何使用Laravel從數據庫檢索圖像

[英]How to retrieve images from a database using Laravel

當前,我正在嘗試從數據庫表中檢索圖像,但是沒有成功。 我已經制作了一個表格,在其中插入了圖像,以便可以檢索它。

我在堆棧溢出中尋找問題的答案。 有相似的問題,但答案不是我要找的答案。 我也在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');
    }
}

希望我已經給您足夠的信息來幫助我找到解決方案。

要顯示二進制文件,您必須使用base64_encode()函數。

嘗試這個 :

控制者

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

視圖

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM