iBet7o

Clase para creación de un captcha matemático

Jul 25th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Captcha
  4.  * -------------------------------------
  5.  *
  6.  * @author iBet7o - <[email protected]>
  7.  */
  8. class Captcha
  9. {
  10.     private $_operations = array('+', '-', '*');
  11.  
  12.     private $_question = array();
  13.  
  14.     public $answer = NULL;
  15.  
  16.     private function _operation()
  17.     {
  18.         $this->_question[] = rand(1, 20);
  19.         $this->_question[] = $this->_operations[rand(0, 2)];
  20.         $this->_question[] = rand(1, 20);
  21.  
  22.         $cal = $this->_question[0];
  23.         if($this->_question[1] == '+')
  24.         {
  25.             $cal += $this->_question[2];
  26.         }
  27.         elseif($this->_question[1] == '-')
  28.         {
  29.             $cal -= $this->_question[2];
  30.         }
  31.         elseif($this->_question[1] == '*')
  32.         {
  33.             $cal *= $this->_question[2];
  34.         }
  35.  
  36.         $this->_question = implode(' ', $this->_question);
  37.         $this->answer = $cal;
  38.     }
  39.  
  40.     private function _image()
  41.     {
  42.         header("Content-type: image/png");
  43.  
  44.         $image = @imagecreate(8 * strlen($this->_question), 15);
  45.  
  46.         $background_color = imagecolorallocate($image, 255, 255, 255);
  47.  
  48.         $color = imagecolorallocate($image, 0, 0, 0);
  49.  
  50.         imagestring($image, 4, 0, 0,  $this->_question, $color);
  51.  
  52.         imagepng($image);
  53.         imagedestroy($image);
  54.     }
  55.  
  56.     public function show()
  57.     {
  58.         $this->_operation();
  59.         $this->_image();
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment