php生成随机码的一段代码

WBOY
Libérer: 2016-07-25 09:07:14
original
1246 Les gens l'ont consulté
  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 {
  37. if (($b & $digit) == $b) {
  38. $j = $i * 4;
  39. $x0 = $lines[$j] * $m + $x;
  40. $y0 = $lines[$j + 1] * $m + $y;
  41. $x1 = $lines[$j + 2] * $m + $x;
  42. $y1 = $lines[$j + 3] * $m + $y;
  43. if ($x0 == $x1) {
  44. $ofs = 3 * ($sx * $y0 + $x0);
  45. for ($h = $y0; $h $pixels[$ofs] = chr(0);
  46. $pixels[$ofs + 1] = chr(0);
  47. $pixels[$ofs + 2] = chr(0);
  48. }
  49. } else {
  50. $ofs = 3 * ($sx * $y0 + $x0);
  51. for ($w = $x0; $w $pixels[$ofs++] = chr(0);
  52. $pixels[$ofs++] = chr(0);
  53. $pixels[$ofs++] = chr(0);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. //将文字加入到图象中

  60. function add_chunk($type)
  61. {
  62. global $result, $data, $chunk, $crc_table;
  63. // chunk :为层

  64. // length: 4 字节: 用来计算 chunk
  65. // chunk type: 4 字节
  66. // chunk data: length bytes
  67. // CRC: 4 字节: 循环冗余码校验
  68. // copy data and create CRC checksum

  69. $len = strlen($data);
  70. $chunk = pack(“c*”, ($len >> 24) & 255,
  71. ($len >> 16) & 255,
  72. ($len >> 8) & 255,
  73. $len & 255);
  74. $chunk .= $type;
  75. $chunk .= $data;
  76. // calculate a CRC checksum with the bytes chunk[4..len-1]

  77. $z = 16777215;
  78. $z |= 255 $c = $z;
  79. for ($n = 4; $n $c8 = ($c >> 8) & 0xffffff;
  80. $c = $crc_table[($c ^ ord($chunk[$n])) & 0xff] ^ $c8;
  81. }
  82. $crc = $c ^ $z;
  83. $chunk .= chr(($crc >> 24) & 255);

  84. $chunk .= chr(($crc >> 16) & 255);
  85. $chunk .= chr(($crc >> 8) & 255);
  86. $chunk .= chr($crc & 255);
  87. // 将结果加到$result中

  88. $result .= $chunk;
  89. }
  90. //主程序

  91. $sx = 55;

  92. $sy = 21;
  93. $pixels = rand(100,99990)%2?”qwer”:”";
  94. // 填充

  95. for ($h = 0; $h {
  96. for ($w = 0; $w {
  97. $r = 100 / $sx * $w + 155;
  98. $g = 100 / $sy * $h + 155;
  99. $b = 255 – (100 / ($sx + $sy) * ($w + $h));
  100. $pixels .= chr($r);
  101. $pixels .= chr($g);
  102. $pixels .= chr($b);
  103. }
  104. }
  105. $checknum = isset($ViewRandomCode)?$ViewRandomCode:1234;

  106. $h = (int)($checknum/100);
  107. $m = (int)($checknum%100);
  108. $digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
  109. $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);
  110. draw2digits(4, 2, $h);

  111. draw2digits(30, 2, $m);
  112. // set_4pixel(0, 0, 0, 26, 7);

  113. // set_4pixel(0, 0, 0, 26, 13);
  114. // 创建循环冗余码校验表

  115. $z = -306674912; // = 0xedb88320
  116. for ($n = 0; $n $c = $n;
  117. for ($k = 0; $k $c2 = ($c >> 1) & 0×7fffffff;
  118. if ($c & 1) $c = $z ^ ($c2); else $c = $c2;
  119. }
  120. $crc_table[$n] = $c;
  121. }
  122. // PNG file signature

  123. $result = pack(“c*”, 137,80,78,71,13,10,26,10);
  124. // IHDR chunk data:

  125. // width: 4 bytes
  126. // height: 4 bytes
  127. // bit depth: 1 byte (8 bits per RGB value)
  128. // color type: 1 byte (2 = RGB)
  129. // compression method: 1 byte (0 = deflate/inflate)
  130. // filter method: 1 byte (0 = adaptive filtering)
  131. // interlace method: 1 byte (0 = no interlace)
  132. $data = pack(“c*”, ($sx >> 24) & 255,
  133. ($sx >> 16) & 255,
  134. ($sx >> 8) & 255,
  135. $sx & 255,
  136. ($sy >> 24) & 255,
  137. ($sy >> 16) & 255,
  138. ($sy >> 8) & 255,
  139. $sy & 255,
  140. 8,
  141. 2,
  142. 0,
  143. 0,
  144. 0);
  145. add_chunk(“IHDR”);
  146. // 以下不敢乱翻译,请自行参考

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

  176. $s1 = 1;
  177. $s2 = 0;
  178. for ($n = $start; $n $s1 = ($s1 + ord($data[$n])) % 65521;
  179. $s2 = ($s2 + $s1) % 65521;
  180. }
  181. $adler = ($s2
  182. $data .= chr(($adler >> 24) & 255);

  183. $data .= chr(($adler >> 16) & 255);
  184. $data .= chr(($adler >> 8) & 255);
  185. $data .= chr($adler & 255);
  186. add_chunk(“IDAT”);
  187. // IEND: marks the end of the PNG-file

  188. $data = “”;
  189. add_chunk(“IEND”);
  190. // 列印图象

  191. echo($result);

  192. ?>
复制代码


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal