PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/captcha.php

https://gitlab.com/mba811/upyun-downloader
PHP | 106 lines | 84 code | 12 blank | 10 comment | 13 complexity | cc5ba13f090359e7d814fd063e61060c MD5 | raw file
  1. <?php
  2. function getAuthImage($text) {
  3. $im_x = 160;
  4. $im_y = 40;
  5. $im = imagecreatetruecolor($im_x,$im_y);
  6. $text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  7. $tmpC0=255;//mt_rand(100,255);
  8. $tmpC1=255;//mt_rand(100,255);
  9. $tmpC2=255;//mt_rand(100,255);
  10. $buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
  11. imagefill($im, 16, 13, $buttum_c);
  12. $font = 'font.ttf';
  13. for ($i=0;$i<strlen($text);$i++)
  14. {
  15. $tmp =substr($text,$i,1);
  16. $array = array(-1,1);
  17. $p = array_rand($array);
  18. $an = $array[$p]*mt_rand(1,10);//角度
  19. $size = 28;
  20. imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
  21. }
  22. $distortion_im = imagecreatetruecolor ($im_x, $im_y);
  23. imagefill($distortion_im, 16, 13, $buttum_c);
  24. for ( $i=0; $i<$im_x; $i++) {
  25. for ( $j=0; $j<$im_y; $j++) {
  26. $rgb = imagecolorat($im, $i , $j);
  27. if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
  28. imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
  29. }
  30. }
  31. }
  32. //加入干扰象素;
  33. $count = 160;//干扰像素的数量
  34. for($i=0; $i<$count; $i++){
  35. $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  36. imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
  37. }
  38. $rand = mt_rand(5,30);
  39. $rand1 = mt_rand(15,25);
  40. $rand2 = mt_rand(5,10);
  41. for ($yy=$rand; $yy<=+$rand+2; $yy++){
  42. for ($px=-80;$px<=80;$px=$px+0.1)
  43. {
  44. $x=$px/$rand1;
  45. if ($x!=0)
  46. {
  47. $y=sin($x);
  48. }
  49. $py=$y*$rand2;
  50. imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
  51. }
  52. }
  53. //设置文件头;
  54. Header("Content-type: image/PNG");
  55. //以PNG格式将图像输出到浏览器或文件;
  56. ImagePNG($distortion_im);
  57. //销毁一图像,释放与image关联的内存;
  58. ImageDestroy($distortion_im);
  59. ImageDestroy($im);
  60. }
  61. function make_rand($type,$length){//验证码文字生成函数
  62. /* $type 参数说明
  63. 1 随机生成只包含大写字母的字符串
  64. 2 随机生成只包含数字的字符串
  65. 3 随机生成包含大写字母和数字的字符串
  66. */
  67. if($type == 1)
  68. {
  69. $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  70. $len = 25;
  71. }
  72. elseif($type == 2)
  73. {
  74. $str = "0123456789";
  75. $len = 9;
  76. }
  77. elseif($type == 3)
  78. {
  79. $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  80. $len = 35;
  81. }
  82. $result="";
  83. for($i=0;$i<$length;$i++){
  84. $num[$i]=rand(0,$len);
  85. $result.=$str[$num[$i]];
  86. }
  87. return $result;
  88. }
  89. //输出调用
  90. $checkcode = make_rand(1,4);
  91. session_start();//将随机数存入session中
  92. $_SESSION['captcha']=strtolower($checkcode);
  93. getAuthImage($checkcode);
  94. ?>