Php symfony将资源标识符从操作传递到模板
我的行动:Php symfony将资源标识符从操作传递到模板,php,symfony1,symfony-1.4,resource-id,Php,Symfony1,Symfony 1.4,Resource Id,我的行动: public function executePreview(sfWebRequest $request) { $this->setLayout('layout_preview'); $text=''; $text= $request->getGetParameter('text'); $this->img=''; $this->img2=$this->createImage(array('font_s
public function executePreview(sfWebRequest $request)
{
$this->setLayout('layout_preview');
$text='';
$text= $request->getGetParameter('text');
$this->img='';
$this->img2=$this->createImage(array('font_size'=>10, 'line_spacing'=>1.5,'font'=>'dejavu', 'angle'=>10,
'preset'=>'BRCA', 'text'=>$text));
}
public function createImage( $params)
{
$x=0;
$y=0;
$interval= $params['font_size']*$params['line_spacing'];
$src_image= imagecreatefromjpeg(sfConfig::get('sf_web_dir').'/images/'.$params['preset'].'.jpg');
$black = imagecolorallocate($src_image, 0, 0, 0);
$lines=explode("\n", $params['text']);
putenv('GDFONTPATH='.join(':',sfConfig::get('app_font_path')));
$fontname=$params['font'].'.ttf';
foreach($lines as $i=>$line):
imagettftext($src_image, $params['font_size'], $params['angle'], $x, $y+$i*$interval, $black, $fontname, $line);
endforeach;
return $src_image;
}
在我的模板中:
<?php
imagejpeg($img2);
?>
我不能将资源标识符传递给模板吗?有什么办法可以解决这个问题?我在别处也需要这个
createImage
函数,我只是想按照DRY
imagejpeg创建图像并返回一个布尔值,我不明白你为什么在视图中调用它,它应该保留到你的操作中,实际上我根本不会在你的情况下使用view
尝试以下方法:
$this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
$this->getResponse()->setContent($src_image);
$this->setLayout(false);
或者直接用PHP设置header并返回sfView::header_ONLY我遵照您的建议,将
imagejpeg()
调用从视图移动到操作。现在好了。谢谢
$this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
$this->getResponse()->setContent($src_image);
$this->setLayout(false);