Skip to content
Browse files

init

  • Loading branch information...
0 parents commit 387965e56521a8b61b9d820772a4f1d06493f517 @wintercn committed Jul 6, 2012
Showing with 226 additions and 0 deletions.
  1. +111 −0 captcha.php
  2. +115 −0 datauricaptcha.php
111 captcha.php
@@ -0,0 +1,111 @@
+<?php
+session_start();
+header ("Content-type: image/png");
+
+$str = "1,3,4,6,7,9,A,C,D,E,F,H,K,M,N,P,Q,R,T,W,X,Y"; //要显示的字符,可自己进行增删
+$list = explode(",", $str);
+$cmax = count($list) - 1;
+$code = '';
+for ( $i=0; $i < 7; $i++ ){
+ $randnum = mt_rand(0, $cmax);
+ $code .= $list[$randnum]; //取出字符,组合成为我们要的验证码字符
+}
+
+$_SESSION['captcha'] = $code;
+
+$im = @imagecreate (350, 90) or die ("Cannot Initialize new GD image stream");
+$background_color = imagecolorallocate ($im, 50, 50, 50);
+$color = imagecolorallocate ($im, 255,255,255);
+
+$ylist = @array(400);
+$current = 0;
+$d = 1;
+for($j = 0; $j < 400; $j++) {
+
+ if($current>10) {
+ $d = -1;
+ } else if($current <-10) {
+ $d = 1;
+ } else if(mt_rand(0, 400) < 200) {
+ $d = -$d;
+ }
+ if(mt_rand(0, 400) < 350) {
+ $current = $current + $d;
+ }
+
+ $ylist[$j] = $current;
+}
+
+function imagecharx($img, $char, $x0, $y0,$ylist)
+{
+ $da = @imagecreate (10, 20) or die ("Cannot Initialize new GD image stream");
+ $background_color = imagecolorallocate ($da, 50, 50, 50);
+ $text_color = imagecolorallocate ($da, 255,255,255);
+ $color = imagecolorallocate ($img, 255,255,255);
+ $arg = rand(0,7)/100.0 * pi();
+ imagestring($da, 10, 0, 0, $char, $text_color);
+ for($i = 0; $i < 200; $i++) {
+ $y = @floor($i/10);
+ $x = $i%10;
+ $point_color = imagecolorat($da,$x,$y);
+
+ if($point_color == $text_color){
+ for($j = 0; $j < 7; $j++) {
+ $dx = 0; $dy = 0;
+ $p = 6;
+ for($s = 0; $s < $p; $s++) {
+ $dx += rand(0, 1000/$p)/100;
+ $dy += rand(0, 1000/$p)/100;
+ }
+ $xx = $x*5+$dx - 25;
+ $yy = $y*5+$dy - 50;
+
+ $x1 = cos($arg)*$xx - sin($arg)*$yy + 25;
+ $y1 = sin($arg)*$xx + cos($arg)*$yy + 50;
+
+ imagesetpixel($img,$x0+$x1,$y0+$y1,$color);
+ }
+ }
+ }
+ imagedestroy($da);
+}
+for($j = 0; $j < 800; $j++) {
+ $rx = mt_rand(0, 400);
+ $ry = mt_rand(0, 100);
+ imagesetpixel($im,$rx,$ry,$color);
+}
+for ( $i=0; $i < 7; $i++ ){
+ imagecharx($im,substr($code,$i,1),$i*43+25,0,$ylist);
+}
+
+$current = 0;
+$d = 1;
+for($j = 0; $j < 400; $j+=10) {
+
+ if($current>30) {
+ $d = -1;
+ } else if($current <-30) {
+ $d = 1;
+ } else if(mt_rand(0, 400) < 70) {
+ $d = -$d;
+ }
+ if(mt_rand(0, 400) < 380) {
+ $current = $current + $d;
+ }
+
+ for($l = 0; $l < 10; $l++) {
+ $dx = 0; $dy = 0;
+ $p = 2;
+ for($s = 0; $s < $p; $s++) {
+ $dx += rand(0, 1000/$p)/100;
+ $dy += rand(0, 1000/$p)/100;
+ }
+
+ imagesetpixel($im,$j+$dy,40+$dx+$current,$color);
+ }
+}
+
+
+imagepng ($im);
+imagedestroy ($im);
+?>
115 datauricaptcha.php
@@ -0,0 +1,115 @@
+<?php
+
+
+ function getCaptcha() {
+ $str = "1,3,4,6,7,9,A,C,D,E,F,H,K,M,N,P,Q,R,T,W,X,Y";
+ $list = explode(",", $str);
+ $cmax = count($list) - 1;
+ $code = '';
+ for ( $i=0; $i < 7; $i++ ){
+ $randnum = mt_rand(0, $cmax);
+ $code .= $list[$randnum];
+ }
+
+ ob_start();
+ $im = @imagecreate (350, 90) or die ("Cannot Initialize new GD image stream");
+ $background_color = imagecolorallocate ($im, 0, 0, 0);
+ $color = imagecolorallocate ($im, 255,255,255);
+
+ $ylist = @array(400);
+ $current = 0;
+ $d = 1;
+ for($j = 0; $j < 400; $j++) {
+
+ if($current>10) {
+ $d = -1;
+ } else if($current <-10) {
+ $d = 1;
+ } else if(mt_rand(0, 400) < 200) {
+ $d = -$d;
+ }
+ if(mt_rand(0, 400) < 350) {
+ $current = $current + $d;
+ }
+
+ $ylist[$j] = $current;
+ }
+
+ function imagecharx($img, $char, $x0, $y0,$ylist)
+ {
+ $da = @imagecreate (10, 20) or die ("Cannot Initialize new GD image stream");
+ $background_color = imagecolorallocate ($da, 50, 50, 50);
+ $text_color = imagecolorallocate ($da, 255,255,255);
+ $color = imagecolorallocate ($img, 255,255,255);
+ $arg = rand(0,7)/100.0 * pi();
+ imagestring($da, 10, 0, 0, $char, $text_color);
+ for($i = 0; $i < 200; $i++) {
+ $y = @floor($i/10);
+ $x = $i%10;
+ $point_color = imagecolorat($da,$x,$y);
+
+ if($point_color == $text_color){
+ for($j = 0; $j < 7; $j++) {
+ $dx = 0; $dy = 0;
+ $p = 6;
+ for($s = 0; $s < $p; $s++) {
+ $dx += rand(0, 1000/$p)/100;
+ $dy += rand(0, 1000/$p)/100;
+ }
+ $xx = $x*5+$dx - 25;
+ $yy = $y*5+$dy - 50;
+
+ $x1 = cos($arg)*$xx - sin($arg)*$yy + 25;
+ $y1 = sin($arg)*$xx + cos($arg)*$yy + 50;
+
+ imagesetpixel($img,$x0+$x1,$y0+$y1,$color);
+ }
+ }
+ }
+ imagedestroy($da);
+ }
+ for($j = 0; $j < 800; $j++) {
+ $rx = mt_rand(0, 400);
+ $ry = mt_rand(0, 100);
+ imagesetpixel($im,$rx,$ry,$color);
+ }
+ for ( $i=0; $i < 7; $i++ ){
+ imagecharx($im,substr($code,$i,1),$i*43+25,0,$ylist);
+ }
+
+ $current = 0;
+ $d = 1;
+ for($j = 0; $j < 400; $j+=10) {
+
+ if($current>30) {
+ $d = -1;
+ } else if($current <-30) {
+ $d = 1;
+ } else if(mt_rand(0, 400) < 70) {
+ $d = -$d;
+ }
+ if(mt_rand(0, 400) < 380) {
+ $current = $current + $d;
+ }
+
+ for($l = 0; $l < 10; $l++) {
+ $dx = 0; $dy = 0;
+ $p = 2;
+ for($s = 0; $s < $p; $s++) {
+ $dx += rand(0, 1000/$p)/100;
+ $dy += rand(0, 1000/$p)/100;
+ }
+
+ imagesetpixel($im,$j+$dy,40+$dx+$current,$color);
+ }
+ }
+
+
+ imagepng ($im);
+ imagedestroy ($im);
+ $gifdata = ob_get_contents();
+
+ ob_end_clean();
+
+ return array( 'question' => 'data:image/gif;base64,'.base64_encode($gifdata), 'answer' => $code );
+ }

0 comments on commit 387965e

Please sign in to comment.
Something went wrong with that request. Please try again.