enble generator to be used without framework
[pear] / Text / CAPTCHA / Driver / Image.php
index 4ed2858..6949afb 100644 (file)
@@ -1,8 +1,6 @@
 <?php
 /**
- *
  * Require Image_Text class for generating the text.
- *
  */
 require_once 'Text/CAPTCHA.php';
 require_once 'Image/Text.php';
@@ -11,10 +9,9 @@ require_once 'Image/Text.php';
  * Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
  *
  * Class to create a graphical Turing test 
- *
  * 
+ * @author  Christian Wenz <wenz@php.net>
  * @license BSD License
- * @author Christian Wenz <wenz@php.net>
  * @todo refine the obfuscation algorithm :-)
  * @todo consider removing Image_Text dependency
  */
@@ -74,7 +71,8 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
         'font_file'        => 'COUR.TTF',
         'text_color'       => '#000000',
         'lines_color'      => '#CACACA',
-        'background_color' => '#555555');
+        'background_color' => '#555555',
+        'antialias'        => false);
         
     /**
      * Whether the immage resource has been created
@@ -97,12 +95,14 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
      *
      * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
      *
-     * @param   array   $options    CAPTCHA options
+     * @param array $options CAPTCHA options
+     *
      * @access public
-     * @return  mixed   true upon success, PEAR error otherwise
+     * @return mixed true upon success, PEAR error otherwise
      */
     function init($options = array())
     {
+            
         if (!is_array($options)) {
             // Compatibility mode ... in future versions, these two
             // lines of code will be used: 
@@ -126,14 +126,14 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
         }
         if (is_array($options)) { 
             if (isset($options['width']) && is_int($options['width'])) {
-              $this->_width = $options['width'];
+                $this->_width = $options['width'];
             } else {
-              $this->_width = 200; 
+                $this->_width = 200; 
             }
             if (isset($options['height']) && is_int($options['height'])) {
-              $this->_height = $options['height'];
+                $this->_height = $options['height'];
             } else {
-              $this->_height = 80; 
+                $this->_height = 80; 
             }
             if (!isset($options['phrase']) || empty($options['phrase'])) {
                 $phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array();
@@ -149,8 +149,10 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
             if (isset($options['imageOptions']) && is_array($options['imageOptions']) && count($options['imageOptions']) > 0) {
                 $this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']); 
             }
+            
             return true;
         }
+        
     }
 
     /**
@@ -158,7 +160,10 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
      *
      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
      *
-     * @access  private
+     * @param array $options Optionally supply advanced options for the phrase creation
+     * 
+     * @access private
+     * @return void
      */
     function _createPhrase($options = array())
     {
@@ -207,15 +212,16 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
         $options['background_color'] = $this->_imageOptions['background_color'];
         $options['max_lines'] = 1;
         $options['mode'] = 'auto';
-        do {
+         do {
             $this->_imt = new Image_Text( 
                 $this->_phrase,
                 $options
             );
+            
+             
             if (PEAR::isError($e = $this->_imt->init())) {
-                $this->_error = PEAR::raiseError(
-                    sprintf('Error initializing Image_Text (%s)',
-                    $e->getMessage()));
+                $this->_error = PEAR::staticRaiseError(
+                    sprintf('Error initializing Image_Text (%s)', $e->getMessage()));
                 return $this->_error;
             } else {
                 $this->_created = true; 
@@ -226,8 +232,13 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
             $this->_error = PEAR::raiseError('The text provided does not fit in the image dimensions');
             return $this->_error;
         }
-        $this->_imt->render(); 
+        $this->_imt->render();
         $this->_im =& $this->_imt->getImg(); 
+        
+        if (isset($this->_imageOptions['antialias']) && $this->_imageOptions['antialias'] === true && function_exists('imageantialias')) {
+            imageantialias($this->_im, true);
+        }
+        
         $colors = $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']);
         $lines_color = imagecolorallocate($this->_im, $colors['r'], $colors['g'], $colors['b']);
         //some obfuscation
@@ -261,7 +272,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
     {
         $retval = $this->_createCAPTCHA();
         if (PEAR::isError($retval)) {
-            return PEAR::raiseError($retval->getMessage());
+            return PEAR::staticRaiseError($retval->getMessage());
         }
         
         if ($this->_output == 'gif' && !function_exists('imagegif')) {
@@ -297,17 +308,16 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
     {
         $retval = $this->_createCAPTCHA();
         if (PEAR::isError($retval)) {
-            return PEAR::raiseError($retval->getMessage());
+            return PEAR::staticRaiseError($retval->getMessage());
         }
-
-        if (is_resource($this->_im)) {
+         if (is_object($this->_im)) {
             ob_start();
             imagepng($this->_im);
             $data = ob_get_contents();
             ob_end_clean();
             return $data;
         } else {
-            $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
+            $this->_error = PEAR::staticRaiseError('Error creating CAPTCHA image (font missing?!)');
             return $this->_error;
         }
     }
@@ -327,7 +337,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
             return PEAR::raiseError($retval->getMessage());
         }
 
-        if (is_resource($this->_im)) {
+        if (is_object($this->_im)) {
             ob_start();
             imagejpeg($this->_im);
             $data = ob_get_contents();
@@ -354,7 +364,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
             return PEAR::raiseError($retval->getMessage());
         }
 
-        if (is_resource($this->_im)) {
+        if (is_object($this->_im)) {
             ob_start();
             imagegif($this->_im);
             $data = ob_get_contents();
@@ -368,6 +378,8 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
 
     /**
      * __wakeup method (PHP 5 only)
+     *
+     * @return void
      */
     function __wakeup()
     {