问题
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);
}
}
in my actions.class.php
(of some module)
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;
}
now when I open /module/preview?text=Some+Text&preset=notice
, I don't see correct image but its binary data along with a warning:
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
On the other hand, if I change $img2=Helper::createPoster($request->getGetParameters());
to $img2=Helper::createSidetape($request->getGetParameters());
in executePreview
, I see an image. Why?
What's the problem in this?
回答1:
I modified executePreview
to the following:
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;
}
The problem was occuring because Symfony first sends Http headers
and then the Content
. both these calls are made in the view layer of the MVC
. Now my action was calling imagejpeg
hence sending output to the browser even before sendHttpHeaders
was called by the framework. So I forced it to send Http header before actually sending image as output.
回答2:
this usually happens when php outputs error / warning messages before code like
$this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
gets executed.
来源:https://stackoverflow.com/questions/8669589/symfony-output-image-cannot-modify-header-information-headers-already-sent