duanou1904 2011-12-12 12:14
浏览 21
已采纳

symfony将资源标识符从action传递给模板

my action:

  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("
", $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;
  }

in my template:

<?php   
 imagejpeg($img2);
?>

but when trying to GET /modulename/preview?preview=something&text=somethingelse, it gives an error. viewing the source of the webpage obtained, I see :

<title>InvalidArgumentException: Unable to escape value &quot;NULL&quot;.</title>

Can I not pass resource identifier to the template? What can be a way around that? I need this createImage function elsewhere also, I'm just trying to follow DRY

  • 写回答

1条回答 默认 最新

  • douou1891 2011-12-12 12:55
    关注

    imagejpeg creates the image and returns a boolean, i don't get why you call it in your view, it should stay to your action, actually i wouldn't use view at all in your case.

    try something like :

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

    or directly setting header with PHP and returning sfView::HEADER_ONLY

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b