Symfony输出图像“”“无法修改标题信息 - 已经由...发送的标题”“”

时间:2011-12-29 15:18:09

标签: php symfony1 http-headers

在/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 

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

这有什么问题?

2 个答案:

答案 0 :(得分: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头。

答案 1 :(得分:0)

当php在

之类的代码之前输出错误/警告消息时,通常会发生这种情况
 $this->getResponse()->setHttpHeader('Content-type', 'image/jpeg'); 

被执行。