使用 Symfony Twig 的数据库中的 Dispaly Blob 图像



我正在尝试显示每篇博客文章的图像。我设置了它们,以便它显示博客名称,摘录和发布日期,但我正在努力获取我存储为Blob的图像。我附加了我的代码,它分为 3 部分:实体,它设置和获取变量;index.html.twig文件,即前端(我如何显示图像(;和post.orm.yml文件,用于设置图像的项目类型,即 BLOB。

发布实体

/**
* Set image
*
* @param /post/blob $image
*
* @return Post
*/
public function setimage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return post/blob
*/
public function getimage()
{
return $this->image;
}

索引.html.twig

{{post.image}}

post.orm.yml

ShannonBlogBundleEntityPost:
type: entity
table: null
repositoryClass: ShannonBlogBundleRepositoryPostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: '255'
body:
type: text
publishedAt:
type: datetime
column: published_at
image:
type:image
lifecycleCallbacks: {  }

后置控制器.php

公共函数图像操作($id( { $image = $this->getDoctrine((->getRepository('ShannonBlogBundle:Image'(->findOneBy(array('id'=> $id((;
返回 $this->render('index.html.twig', array('image' => $image((; } } 公共函数图像操作($id( { $image = $this->getDoctrine((->getRepository('ShannonBlogBundle:Image'(->findOneBy(array('id'=> $id((;
返回 $this->render('index.html.twig', array('image' => $image(''(; }

您需要在base64中对图像进行编码,并将其嵌入HTML中:

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

最新更新