MympsVerify.fun.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. function GetMympsVerify($type = 'engber', $noise = '12', $line = '1', $distort = '6', $incline = '30', $return = true, $close = '0', $number = '4')
  3. {
  4. global $timestamp;
  5. if ($type == 'plus') {
  6. $str1 = GetMympsRand('number', GetRand(array(1, 2)));
  7. $str2 = GetMympsRand('number', 1);
  8. $text = $str1 . '+' . $str2 . '=';
  9. }
  10. else {
  11. $text = GetMympsRand($type, $number);
  12. }
  13. $time = $timestamp + 600;
  14. $im_x = 195;
  15. $im_y = 52;
  16. $im = imagecreatetruecolor($im_x, $im_y);
  17. $text_c = ImageColorAllocate($im, mt_rand(1, 120), mt_rand(1, 120), mt_rand(1, 120));
  18. $tmpC0 = mt_rand(200, 255);
  19. $tmpC1 = mt_rand(230, 255);
  20. $tmpC2 = mt_rand(240, 255);
  21. $nx = 0;
  22. switch ($close) {
  23. case '0':
  24. $nxx = '1.7';
  25. break;
  26. case '1':
  27. $nxx = '1.6';
  28. break;
  29. case '2':
  30. $nxx = '1.5';
  31. break;
  32. case '3':
  33. $nxx = '1.4';
  34. break;
  35. case '4':
  36. $nxx = '1.3';
  37. break;
  38. case '5':
  39. $nxx = '1.2';
  40. break;
  41. case '6':
  42. $nxx = '1.1';
  43. break;
  44. case '7':
  45. $nxx = '1';
  46. break;
  47. case '8':
  48. $nxx = '0.9';
  49. break;
  50. }
  51. $buttum_c = ImageColorAllocate($im, $tmpC0, $tmpC1, $tmpC2);
  52. imagefill($im, 16, 13, $buttum_c);
  53. $font = MYMPS_DATA . '/ttf/mymps' . mt_rand(1, 6) . '.ttf';
  54. if ($noise) {
  55. for ($i = 0; $i < $noise; $i++) {
  56. $noiseColor = imagecolorallocate($im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
  57. for ($j = 0; $j < 3; $j++) {
  58. imagestring($im, 5, mt_rand(-10, $im_x), mt_rand(-10, $im_y), GetMympsRand('engber', 1), $noiseColor);
  59. }
  60. }
  61. }
  62. for ($i = 0; $i < strlen($text); $i++) {
  63. $tmp = substr($text, $i, 1);
  64. $array = array(-1, 1);
  65. $p = array_rand($array);
  66. $an = $array[$p] * mt_rand(0 - $incline, $incline);
  67. $size = 26;
  68. imagettftext($im, $size, $an, $nx, $size * 1.5, $text_c, $font, $tmp);
  69. $nx += mt_rand($size * $nxx, $size * $nxx);
  70. }
  71. $distortion_im = imagecreatetruecolor($im_x, $im_y);
  72. imagefill($distortion_im, 16, 13, $buttum_c);
  73. for ($i = 0; $i < $im_x; $i++) {
  74. for ($j = 0; $j < $im_y; $j++) {
  75. $rgb = imagecolorat($im, $i, $j);
  76. if ((int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) <= imagesx($distortion_im) && 0 <= (int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10)) {
  77. imagesetpixel($distortion_im, (int) ($i + 10 + sin($j / $im_y * 2.5 * M_PI - M_PI * 0.10000000000000001) * $distort), $j, $rgb);
  78. }
  79. }
  80. }
  81. if ($line) {
  82. for ($i = 0; $i < $line; $i++) {
  83. $rand = mt_rand(7, 38);
  84. $rand1 = mt_rand(15, 25);
  85. $rand2 = mt_rand(5, 10);
  86. for ($yy = $rand; $yy <= 0 + $rand + 2; $yy++) {
  87. for ($px = -100; $px <= 95; $px = $px + 0.10000000000000001) {
  88. $x = $px / $rand1;
  89. if ($x != 0) {
  90. $y = sin($x);
  91. }
  92. $py = $y * $rand2;
  93. imagesetpixel($distortion_im, $px + 100, $py + $yy, $text_c);
  94. }
  95. }
  96. }
  97. }
  98. Header('Content-type: image/JPEG');
  99. ImagePNG($distortion_im);
  100. ImageDestroy($distortion_im);
  101. ImageDestroy($im);
  102. $text = $type == 'plus' ? $str1 + $str2 : $text;
  103. if ($return) {
  104. return array('code' => $text, 'time' => $time);
  105. }
  106. }
  107. function GetMympsRand($type = 'engber', $number = 4)
  108. {
  109. $hash = '';
  110. if ($type == 'number') {
  111. $chars = '456789';
  112. }
  113. else if ($type == 'english') {
  114. $chars = 'abcdefghjklmnpqrstuvwxy';
  115. }
  116. else if ($type == 'engber') {
  117. $chars = '3456789abcdefghjklmnpqrstuvwxy';
  118. }
  119. $max = strlen($chars) - 1;
  120. mt_srand((double) microtime() * 1000000);
  121. for ($i = 0; $i < $number; $i++) {
  122. $hash .= $chars[mt_rand(0, $max)];
  123. }
  124. $max = $chars = $type = NULL;
  125. $hash = strtoupper($hash);
  126. return $hash;
  127. }
  128. function GetRand($nxarray)
  129. {
  130. $nxarray = $nxarray ? $nxarray : array(0, 5, 10);
  131. $nxarr = rand(0, count($nxarray) - 1);
  132. return $nxarray[$nxarr];
  133. }
  134. ?>