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.      */
  69.  
  70.     function init($width = 200$height = 80$phrase = null$options = null)
  71.     {
  72.         if (is_int($width&& is_int($height)) {
  73.             $this->_width $width;
  74.             $this->_height $height
  75.             if (empty($phrase)) {
  76.                 $this->_createPhrase();
  77.             else {
  78.                 $this->_phrase $phrase;
  79.             }
  80.             if (empty($options)) {
  81.                 $this->_options = array(
  82.                     'font_size' => 24,
  83.                     'font_path' => './',
  84.                     'font_file' => 'COUR.TTF'
  85.                 );
  86.             else {
  87.                 $this->_options $options;
  88.                 if (!isset($this->_options['font_size'])) {
  89.                     $this->_options['font_size'= 24;
  90.                 }
  91.                 if (!isset($this->_options['font_path'])) {
  92.                     $this->_options['font_path''./';
  93.                 }
  94.                 if (!isset($this->_options['font_file'])) {
  95.                     $this->_options['font_file''COUR.TTF';
  96.                 }
  97.             }
  98.             $retval $this->_createCAPTCHA();
  99.             if (PEAR::isError($retval)) {
  100.                 return PEAR::raiseError($retval->getMessage());
  101.             }
  102.         else {
  103.             return PEAR::raiseError('Use numeric CAPTCHA dimensions!');
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Create random CAPTCHA phrase, Image edition (with size check)
  109.      *
  110.      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
  111.      *
  112.      * @access  private
  113.      */
  114.     function _createPhrase()
  115.     {
  116.         $len intval(min(8$this->_width / 25));
  117.         $this->_phrase = Text_Password::create($len);
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Create CAPTCHA image
  123.      *
  124.      * This method creates a CAPTCHA image
  125.      *
  126.      * @access  private
  127.      */
  128.     function _createCAPTCHA()
  129.     {
  130.         $options['canvas'= array(
  131.             'width' => $this->_width,
  132.             'height' => $this->_height
  133.         )
  134.         $options['width'$this->_width - 20;
  135.         $options['height'$this->_height - 20; 
  136.         $options['cx'ceil(($this->_width/ 2 + 10);
  137.         $options['cy'ceil(($this->_height/ 2 + 10)
  138.         $options['angle'rand(030- 15;
  139.         $options['font_size'$this->_options['font_size'];
  140.         $options['font_path'$this->_options['font_path'];
  141.         $options['font_file'$this->_options['font_file'];
  142.         $options['color'= array('#FFFFFF''#000000');
  143.         $options['max_lines'= 1;
  144.         $options['mode''auto';
  145.         $this->_imt = new Image_Text
  146.             $this->_phrase,
  147.             $options
  148.         );
  149.         if (PEAR::isError($this->_imt->init())) {
  150.             return PEAR::raiseError('Error initializing Image_Text (font missing?!)');
  151.         }
  152.         $this->_imt->measurize();
  153.         $this->_imt->render()
  154.         $this->_im =$this->_imt->getImg()
  155.         $white imagecolorallocate($this->_im0xFF0xFF0xFF);
  156.         //some obfuscation
  157.         for ($i=0; $i<3; $i++{
  158.             $x1 rand(0$this->_width - 1);
  159.             $y1 rand(0round($this->_height / 100));
  160.             $x2 rand(0round($this->_width / 100));
  161.             $y2 rand(0$this->_height - 1);
  162.             imageline($this->_im$x1$y1$x2$y2$white);
  163.             $x1 rand(0$this->_width - 1);
  164.             $y1 $this->_height rand(1round($this->_height / 100));
  165.             $x2 $this->_width rand(1round($this->_width / 100));
  166.             $y2 rand(0$this->_height - 1);
  167.             imageline($this->_im$x1$y1$x2$y2$white);
  168.             $cx rand(0$this->_width - 50+ 25;
  169.             $cy rand(0$this->_height - 50+ 25;
  170.             $w rand(124);
  171.             imagearc($this->_im$cx$cy$w$w0360$white);
  172.         }
  173.     }
  174.  
  175.  
  176.     /**
  177.      * Return CAPTCHA as image resource
  178.      *
  179.      * This method returns the CAPTCHA as GD2 image resource
  180.      *
  181.      * @access  public
  182.      * @return  im        image resource
  183.      */
  184.     function getCAPTCHA()
  185.     {
  186.         return $this->_im;
  187.     }
  188.  
  189.     /**
  190.      * Return CAPTCHA as PNG
  191.      *
  192.      * This method returns the CAPTCHA as PNG
  193.      *
  194.      * @access  public
  195.      */
  196.     function getCAPTCHAAsPNG()
  197.     {
  198.         ob_start();
  199.         imagepng($this->_im);
  200.         $data ob_get_contents();
  201.         ob_end_clean();
  202.         return $data;
  203.     }
  204.  
  205.     /**
  206.      * Return CAPTCHA as JPEG
  207.      *
  208.      * This method returns the CAPTCHA as JPEG
  209.      *
  210.      * @access  public
  211.      */
  212.     function getCAPTCHAAsJPEG()
  213.     {
  214.         ob_start();
  215.         imagejpeg($this->_im);
  216.         $data ob_get_contents();
  217.         ob_end_clean();
  218.         return $data;
  219.     }
  220.  
  221. }

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