【问题标题】:Show images in view from http header从 http 标头显示图像
【发布时间】:2014-10-08 14:59:21
【问题描述】:

我在我的控制器中生成一个图像(我使用 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

我想展示图片,有什么想法吗?

谢谢!

【问题讨论】:

  • &lt;img src=... 需要一个 URL。如果$response 包含图像的原始二进制数据,那么您不能只将其填充到 src 属性中。除非你把它变成一个 data-uri。
  • 谢谢 Marc,但我怎样才能将这个射线二进制数据转换为 data-uri?

标签: php html image symfony http-headers


【解决方案1】:

您可以使用显示图像的路线

在您的控制器中:

/*
 * @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')}}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    相关资源
    最近更新 更多