Advertisement
DJ_PhoeniX

Captcha 2.0

Nov 30th, 2011
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2.     function make_text($img, $c = 5){
  3.         $txt = "";
  4.         $arr = array();
  5.         for($i = 0; $i < $c; $i++){
  6.             switch(mt_rand(0,2)){
  7.                 case(0):
  8.                     $txt.=mt_rand(0,9);
  9.                     break;
  10.                 case(1):
  11.                     $txt.=chr(ord('a')+mt_rand(0,25));
  12.                     break;
  13.                 case(2):
  14.                     $txt.=chr(ord('A')+mt_rand(0,25));
  15.                     break;
  16.             }
  17.             $arr[] = $i;
  18.         }
  19.         shuffle($arr);
  20.         $sx = ImageSX($img);
  21.         $sy = ImageSY($img);
  22.         $wm = 0;
  23.         foreach($arr as $i){
  24.             $s = mt_rand($sy/2,$sy/3*2);
  25.             $a = mt_rand(-20,20);
  26.             $f = "font".mt_rand(1,6).".ttf";
  27.             $b = ImageTtfBBox($s,$a,$f,$txt[$i]);
  28.             $h = max($b[1]-$b[7],$b[3]-$b[5]);
  29.             $x = $i*$sx/$c-(($a>0)?$b[6]:0);
  30.             $y = mt_rand($h,max($h,$sy-($a<0?$b[3]:$b[1])));
  31.             $cl = ImageColorAllocateAlpha($img,0,0,0,64);
  32.             ImageTtfText($img,$s,$a,$x,$y,$cl,$f,$txt[$i]);
  33.             $x-=mt_rand(1,3);$y-=mt_rand(1,3);
  34.             $cl = ImageColorAllocateAlpha($img,255,255,255,56);
  35.             ImageTtfText($img,$s,$a,$x,$y,$cl,$f,$txt[$i]);
  36.         }
  37.         return $txt;
  38.     }
  39.     function make_wave($img){
  40.         $x = ImageSX($img);
  41.         $y = ImageSY($img);
  42.         $c = array();
  43.         $r = mt_rand(0,255);
  44.         $g = mt_rand(0,255);
  45.         $b = mt_rand(0,255);
  46.         $j = mt_rand(5,10);
  47.         for($i = 0; $i < $j; $i++)
  48.             $c[] = ImageColorAllocateAlpha($img,$r,$g,$b,($i/$j*127));
  49.         $start = mt_rand(0,$y);
  50.         $angle = mt_rand(-90,90);
  51.         for($i = 0; $i < $x; $i++){
  52.             $sine = sin($i/$j)*$j+($angle*$i/$y);
  53.             ImageSetPixel($img,$i,round(($start+$sine)%$y),$c[0]);
  54.             for($j = 1; $j < count($c); $j++){
  55.                 ImageSetPixel($img,$i,round(($start+$sine-$j)%$y),$c[$j]);
  56.                 ImageSetPixel($img,$i,round(($start+$sine+$j)%$y),$c[$j]);
  57.             }
  58.         }
  59.     }
  60.     function make_bg($img){
  61.         $x = ImageSX($img);
  62.         $y = ImageSY($img);
  63.         $bg = ImageCreateTrueColor($x,$y);
  64.         imageSaveAlpha($bg, true);
  65.         imageAlphaBlending($bg, true);
  66.         $c = ImageColorAllocate($bg,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  67.         ImageFilledRectangle($bg,0,0,$x,$y,$c);
  68.         $c=mt_rand(8,10);
  69.         for($i=0;$i<$c;$i++){
  70.             make_wave($bg);
  71.         }
  72.         ImageCopy($img,$bg,0,0,0,0,$x,$y);
  73.         ImageDestroy($bg);
  74.     }
  75.     $img = ImageCreateTrueColor(200,60);
  76.     imageSaveAlpha($img, true);
  77.     imageAlphaBlending($img, true);
  78.     make_bg($img);
  79.     $text = make_text($img);
  80.     ob_start();
  81.     ImagePng($img);
  82.     ImageDestroy($img);
  83.     $img = "data:image/png;base64,".base64_encode(ob_get_contents());
  84.     ob_end_clean();
  85.     echo "<img src=\"".$img."\" alt=\"captcha\"/>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement