【问题标题】:Symfony output image """Cannot modify header information - headers already sent by...""" [duplicate]Symfony 输出图像“”“无法修改标头信息 - 标头已由...发送”“” [重复]
【发布时间】:2011-12-29 15:18:09
【问题描述】:

在/lib/Helper.class.php:

class Helper
{
  //... some functions
  static public function createImage( $src_image, $params)
  {
    $vars=array();
    foreach(array('angle'=>0,'font'=>'dejavu','font_size'=>20,'line_spacing'=>1.5,'preset'=>'BRCA','color'=>array(0,0,0),'text'=>'default text') as $key=>$value):

      $vars[$key]= array_key_exists($key, $params)?$params[$key]:sfConfig::get('app_default_poster_'.$key, $value);
    endforeach;

    extract($vars);
    $interval= $font_size*$line_spacing;
    $x=0;
    $y=0;
    $img_color = imagecolorallocate($src_image, $color[0], $color[1], $color[2]);
    $lines=explode("\n", $text);
    putenv('GDFONTPATH='.join(':',sfConfig::get('app_font_path')));
    $fontname=$font.'.ttf';
    foreach($lines as $i=>$line):

      imagettftext($src_image, $font_size, $angle, $x, $y+($i+1)*$interval, $img_color, $fontname, $line);

    endforeach;
    return $src_image;

  } 

  static public function createPoster( $params)
  {   
    $im= imagecreatefromjpeg(sfConfig::get('sf_web_dir').'/images/'.$params['preset'].'.jpg');
    return self::createImage($im, $params);
  }
  static public function createSidetape( $params)
  {

    $im= imagecreatetruecolor(sfConfig::get('app_sidetape_width'),sfConfig::get('app_sidetape_height'));
    $bg= imagecolorallocate($im, 255, 255, 255);
    imagefill($im, 0,0,$bg);
    return self::createImage($im , $params);
  } 


}

在我的actions.class.php(某些模块)中

  public function executePreview(sfWebRequest $request)
  {
    $this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
    $this->setLayout(false);
    $img2=Helper::createPoster($request->getGetParameters());
    imagejpeg($img2, NULL, 10); 
    return sfView::NONE;

  }

现在当我打开 /module/preview?text=Some+Text&preset=notice 时,我看不到正确的图像,但它的二进制数据以及警告:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/weblog/apps/backend/modules/poster/actions/actions.class.php:253) in /var/www/symfony/lib/response/sfWebResponse.class.php on line 336 Warning: Cannot modify header information - headers already sent by (output started at /var/www/weblog/apps/backend/modules/poster/actions/actions.class.php:253) in /var/www/symfony/lib/response/sfWebResponse.class.php on line 357 

另一方面,如果我在executePreview 中将$img2=Helper::createPoster($request->getGetParameters()); 更改为$img2=Helper::createSidetape($request->getGetParameters());,我会看到图像。为什么?

这有什么问题?

【问题讨论】:

    标签: php symfony1 http-headers


    【解决方案1】:

    我将executePreview修改为:

      public function executePreview(sfWebRequest $request)
      {
        $this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
        $this->setLayout(false);
        $img2=Helper::createPoster($request->getGetParameters());
        $this->getResponse()->sendHttpHeaders();
        imagejpeg($img2, NULL, 10); 
        return sfView::NONE;
    
      }
    

    出现问题是因为 Symfony 首先发送 Http headers,然后是 Content。这两个调用都是在MVC 的视图层中进行的。现在我的操作是调用imagejpeg,因此甚至在框架调用sendHttpHeaders 之前就将输出发送到浏览器。所以我强迫它在实际发送图像作为输出之前发送 Http 标头。

    【讨论】:

      【解决方案2】:

      这通常发生在 php 在类似代码之前输出错误/警告消息时

       $this->getResponse()->setHttpHeader('Content-type', 'image/jpeg'); 
      

      被执行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-09
        • 2012-06-19
        • 2011-11-14
        相关资源
        最近更新 更多