当前位置:Gxlcms > PHP教程 > php生成随机码的一段代码

php生成随机码的一段代码

时间:2021-07-01 10:21:17 帮助过:17人阅读

  1. /*

  2. * 说明:生成随机码,并用图形方式显示随机码。
  3. */

  4. $ViewRandomCode = mt_rand(1000,10000);

  5. session_start();

  6. $_SESSION['checksum'] = $ViewRandomCode;

  7. function set_4pixel($r, $g, $b, $x, $y)

  8. {
  9. global $sx, $sy, $pixels;

  10. $ofs = 3 * ($sx * $y + $x);

  11. $pixels[$ofs] = chr($r);
  12. $pixels[$ofs + 1] = chr($g);
  13. $pixels[$ofs + 2] = chr($b);
  14. $pixels[$ofs + 3] = chr($r);
  15. $pixels[$ofs + 4] = chr($g);
  16. $pixels[$ofs + 5] = chr($b);
  17. $ofs += 3 * $sx;
  18. $pixels[$ofs] = chr($r);
  19. $pixels[$ofs + 1] = chr($g);
  20. $pixels[$ofs + 2] = chr($b);
  21. $pixels[$ofs + 3] = chr($r);
  22. $pixels[$ofs + 4] = chr($g);
  23. $pixels[$ofs + 5] = chr($b);
  24. }
  25. //生成数字图象的函数
  26. function draw2digits($x, $y, $number)
  27. {
  28. draw_digit($x, $y, (int) ($number / 10));
  29. draw_digit($x + 11, $y, $number % 10);
  30. }

  31. function draw_digit($x, $y, $digit)

  32. {
  33. global $sx, $sy, $pixels, $digits, $lines;

  34. $digit = $digits[$digit];

  35. $m = 8;
  36. for ($b = 1, $i = 0; $i < 7; $i++, $b *= 2)
  37. {
  38. if (($b & $digit) == $b) {
  39. $j = $i * 4;
  40. $x0 = $lines[$j] * $m + $x;
  41. $y0 = $lines[$j + 1] * $m + $y;
  42. $x1 = $lines[$j + 2] * $m + $x;
  43. $y1 = $lines[$j + 3] * $m + $y;
  44. if ($x0 == $x1) {
  45. $ofs = 3 * ($sx * $y0 + $x0);
  46. for ($h = $y0; $h <= $y1; $h++, $ofs += 3 * $sx) {
  47. $pixels[$ofs] = chr(0);
  48. $pixels[$ofs + 1] = chr(0);
  49. $pixels[$ofs + 2] = chr(0);
  50. }
  51. } else {
  52. $ofs = 3 * ($sx * $y0 + $x0);
  53. for ($w = $x0; $w <= $x1; $w++) {
  54. $pixels[$ofs++] = chr(0);
  55. $pixels[$ofs++] = chr(0);
  56. $pixels[$ofs++] = chr(0);
  57. }
  58. }
  59. }
  60. }
  61. }

  62. //将文字加入到图象中

  63. function add_chunk($type)
  64. {
  65. global $result, $data, $chunk, $crc_table;

  66. // chunk :为层

  67. // length: 4 字节: 用来计算 chunk
  68. // chunk type: 4 字节
  69. // chunk data: length bytes
  70. // CRC: 4 字节: 循环冗余码校验

  71. // copy data and create CRC checksum

  72. $len = strlen($data);
  73. $chunk = pack(“c*”, ($len >> 24) & 255,
  74. ($len >> 16) & 255,
  75. ($len >> 8) & 255,
  76. $len & 255);
  77. $chunk .= $type;
  78. $chunk .= $data;

  79. // calculate a CRC checksum with the bytes chunk[4..len-1]

  80. $z = 16777215;
  81. $z |= 255 << 24;
  82. $c = $z;
  83. for ($n = 4; $n < strlen($chunk); $n++) {
  84. $c8 = ($c >> 8) & 0xffffff;
  85. $c = $crc_table[($c ^ ord($chunk[$n])) & 0xff] ^ $c8;
  86. }
  87. $crc = $c ^ $z;

  88. $chunk .= chr(($crc >> 24) & 255);

  89. $chunk .= chr(($crc >> 16) & 255);
  90. $chunk .= chr(($crc >> 8) & 255);
  91. $chunk .= chr($crc & 255);

  92. // 将结果加到$result中

  93. $result .= $chunk;
  94. }

  95. //主程序

  96. $sx = 55;

  97. $sy = 21;
  98. $pixels = rand(100,99990)%2?”qwer”:”";

  99. // 填充

  100. for ($h = 0; $h < $sy; $h++)
  101. {
  102. for ($w = 0; $w < $sx; $w++)
  103. {
  104. $r = 100 / $sx * $w + 155;
  105. $g = 100 / $sy * $h + 155;
  106. $b = 255 – (100 / ($sx + $sy) * ($w + $h));
  107. $pixels .= chr($r);
  108. $pixels .= chr($g);
  109. $pixels .= chr($b);
  110. }
  111. }

  112. $checknum = isset($ViewRandomCode)?$ViewRandomCode:1234;

  113. $h = (int)($checknum/100);
  114. $m = (int)($checknum%100);
  115. $digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
  116. $lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);

  117. draw2digits(4, 2, $h);

  118. draw2digits(30, 2, $m);

  119. // set_4pixel(0, 0, 0, 26, 7);

  120. // set_4pixel(0, 0, 0, 26, 13);

  121. // 创建循环冗余码校验表

  122. $z = -306674912; // = 0xedb88320
  123. for ($n = 0; $n < 256; $n++) {
  124. $c = $n;
  125. for ($k = 0; $k < 8; $k++) {
  126. $c2 = ($c >> 1) & 0×7fffffff;
  127. if ($c & 1) $c = $z ^ ($c2); else $c = $c2;
  128. }
  129. $crc_table[$n] = $c;
  130. }

  131. // PNG file signature

  132. $result = pack(“c*”, 137,80,78,71,13,10,26,10);

  133. // IHDR chunk data:

  134. // width: 4 bytes
  135. // height: 4 bytes
  136. // bit depth: 1 byte (8 bits per RGB value)
  137. // color type: 1 byte (2 = RGB)
  138. // compression method: 1 byte (0 = deflate/inflate)
  139. // filter method: 1 byte (0 = adaptive filtering)
  140. // interlace method: 1 byte (0 = no interlace)
  141. $data = pack(“c*”, ($sx >> 24) & 255,
  142. ($sx >> 16) & 255,
  143. ($sx >> 8) & 255,
  144. $sx & 255,
  145. ($sy >> 24) & 255,
  146. ($sy >> 16) & 255,
  147. ($sy >> 8) & 255,
  148. $sy & 255,
  149. 8,
  150. 2,
  151. 0,
  152. 0,
  153. 0);
  154. add_chunk(“IHDR”);

  155. // 以下不敢乱翻译,请自行参考

  156. // scanline:
  157. // filter byte: 0 = none
  158. // RGB bytes for the line
  159. // the scanline is compressed with “zlib”, method 8 (RFC-1950):
  160. // compression method/flags code: 1 byte ($78 = method 8, 32k window)
  161. // additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)
  162. // compressed data blocks: n bytes
  163. // one block (RFC-1951):
  164. // bit 0: BFINAL: 1 for the last block
  165. // bit 1 and 2: BTYPE: 0 for no compression
  166. // next 2 bytes: LEN (LSB first)
  167. // next 2 bytes: one’s complement of LEN
  168. // LEN bytes uncompressed data
  169. // check value: 4 bytes (Adler-32 checksum of the uncompressed data)
  170. //
  171. $len = ($sx * 3 + 1) * $sy;
  172. $data = pack(“c*”, 0×78, 0×01,
  173. 1,
  174. $len & 255,
  175. ($len >> 8) & 255,
  176. 255 – ($len & 255),
  177. 255 – (($len >> 8) & 255));
  178. $start = strlen($data);
  179. $i2 = 0;
  180. for ($h = 0; $h < $sy; $h++) {
  181. $data .= chr(0);
  182. for ($w = 0; $w < $sx * 3; $w++) {
  183. $data .= $pixels[$i2++];
  184. }
  185. }

  186. // calculate a Adler32 checksum with the bytes data[start..len-1]

  187. $s1 = 1;
  188. $s2 = 0;
  189. for ($n = $start; $n < strlen($data); $n++) {
  190. $s1 = ($s1 + ord($data[$n])) % 65521;
  191. $s2 = ($s2 + $s1) % 65521;
  192. }
  193. $adler = ($s2 << 16) | $s1;

  194. $data .= chr(($adler >> 24) & 255);

  195. $data .= chr(($adler >> 16) & 255);
  196. $data .= chr(($adler >> 8) & 255);
  197. $data .= chr($adler & 255);
  198. add_chunk(“IDAT”);

  199. // IEND: marks the end of the PNG-file

  200. $data = “”;
  201. add_chunk(“IEND”);

  202. // 列印图象

  203. echo($result);

  204. ?>

人气教程排行