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 BSD License
  17.  * @author Christian Wenz <wenz@php.net>
  18.  * @todo refine the obfuscation algorithm :-)
  19.  * @todo consider removing Image_Text 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' => '#555555');
  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.                 $phraseoptions (isset($options['phraseOptions']&& is_array($options['phraseOptions'])) $options['phraseOptions': array();
  139.                 $this->_createPhrase($phraseoptions);
  140.             else {
  141.                 $this->_phrase $options['phrase'];
  142.             }
  143.             if (!isset($options['output']|| empty($options['output'])) {
  144.                 $this->_output 'resource';
  145.             else {
  146.                 $this->_output $options['output'];
  147.             
  148.             if (isset($options['imageOptions']&& is_array($options['imageOptions']&& count($options['imageOptions']> 0{
  149.                 $this->_imageOptions array_merge($this->_imageOptions$options['imageOptions'])
  150.             }
  151.             return true;
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Create random CAPTCHA phrase, Image edition (with size check)
  157.      *
  158.      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
  159.      *
  160.      * @access  private
  161.      */
  162.     function _createPhrase($options = array())
  163.     {
  164.         $len intval(min(8$this->_width / 25));
  165.         if (!is_array($options|| count($options=== 0{
  166.             $this->_phrase = Text_Password::create($len);
  167.         else {
  168.             if (count($options=== 1{
  169.                 $this->_phrase = Text_Password::create($len$options[0]);
  170.             else {
  171.                 $this->_phrase = Text_Password::create($len$options[0]$options[1]);
  172.             }
  173.         }
  174.         $this->_created = false;
  175.     }
  176.  
  177.     /**
  178.      * Create CAPTCHA image
  179.      *
  180.      * This method creates a CAPTCHA image
  181.      *
  182.      * @access  private
  183.      * @return  void   PEAR_Error on error
  184.      */
  185.     function _createCAPTCHA()
  186.     {
  187.         if ($this->_error{
  188.             return $this->_error;
  189.         }
  190.         if ($this->_created{
  191.             return;
  192.         }
  193.         $options['canvas'= array(
  194.             'width' => $this->_width,
  195.             'height' => $this->_height
  196.         )
  197.         $options['width'$this->_width - 20;
  198.         $options['height'$this->_height - 20; 
  199.         $options['cx'ceil(($this->_width/ 2 + 10);
  200.         $options['cy'ceil(($this->_height/ 2 + 10)
  201.         $options['angle'rand(030- 15;
  202.         $options['font_size'$this->_imageOptions['font_size'];
  203.         $options['font_path'$this->_imageOptions['font_path'];
  204.         $options['font_file'$this->_imageOptions['font_file'];
  205.         $options['color'= array($this->_imageOptions['text_color']);
  206.         $options['background_color'$this->_imageOptions['background_color'];
  207.         $options['max_lines'= 1;
  208.         $options['mode''auto';
  209.         do {
  210.             $this->_imt = new Image_Text
  211.                 $this->_phrase,
  212.                 $options
  213.             );
  214.             if (PEAR::isError($e $this->_imt->init())) {
  215.                 $this->_error = PEAR::raiseError(
  216.                     sprintf('Error initializing Image_Text (%s)',
  217.                     $e->getMessage()));
  218.                 return $this->_error;
  219.             else {
  220.                 $this->_created = true; 
  221.             }
  222.             $result $this->_imt->measurize();
  223.         while ($result === false && --$options['font_size'> 0);
  224.         if ($result === false{
  225.             $this->_error = PEAR::raiseError('The text provided does not fit in the image dimensions');
  226.             return $this->_error;
  227.         }
  228.         $this->_imt->render()
  229.         $this->_im =$this->_imt->getImg()
  230.         $colors $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']);
  231.         $lines_color imagecolorallocate($this->_im$colors['r']$colors['g']$colors['b']);
  232.         //some obfuscation
  233.         for ($i = 0; $i < 3; $i++{
  234.             $x1 rand(0$this->_width - 1);
  235.             $y1 rand(0round($this->_height / 100));
  236.             $x2 rand(0round($this->_width / 100));
  237.             $y2 rand(0$this->_height - 1);
  238.             imageline($this->_im$x1$y1$x2$y2$lines_color);
  239.             $x1 rand(0$this->_width - 1);
  240.             $y1 $this->_height rand(1round($this->_height / 100));
  241.             $x2 $this->_width rand(1round($this->_width / 100));
  242.             $y2 rand(0$this->_height - 1);
  243.             imageline($this->_im$x1$y1$x2$y2$lines_color);
  244.             $cx rand(0$this->_width - 50+ 25;
  245.             $cy rand(0$this->_height - 50+ 25;
  246.             $w rand(124);
  247.             imagearc($this->_im$cx$cy$w$w0360$lines_color);
  248.         }
  249.     }
  250.  
  251.     /**
  252.      * Return CAPTCHA as image resource
  253.      *
  254.      * This method returns the CAPTCHA depending on the output format
  255.      *
  256.      * @access  public
  257.      * @return  mixed        image resource or PEAR error
  258.      */
  259.     function getCAPTCHA()
  260.     {
  261.         $retval $this->_createCAPTCHA();
  262.         if (PEAR::isError($retval)) {
  263.             return PEAR::raiseError($retval->getMessage());
  264.         }
  265.         
  266.         if ($this->_output == 'gif' && !function_exists('imagegif')) {
  267.             $this->_output 'png';
  268.         }
  269.  
  270.         switch ($this->_output{
  271.             case 'png':
  272.                 return $this->getCAPTCHAAsPNG();
  273.                 break;
  274.             case 'jpg'
  275.             case 'jpeg':
  276.                 return $this->getCAPTCHAAsJPEG();
  277.                 break;
  278.             case 'gif':
  279.                 return $this->getCAPTCHAAsGIF();
  280.                 break;
  281.             case 'resource':
  282.             default:
  283.                 return $this->_im;
  284.         }
  285.     }
  286.  
  287.     /**
  288.      * Return CAPTCHA as PNG
  289.      *
  290.      * This method returns the CAPTCHA as PNG
  291.      *
  292.      * @access  public
  293.      * @return  mixed        image contents or PEAR error
  294.      */
  295.     function getCAPTCHAAsPNG()
  296.     {
  297.         $retval $this->_createCAPTCHA();
  298.         if (PEAR::isError($retval)) {
  299.             return PEAR::raiseError($retval->getMessage());
  300.         }
  301.  
  302.         if (is_resource($this->_im)) {
  303.             ob_start();
  304.             imagepng($this->_im);
  305.             $data ob_get_contents();
  306.             ob_end_clean();
  307.             return $data;
  308.         else {
  309.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  310.             return $this->_error;
  311.         }
  312.     }
  313.  
  314.     /**
  315.      * Return CAPTCHA as JPEG
  316.      *
  317.      * This method returns the CAPTCHA as JPEG
  318.      *
  319.      * @access  public
  320.      * @return  mixed        image contents or PEAR error
  321.      */
  322.     function getCAPTCHAAsJPEG()
  323.     {
  324.         $retval $this->_createCAPTCHA();
  325.         if (PEAR::isError($retval)) {
  326.             return PEAR::raiseError($retval->getMessage());
  327.         }
  328.  
  329.         if (is_resource($this->_im)) {
  330.             ob_start();
  331.             imagejpeg($this->_im);
  332.             $data ob_get_contents();
  333.             ob_end_clean();
  334.             return $data;
  335.         else {
  336.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  337.             return $this->_error;
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Return CAPTCHA as GIF
  343.      *
  344.      * This method returns the CAPTCHA as GIF
  345.      *
  346.      * @access  public
  347.      * @return  mixed        image contents or PEAR error
  348.      */
  349.     function getCAPTCHAAsGIF()
  350.     {
  351.         $retval $this->_createCAPTCHA();
  352.         if (PEAR::isError($retval)) {
  353.             return PEAR::raiseError($retval->getMessage());
  354.         }
  355.  
  356.         if (is_resource($this->_im)) {
  357.             ob_start();
  358.             imagegif($this->_im);
  359.             $data ob_get_contents();
  360.             ob_end_clean();
  361.             return $data;
  362.         else {
  363.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  364.             return $this->_error;
  365.         }
  366.     }
  367.  
  368.     /**
  369.      * __wakeup method (PHP 5 only)
  370.      */
  371.     function __wakeup()
  372.     {
  373.         $this->_created = false;
  374.     
  375. }

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