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

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