Text_CAPTCHA
[ class tree: Text_CAPTCHA ] [ index: Text_CAPTCHA ] [ all elements ]

Source for file Image.php

Documentation is available at Image.php

  1. <?php
  2. /**
  3.  * Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
  4.  *
  5.  * Class to create a graphical Turing test
  6.  *
  7.  * TODOs:
  8.  * + refine the obfuscation algorithm :-)
  9.  * + learn how to use Image_Text better (or remove dependency)
  10.  * 
  11.  * 
  12.  * @license PHP License, version 3.0
  13.  * @author Christian Wenz <wenz@php.net>
  14.  */
  15.  
  16. {
  17.  
  18.     /**
  19.      * Image object
  20.      *
  21.      * @access private
  22.      * @var resource 
  23.      */
  24.     var $_im;
  25.  
  26.     /**
  27.      * Image_Text object
  28.      *
  29.      * @access private
  30.      * @var resource 
  31.      */
  32.     var $_imt;
  33.  
  34.     /**
  35.      * Width of CAPTCHA
  36.      *
  37.      * @access private
  38.      * @var int 
  39.      */
  40.     var $_width;
  41.  
  42.     /**
  43.      * Height of CAPTCHA
  44.      *
  45.      * @access private
  46.      * @var int 
  47.      */
  48.     var $_height;
  49.  
  50.     /**
  51.      * Further options (for Image_Text)
  52.      *
  53.      * @access private
  54.      * @var array 
  55.      */
  56.     var $_options;
  57.  
  58.     /**
  59.      * init function
  60.      *
  61.      * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
  62.      *
  63.      * @param   int     $width      Width of image
  64.      * @param   int     $height     Height of image
  65.      * @param   string  $phrase     The "secret word" of the CAPTCHA
  66.      * @param   array   $options    further options (for Image_Text)
  67.      * @access public
  68.      * @return  mixed        true upon success, PEAR error otherwise
  69.      */
  70.  
  71.     function init($width = 200$height = 80$phrase = null$options = null)
  72.     {
  73.         if (is_int($width&& is_int($height)) {
  74.             $this->_width $width;
  75.             $this->_height $height
  76.             if (empty($phrase)) {
  77.                 $this->_createPhrase();
  78.             else {
  79.                 $this->_phrase $phrase;
  80.             }
  81.             if (empty($options)) {
  82.                 $this->_options = array(
  83.                     'font_size' => 24,
  84.                     'font_path' => './',
  85.                     'font_file' => 'COUR.TTF'
  86.                 );
  87.             else {
  88.                 $this->_options $options;
  89.                 if (!isset($this->_options['font_size'])) {
  90.                     $this->_options['font_size'= 24;
  91.                 }
  92.                 if (!isset($this->_options['font_path'])) {
  93.                     $this->_options['font_path''./';
  94.                 }
  95.                 if (!isset($this->_options['font_file'])) {
  96.                     $this->_options['font_file''COUR.TTF';
  97.                 }
  98.             }
  99.             $retval $this->_createCAPTCHA();
  100.             if (PEAR::isError($retval)) {
  101.                 return PEAR::raiseError($retval->getMessage());
  102.             else {
  103.                 return true;
  104.             }
  105.         else {
  106.             return PEAR::raiseError('Use numeric CAPTCHA dimensions!');
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Create random CAPTCHA phrase, Image edition (with size check)
  112.      *
  113.      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
  114.      *
  115.      * @access  private
  116.      */
  117.     function _createPhrase()
  118.     {
  119.         $len intval(min(8$this->_width / 25));
  120.         $this->_phrase = Text_Password::create($len);
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Create CAPTCHA image
  126.      *
  127.      * This method creates a CAPTCHA image
  128.      *
  129.      * @access  private
  130.      */
  131.     function _createCAPTCHA()
  132.     {
  133.         $options['canvas'= array(
  134.             'width' => $this->_width,
  135.             'height' => $this->_height
  136.         )
  137.         $options['width'$this->_width - 20;
  138.         $options['height'$this->_height - 20; 
  139.         $options['cx'ceil(($this->_width/ 2 + 10);
  140.         $options['cy'ceil(($this->_height/ 2 + 10)
  141.         $options['angle'rand(030- 15;
  142.         $options['font_size'$this->_options['font_size'];
  143.         $options['font_path'$this->_options['font_path'];
  144.         $options['font_file'$this->_options['font_file'];
  145.         $options['color'= array('#FFFFFF''#000000');
  146.         $options['max_lines'= 1;
  147.         $options['mode''auto';
  148.         $this->_imt = new Image_Text
  149.             $this->_phrase,
  150.             $options
  151.         );
  152.         if (PEAR::isError($this->_imt->init())) {
  153.             return PEAR::raiseError('Error initializing Image_Text (font missing?!)');
  154.         }
  155.         $this->_imt->measurize();
  156.         $this->_imt->render()
  157.         $this->_im =$this->_imt->getImg()
  158.         $white imagecolorallocate($this->_im0xFF0xFF0xFF);
  159.         //some obfuscation
  160.         for ($i=0; $i<3; $i++{
  161.             $x1 rand(0$this->_width - 1);
  162.             $y1 rand(0round($this->_height / 100));
  163.             $x2 rand(0round($this->_width / 100));
  164.             $y2 rand(0$this->_height - 1);
  165.             imageline($this->_im$x1$y1$x2$y2$white);
  166.             $x1 rand(0$this->_width - 1);
  167.             $y1 $this->_height rand(1round($this->_height / 100));
  168.             $x2 $this->_width rand(1round($this->_width / 100));
  169.             $y2 rand(0$this->_height - 1);
  170.             imageline($this->_im$x1$y1$x2$y2$white);
  171.             $cx rand(0$this->_width - 50+ 25;
  172.             $cy rand(0$this->_height - 50+ 25;
  173.             $w rand(124);
  174.             imagearc($this->_im$cx$cy$w$w0360$white);
  175.         }
  176.     }
  177.  
  178.  
  179.     /**
  180.      * Return CAPTCHA as image resource
  181.      *
  182.      * This method returns the CAPTCHA as GD2 image resource
  183.      *
  184.      * @access  public
  185.      * @return  im        image resource
  186.      */
  187.     function getCAPTCHA()
  188.     {
  189.         return $this->_im;
  190.     }
  191.  
  192.     /**
  193.      * Return CAPTCHA as PNG
  194.      *
  195.      * This method returns the CAPTCHA as PNG
  196.      *
  197.      * @access  public
  198.      */
  199.     function getCAPTCHAAsPNG()
  200.     {
  201.         if (is_resource($this->_im)) {
  202.             ob_start();
  203.             imagepng($this->_im);
  204.             $data ob_get_contents();
  205.             ob_end_clean();
  206.             return $data;
  207.         else {
  208.             return PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');        
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Return CAPTCHA as JPEG
  214.      *
  215.      * This method returns the CAPTCHA as JPEG
  216.      *
  217.      * @access  public
  218.      */
  219.     function getCAPTCHAAsJPEG()
  220.     {
  221.         if (is_resource($this->_im)) {
  222.             ob_start();
  223.             imagejpeg($this->_im);
  224.             $data ob_get_contents();
  225.             ob_end_clean();
  226.             return $data;
  227.         else {
  228.             return PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');        
  229.         }
  230.     }
  231.  
  232. }

Documentation generated on Mon, 11 Mar 2019 14:18:44 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.