Dispaly Blob image from database using Symfony Twig

I am trying to display images for each blog post. I have them set up so that it is displaying the blog name, excerpt and published date, but I am struggling to get the images which I have stored as Blobs. I have attached my code, which is in 3 parts: the entity, which has set and get variables; the index.html.twig file, which is the front end (how I am displaying the image); and the post.orm.yml file, which is to set the type of item the image is, ie, BLOB.

Post entity

/**
 * 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;
}

index.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: {  }

postcontroller.php

public function imageAction($id) { $image = $this->getDoctrine()->getRepository('ShannonBlogBundle:Image')->findOneBy(array('id'=> $id));
return $this->render('index.html.twig', array('image' => $image)); } } public function imageAction($id) { $image = $this->getDoctrine()->getRepository('ShannonBlogBundle:Image')->findOneBy(array('id'=> $id));
return $this->render('index.html.twig', array('image' => $image)``); }


您需要使用base64编码您的图像,并将其嵌入到HTML中:

<img  src="data:image/png;base64,{{ imageBase64 }}" />
链接地址: http://www.djcxy.com/p/47814.html

上一篇: 客户端有什么区别

下一篇: 使用Symfony Twig从数据库中分辨Blob图像