2014-10-08 89 views
-1

我在我的控制器(我用的Symfony2)生成图像,当我跟我的形象回到我的回应,我得到这个:显示图像

HTTP/1.0 200 OK 
Cache-Control: no-cache 
Content-Disposition: inline; filename="" 
Content-Type: image/jpeg 

我需要显示在图像我的观点,但如果我把:

<img src="<?= $response ?>" /> 

浏览器显示404错误,如果我用

<?php echo $response; ?> 

我只得到

HTTP/1.0 200 OK 
Cache-Control: no-cache 
Content-Disposition: inline; filename="example.jpg" 
Content-Type: image/jpeg 
Date: Wed, 08 Oct 2014 14:55:24 GMT 
X-Sendfile: 67902 

我想显示图像,任何想法?

谢谢!

+0

' 2014-10-08 15:17:27

+0

谢谢Marc,但是我怎样才能将这个射线二进制数据转换为数据 - uri? – user3396420 2014-10-08 15:21:32

+0

http://en.wikipedia.org/wiki/Data_URI_scheme#Format – 2014-10-08 16:25:42

回答

0

可以使用该显示图像

在控制器的路由:

/* 
* @Route("/getImage", name="getImage") 
* @Method("GET") 
*/ 
public function testAction(Request $request, $id) 
{ 
    $response = new \Symfony\Component\HttpFoundation\Response(); 
    $response->headers->set('Cache-Control', 'private'); 
    $response->headers->set('Content-type', 'image/jpg'); 
    $response->headers->set('Content-Disposition', 'attachment; filename="example.jpg"');   
    $response->setContent(file_get_contents('example.jpg')); 
    return $response;   

}

在您的树枝的模板:

<img src="{{path('getImage')}}">