phpsymfony1symfony-1.4resource-id

symfony pass resource identifier to template from 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("\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;
  }

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


Solution

  • 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