/**
  * Draw thumbnail result to resource.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  public
  */
 function render()
 {
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource');
     }
     // Estimate a rectangular portion of the source image and a size of the target image
     if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_CROP) {
         if ($this->options['percent']) {
             $W = floor($this->options['percent'] / 100 * $this->imageInfo['width']);
             $H = floor($this->options['percent'] / 100 * $this->imageInfo['height']);
         } else {
             $W = $this->options['width'];
             $H = $this->options['height'];
         }
         $width = $W;
         $height = $H;
         $Y = $this->_coord('valign', 'height', $H);
         $X = $this->_coord('halign', 'width', $W);
     } else {
         $W = $this->imageInfo['width'];
         $H = $this->imageInfo['height'];
         $X = 0;
         $Y = 0;
         if ($this->options['percent']) {
             $width = floor($this->options['percent'] / 100 * $W);
             $height = floor($this->options['percent'] / 100 * $H);
         } else {
             $width = $this->options['width'];
             $height = $this->options['height'];
             if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MIN) {
                 $Ww = $W / $width;
                 $Hh = $H / $height;
                 if ($Ww > $Hh) {
                     $W = floor($width * $Hh);
                     $X = $this->_coord('halign', 'width', $W);
                 } else {
                     $H = floor($height * $Ww);
                     $Y = $this->_coord('valign', 'height', $H);
                 }
             } else {
                 if ($H > $W) {
                     $width = floor($height / $H * $W);
                 } else {
                     $height = floor($width / $W * $H);
                 }
             }
         }
     }
     // Create the target image
     if (function_exists('imagecreatetruecolor')) {
         $target = imagecreatetruecolor($width, $height);
     } else {
         $target = imagecreate($width, $height);
     }
     if (!Image_Tools::isGDImageResource($target)) {
         return PEAR::raiseError('Cannot initialize new GD image stream');
     }
     // enable transparency if supported
     if (Image_Tools_Utils::compareGDVersion('2.0.28', '>=')) {
         // imagealphablending() and imagesavealpha() requires GD 2.0.38
         imagealphablending($target, false);
         imagesavealpha($target, true);
     }
     // Copy the source image to the target image
     if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_CROP) {
         $result = imagecopy($target, $this->resultImage, 0, 0, $X, $Y, $W, $H);
     } elseif (function_exists('imagecopyresampled')) {
         $result = imagecopyresampled($target, $this->resultImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     } else {
         $result = imagecopyresized($target, $this->resultImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     }
     if (!$result) {
         return PEAR::raiseError('Cannot resize image');
     }
     // Free a memory from the source image and save the resulting thumbnail
     imagedestroy($this->resultImage);
     $this->resultImage = $target;
     return true;
 }