CakePHP如何返回JPG的Mimetype头?

时间:2014-05-07 02:54:44

标签: cakephp http-headers mime-types

发现这个:

http://stackoverflow.com/questions/7198124/setting-the-header-to-be-content-image-in-cakephp

但要么不理解,要么不适合我。

基本上想要记录'打开'用于电子邮件。目前它"工作"但在gmail中它显示图像没有显示 - 所以我想返回一个实际的图像标题。我试过了:

    $this->layout=false;
    $this->response->type('jpg');

在Controller for Opens中,但这不起作用。

Web Sniffer(http://web-sniffer.net/),正在显示jpeg响应,但仍然没有找到文件的空白图像。我该如何解决?

[编辑]

想一想:

http://stackoverflow.com/questions/900207/return-a-php-page-as-an-image

和此:

http://book.cakephp.org/2.0/en/controllers/request-response.html

可能是解决方案

3 个答案:

答案 0 :(得分:2)

提供真实的图像

如果您只发送图像的标题,但不发送图像内容 - 则会将其视为损坏的图像。为您正在使用的CakePHP版本发送文件refer to the documentation。例如在2.3 +:

public function opened() {
    ...

    $this->response->file('/path/to/1x1.gif');
    return $this->response;
}

答案 1 :(得分:0)

非常确定这有效:

    $name = './img/open.jpg';
    $fp = fopen($name, 'rb');


    $this->response->header("Content-Type: image/jpg");
    $this->response->header("Content-Length: " . filesize($name));
    fpassthru($fp);

其中open.jpg是cakephp' s / img目录中的1x1真实像素图像。

如果其他人可以确认会爱吗?

获得:

 ����JFIF``��C      $.' ",#(7),01444'9=82<.342��C     2!!22222222222222222222222222222222222222222222222222��"����������?����

手动(所以我猜这是一个&#34;真正的&#34;图像文件?)。不再获取gmails没有图像文件图标。

nvermind - websniff说这是一个HTML请求。我想出来的时候会更新。

[编辑]

认为这可能是正确的方法:

    $this->response->type('jpg');
    $this->response->file('./img/open.jpg');

刚刚测试过,肯定会获得1x1像素下载。没有上面的乱码。

答案 2 :(得分:0)

Gmails缓存代理

通常提供真实图像as suggest by AD7Six是可行的方法,但是当Gmails缓存代理时,您可能会在提供图像时遇到问题。

<强> http://www.emailmarketingtipps.de/2013/12/07/gmails-image-caching-affects-email-marketing-heal-opens-tracking/

问题在于图像是否已被缓存,代理不会再次请求它,因此对打开的跟踪不可靠。

救援的内容长度

直到最近,其解决方法是回复内容长度为0(以及private缓存控制,这似乎是其他一些网络邮件提供商所必需的):

// ...

$this->response->header(array
(
    'Cache-Control'  => 'private',
    'Content-Length' => 0
));
$this->response->type('gif');
return $this->response;

这将返回没有内容正文的响应,这被视为已损坏,但并非所有客户端确实显示了损坏的图像符号,仍然建议使用img标记上的样式隐藏图像。

返回缓存控制

但据报道,Gmail最近进行了一些更改,因此现在再次尊重发送no-cache标题。

<强> http://blog.movableink.com/real-time-content-and-re-open-tracking-return-to-gmail/

因此,除了AD7Six示例之外,适当的Cache-Control标头现在可以执行此操作

// ...

$this->response->header(array
(
    'Cache-Control' => 'no-cache, max-age=0'
));
$this->response->file('/path/to/1x1.gif');
return $this->response;