php - 如何使用 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');
}
}
希望我已经为您提供了足够的信息来帮助我找到解决方案。
解决方案
要显示二进制文件,您必须使用 base64_encode() 函数。
尝试这个 :
控制器
$g = Gallerij::find(1);
$image = chunk_split(base64_encode($g->afbeelding));
看法
<img src="data:image/png;base64,{{ $image }}">
推荐阅读
- android - 数学函数 Power from String ("2 power 3")
- python - while 循环中的语法无效
- gitlab - 使用appium为移动自动化设置gitlab cicd?
- android - 当用户退出应用程序时显示插页式 adMob 广告,但不是有意的
- kafka-consumer-api - 来自特定偏移量的 Kafka 消费者
- javascript - 使用 Vue 的动态 CSS 网格
- bash - sed /end-of-line$/ 模式匹配不适用于在变量值中包含 / 的 shell 变量
- javascript - 如何在测验中显示正确答案?
- angular - Ionic 3 trigger Popover from another button?
- node.js - 使用 winston 记录每个请求和错误消息