Symfony输出图像“”“无法修改标头信息 - 标头已发送..""".


Symfony output image """Cannot modify header information - headers already sent by..."""

in/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 在代码之前输出错误/警告消息时

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

被执行。

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 标头。