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.  * 
  15.  * @license PHP License, version 3.0
  16.  * @author Christian Wenz <wenz@php.net>
  17.  * @todo refine the obfuscation algorithm :-)
  18.  * @todo learn how to use Image_Text better (or remove dependency)
  19.  */
  20.  
  21. {
  22.  
  23.     /**
  24.      * Image object
  25.      *
  26.      * @access private
  27.      * @var resource 
  28.      */
  29.     var $_im;
  30.  
  31.     /**
  32.      * Image_Text object
  33.      *
  34.      * @access private
  35.      * @var resource 
  36.      */
  37.     var $_imt;
  38.  
  39.     /**
  40.      * Width of CAPTCHA
  41.      *
  42.      * @access private
  43.      * @var int 
  44.      */
  45.     var $_width;
  46.  
  47.     /**
  48.      * Height of CAPTCHA
  49.      *
  50.      * @access private
  51.      * @var int 
  52.      */
  53.     var $_height;
  54.  
  55.     /**
  56.      * CAPTCHA output format
  57.      *
  58.      * @access private
  59.      * @var string 
  60.      */
  61.     var $_output;
  62.  
  63.     /**
  64.      * Further options (here: for Image_Text)
  65.      *
  66.      * @access private
  67.      * @var array 
  68.      */
  69.     var $_imageOptions;
  70.  
  71.     /**
  72.      * Whether the immage resource has been created
  73.      *
  74.      * @access private
  75.      * @var boolean 
  76.      */
  77.     var $_created = false;
  78.  
  79.     /**
  80.      * Last error
  81.      *
  82.      * @access protected
  83.      * @var PEAR_Error 
  84.      */
  85.     var $_error = null;
  86.  
  87.     /**
  88.      * init function
  89.      *
  90.      * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
  91.      *
  92.      * @param   array   $options    CAPTCHA options
  93.      * @access public
  94.      * @return  mixed   true upon success, PEAR error otherwise
  95.      */
  96.     function init($options = array())
  97.     {
  98.         if (!is_array($options)) {
  99.             // Compatibility mode ... in future versions, these two
  100.             // lines of code will be used: 
  101.             // $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!');
  102.             // return $this->_error;                  
  103.             $o = array();
  104.             $args func_get_args();
  105.             if (isset($args[0])) {
  106.                 $o['width'$args[0];
  107.             }    
  108.             if (isset($args[1])) {
  109.                 $o['height'$args[1];
  110.             }    
  111.             if (isset($args[2]&& $args[2!= null{
  112.                 $o['phrase'$args[2];
  113.             }
  114.             if (isset($args[3]&& is_array($args[3])) {
  115.                 $o['imageOptions'$args[3];
  116.             }
  117.             $options $o;
  118.         }
  119.         if (is_array($options)) 
  120.             if (isset($options['width']&& is_int($options['width'])) {
  121.               $this->_width $options['width'];
  122.             else {
  123.               $this->_width = 200; 
  124.             }
  125.             if (isset($options['height']&& is_int($options['height'])) {
  126.               $this->_height $options['height'];
  127.             else {
  128.               $this->_height = 80; 
  129.             }
  130.             if (!isset($options['phrase']|| empty($options['phrase'])) {
  131.                 $this->_createPhrase();
  132.             else {
  133.                 $this->_phrase $options['phrase'];
  134.             }
  135.             if (!isset($options['output']|| empty($options['output'])) {
  136.                 $this->_output 'resource';
  137.             else {
  138.                 $this->_output $options['output'];
  139.             
  140.             if (!isset($options['imageOptions']|| !is_array($options['imageOptions']|| count($options['imageOptions']== 0{
  141.                 $this->_imageOptions = array(
  142.                     'font_size' => 24,
  143.                     'font_path' => './',
  144.                     'font_file' => 'COUR.TTF'
  145.                 );
  146.             else {
  147.                 $this->_imageOptions $options['imageOptions'];
  148.                 if (!isset($this->_imageOptions['font_size'])) {
  149.                     $this->_imageOptions['font_size'= 24;
  150.                 }
  151.                 if (!isset($this->_imageOptions['font_path'])) {
  152.                     $this->_imageOptions['font_path''./';
  153.                 }
  154.                 if (!isset($this->_imageOptions['font_file'])) {
  155.                     $this->_imageOptions['font_file''COUR.TTF';
  156.                 }
  157.             }
  158.             return true;
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Create random CAPTCHA phrase, Image edition (with size check)
  164.      *
  165.      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
  166.      *
  167.      * @access  private
  168.      */
  169.     function _createPhrase()
  170.     {
  171.         $len intval(min(8$this->_width / 25));
  172.         $this->_phrase = Text_Password::create($len);
  173.         $this->_created = false;
  174.     }
  175.  
  176.     /**
  177.      * Create CAPTCHA image
  178.      *
  179.      * This method creates a CAPTCHA image
  180.      *
  181.      * @access  private
  182.      * @return  void   PEAR_Error on error
  183.      */
  184.     function _createCAPTCHA()
  185.     {
  186.         if ($this->_error{
  187.             return $this->_error;
  188.         }
  189.         if ($this->_created{
  190.             return;
  191.         }
  192.         $options['canvas'= array(
  193.             'width' => $this->_width,
  194.             'height' => $this->_height
  195.         )
  196.         $options['width'$this->_width - 20;
  197.         $options['height'$this->_height - 20; 
  198.         $options['cx'ceil(($this->_width/ 2 + 10);
  199.         $options['cy'ceil(($this->_height/ 2 + 10)
  200.         $options['angle'rand(030- 15;
  201.         $options['font_size'$this->_imageOptions['font_size'];
  202.         $options['font_path'$this->_imageOptions['font_path'];
  203.         $options['font_file'$this->_imageOptions['font_file'];
  204.         $options['color'= array('#FFFFFF''#000000');
  205.         $options['max_lines'= 1;
  206.         $options['mode''auto';
  207.         $this->_imt = new Image_Text
  208.             $this->_phrase,
  209.             $options
  210.         );
  211.         if (PEAR::isError($this->_imt->init())) {
  212.             $this->_error = PEAR::raiseError('Error initializing Image_Text (font missing?!)');
  213.             return $this->_error;
  214.         else {
  215.             $this->_created = true; 
  216.         }
  217.         $this->_imt->measurize();
  218.         $this->_imt->render()
  219.         $this->_im =$this->_imt->getImg()
  220.         $white imagecolorallocate($this->_im0xFF0xFF0xFF);
  221.         //some obfuscation
  222.         for ($i = 0; $i < 3; $i++{
  223.             $x1 rand(0$this->_width - 1);
  224.             $y1 rand(0round($this->_height / 100));
  225.             $x2 rand(0round($this->_width / 100));
  226.             $y2 rand(0$this->_height - 1);
  227.             imageline($this->_im$x1$y1$x2$y2$white);
  228.             $x1 rand(0$this->_width - 1);
  229.             $y1 $this->_height rand(1round($this->_height / 100));
  230.             $x2 $this->_width rand(1round($this->_width / 100));
  231.             $y2 rand(0$this->_height - 1);
  232.             imageline($this->_im$x1$y1$x2$y2$white);
  233.             $cx rand(0$this->_width - 50+ 25;
  234.             $cy rand(0$this->_height - 50+ 25;
  235.             $w rand(124);
  236.             imagearc($this->_im$cx$cy$w$w0360$white);
  237.         }
  238.     }
  239.  
  240.     /**
  241.      * Return CAPTCHA as image resource
  242.      *
  243.      * This method returns the CAPTCHA depending on the output format
  244.      *
  245.      * @access  public
  246.      * @return  mixed        image resource or PEAR error
  247.      */
  248.     function getCAPTCHA()
  249.     {
  250.         $retval $this->_createCAPTCHA();
  251.         if (PEAR::isError($retval)) {
  252.             return PEAR::raiseError($retval->getMessage());
  253.         }
  254.         
  255.         if ($this->_output == 'gif' && !function_exists('imagegif')) {
  256.             $this->_output 'png';
  257.         }
  258.  
  259.         switch ($this->_output{
  260.             case 'png':
  261.                 return $this->getCAPTCHAAsPNG();
  262.                 break;
  263.             case 'jpg'
  264.             case 'jpeg':
  265.                 return $this->getCAPTCHAAsJPEG();
  266.                 break;
  267.             case 'gif':
  268.                 return $this->getCAPTCHAAsGIF();
  269.                 break;
  270.             case 'resource':
  271.             default:
  272.                 return $this->_im;
  273.         }
  274.     }
  275.  
  276.     /**
  277.      * Return CAPTCHA as PNG
  278.      *
  279.      * This method returns the CAPTCHA as PNG
  280.      *
  281.      * @access  public
  282.      * @return  mixed        image contents or PEAR error
  283.      */
  284.     function getCAPTCHAAsPNG()
  285.     {
  286.         $retval $this->_createCAPTCHA();
  287.         if (PEAR::isError($retval)) {
  288.             return PEAR::raiseError($retval->getMessage());
  289.         }
  290.  
  291.         if (is_resource($this->_im)) {
  292.             ob_start();
  293.             imagepng($this->_im);
  294.             $data ob_get_contents();
  295.             ob_end_clean();
  296.             return $data;
  297.         else {
  298.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  299.             return $this->_error;
  300.         }
  301.     }
  302.  
  303.     /**
  304.      * Return CAPTCHA as JPEG
  305.      *
  306.      * This method returns the CAPTCHA as JPEG
  307.      *
  308.      * @access  public
  309.      * @return  mixed        image contents or PEAR error
  310.      */
  311.     function getCAPTCHAAsJPEG()
  312.     {
  313.         $retval $this->_createCAPTCHA();
  314.         if (PEAR::isError($retval)) {
  315.             return PEAR::raiseError($retval->getMessage());
  316.         }
  317.  
  318.         if (is_resource($this->_im)) {
  319.             ob_start();
  320.             imagejpeg($this->_im);
  321.             $data ob_get_contents();
  322.             ob_end_clean();
  323.             return $data;
  324.         else {
  325.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  326.             return $this->_error;
  327.         }
  328.     }
  329.  
  330.     /**
  331.      * Return CAPTCHA as GIF
  332.      *
  333.      * This method returns the CAPTCHA as GIF
  334.      *
  335.      * @access  public
  336.      * @return  mixed        image contents or PEAR error
  337.      */
  338.     function getCAPTCHAAsGIF()
  339.     {
  340.         $retval $this->_createCAPTCHA();
  341.         if (PEAR::isError($retval)) {
  342.             return PEAR::raiseError($retval->getMessage());
  343.         }
  344.  
  345.         if (is_resource($this->_im)) {
  346.             ob_start();
  347.             imagegif($this->_im);
  348.             $data ob_get_contents();
  349.             ob_end_clean();
  350.             return $data;
  351.         else {
  352.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  353.             return $this->_error;
  354.         }
  355.     }
  356.  
  357.     /**
  358.      * __wakeup method (PHP 5 only)
  359.      */
  360.     function __wakeup()
  361.     {
  362.         $this->_created = false;
  363.     
  364. }

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