【问题标题】:How can CakePHP return a Mimetype header of JPG?CakePHP 如何返回 JPG 的 Mimetype 标头?
【发布时间】:2014-06-23 20:20:10
【问题描述】:

找到这个:

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

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

基本上想记录电子邮件的“打开”。目前它“有效”,但在 gmail 中它显示没有显示图像 - 所以我想返回一个实际的图像标题。我试过这样做:

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

在用于打开的控制器中,但这不起作用。

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

可能是解决办法

【问题讨论】:

    标签: cakephp http-headers mime-types


    【解决方案1】:

    提供真实图片

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

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

    【讨论】:

      【解决方案2】:

      很确定这有效:

          $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 的 /img 目录中的 1x1 真实像素图像。

      如果其他人可以确认会很高兴?

      获取:

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

      手动(所以我猜那是一个“真实”的图像文件?)。不再获取 gmail 无图像文件图标。

      nvermind--websniff 说这是一个 html 请求。当我弄清楚时会更新。

      [编辑]

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

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

      刚刚测试过,肯定可以下载 1x1 像素。没有像上面那样的胡言乱语。

      【讨论】:

        【解决方案3】:

        Gmails 缓存代理

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

        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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-13
          • 2018-09-21
          • 1970-01-01
          相关资源
          最近更新 更多