Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?
  2.     session_start();
  3.     class captcha
  4.     {
  5.         public $CAPTCHA_TYPE_CALC = 1;
  6.         public $CAPTCHA_TYPE_CHARS = 2;
  7.        
  8.         //Einstellungen
  9.         private $stringLength = 6;
  10.         private $calcMaxValue = 10;
  11.         private $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.         //Einstellungen
  13.        
  14.         public function create($type){
  15.             header("Content-Type: image/png");
  16.            
  17.             if($type == $this->CAPTCHA_TYPE_CALC){
  18.                 $img = ImageCreate(300, 100);
  19.                 $black = ImageColorAllocate($img, 0 , 0, 0);
  20.                
  21.                 $positionX = 70;
  22.                 $digit = Array(rand(0, $this->calcMaxValue), rand(0, $this->calcMaxValue));
  23.                
  24.                 for($i = 0; $i < count($digit); $i++){
  25.                     $rotation = rand(0, 30);
  26.                     $r = rand(0,255);
  27.                     $g = rand(0,255);
  28.                     $b = rand(0,255);
  29.                     $white = ImageColorAllocate($img, $r, $b, $g);
  30.                     imagettftext($img, 30.5, $rotation, $positionX, 60, $white, "./fonts/TAHOMA.TTF", $digit[$i]);
  31.                     if($i == 0){
  32.                         $positionX += 90;
  33.                         imagettftext($img, 30.5, $rotation, $positionX, 60, $white, "./fonts/TAHOMA.TTF", "+");
  34.                     }
  35.                     $positionX += 90;
  36.                 }
  37.                 $_SESSION["calcResult"] = $digit[0] + $digit[1];
  38.             }else if($type == $this->CAPTCHA_TYPE_CHARS){
  39.                 $img = ImageCreate($this->stringLength * 50, 100);
  40.                 $black = ImageColorAllocate($img, 0 , 0, 0);
  41.                
  42.                 $positionX = 70;
  43.                 for($i = 0; $i < $this->stringLength; $i++) {
  44.                     $rotation = rand(0, 45);
  45.                     $r = rand(0,255);
  46.                     $g = rand(0,255);
  47.                     $b = rand(0,255);
  48.                     $white = ImageColorAllocate($img, $r, $b, $g);
  49.                     $char = $this->characters[rand(0, strlen($this->characters))];
  50.                     $size = rand(30, 50);
  51.                     imagettftext($img, $size, $rotation, $positionX, 60, $white, "./fonts/ARIAL.TTF", $char);
  52.                     $positionX += ($size - 10);
  53.                     $string .= $char;
  54.                 }
  55.                 $_SESSION["charsResult"] = $string;
  56.             }
  57.            
  58.             ImagePNG($img);
  59.             ImageDestroy($img);
  60.         }
  61.        
  62.         public function checkCaptcha($type, $result){
  63.             if($type == $this->CAPTCHA_TYPE_CALC){
  64.                 if($result == $_SESSION["calcResult"]){
  65.                     return true;
  66.                 }else{
  67.                     return false;
  68.                 }
  69.             }else if($type == $this->CAPTCHA_TYPE_CHARS){
  70.                 if($result == $_SESSION["charsResult"]){
  71.                     return true;
  72.                 }else{
  73.                     return false;
  74.                 }
  75.             }
  76.         }
  77.     }
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement