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

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