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

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