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

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