Display image stored in BLOB database in symfony

前端 未结 4 1005
温柔的废话
温柔的废话 2021-01-21 10:19

I load my image (blob data ) in my GETer Entity When I just return ($this->foto) in my GETer I see :Resource id #284 on the screen When I change my GETer like this : return stre

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 10:43

    A more direct way, without extra work in the controller:

    In the Entity Class

    /**
     * @ORM\Column(name="photo", type="blob", nullable=true)
     */
    private $photo;
    
    private $rawPhoto;
    
    public function displayPhoto()
    {
        if(null === $this->rawPhoto) {
            $this->rawPhoto = "data:image/png;base64," . base64_encode(stream_get_contents($this->getPhoto()));
        }
    
        return $this->rawPhoto;
    }
    

    In the view

    
    

    EDIT

    Thanks to @b.enoit.be answer to my question here, I could improve this code so the image can be displayed more than once.

提交回复
热议问题