从控制器 symfony2 返回图像 [英] Return Image from Controller symfony2

查看:27
本文介绍了从控制器 symfony2 返回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在没有任何模板的情况下从控制器返回图像.我想在时事通讯中使用它进行像素跟踪.

I would like to know how can i return an image from the controller without any template. I would like to use it for pixel tracking in a newsletter.

我从这个代码开始

    $image = "1px.png";
    $file =    readfile("/path/to/my/image/1px.png");
    $headers = array(
        'Content-Type'     => 'image/png',
        'Content-Disposition' => 'inline; filename="'.$file.'"');
    return new Response($image, 200, $headers);

但是在导航器上我有一个断开的链接(找不到文件...)

But on the navigator i have a broken link (file not found...)

推荐答案

现在您将文件名作为响应体返回,并将文件内容写入 Content-Disposition 的文件名属性.

Right now you return the filename as response body and write the file-content to the filename property of Content-Disposition.

return new Response($image, 200, $headers);

应该是:

return new Response($file, 200, $headers);

...和...

'Content-Disposition' => 'inline; filename="'.$file.'"');

应该是……

'Content-Disposition' => 'inline; filename="'.$image.'"');

对吗?

进一步查看这个问题.

这篇关于从控制器 symfony2 返回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆