PHP generates random code for a segment

Source: Internet
Author: User
  1. /*

  2. * Description: Generate random codes and display random codes graphically.
  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. Functions for generating digital images
  26. function Draw2digits ($x, $y, $number)
  27. {
  28. Draw_digit ($x, $y, (int) ($number/10));
  29. Draw_digit ($x + one, $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. $x 0 = $lines [$j] * $m + $x;
  41. $y 0 = $lines [$j + 1] * $m + $y;
  42. $x 1 = $lines [$j + 2] * $m + $x;
  43. $y 1 = $lines [$j + 3] * $m + $y;
  44. if ($x 0 = = $x 1) {
  45. $ofs = 3 * ($SX * $y 0 + $x 0);
  46. for ($h = $y 0; $h <= $y 1, $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 * $y 0 + $x 0);
  53. for ($w = $x 0; $w <= $x 1; $w + +) {
  54. $pixels [$ofs + +] = chr (0);
  55. $pixels [$ofs + +] = chr (0);
  56. $pixels [$ofs + +] = chr (0);
  57. }
  58. }
  59. }
  60. }
  61. }

  62. Add text to an image

  63. function Add_chunk ($type)
  64. {
  65. Global $result, $data, $chunk, $CRC _table;

  66. Chunk: For layers

  67. Length:4 bytes: Used to calculate chunk
  68. Chunk Type:4 bytes
  69. Chunk Data:length bytes
  70. Crc:4 bytes: Cyclic Redundancy code Check

  71. Copy data and create CRC checksum

  72. $len = strlen ($data);
  73. $chunk = Pack ("c*", ($len >>) & 255,
  74. ($len >>) & 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. $c 8 = ($c >> 8) & 0xFFFFFF;
  85. $c = $CRC _table[($c ^ ord ($chunk [$n])) & 0xFF] ^ $c 8;
  86. }
  87. $CRC = $c ^ $z;

  88. $chunk. = Chr (($CRC >>) & 255);

  89. $chunk. = Chr (($CRC >>) & 255);
  90. $chunk. = Chr (($CRC >> 8) & 255);
  91. $chunk. = Chr ($CRC & 255);

  92. Add results to $result

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

  95. Main program

  96. $SX = 55;

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

  99. Fill

  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– ($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 (2, $m);

  119. Set_4pixel (0, 0, 0, 26, 7);

  120. Set_4pixel (0, 0, 0, 26, 13);

  121. Create a cyclic redundancy check table

  122. $z =-306674912; = 0xedb88320
  123. for ($n = 0; $n < $n + +) {
  124. $c = $n;
  125. for ($k = 0; $k < 8; $k + +) {
  126. $c 2 = ($c >> 1) & 0x7fffffff;
  127. if ($c & 1) $c = $z ^ ($c 2); else $c = $c 2;
  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 >>) & 255,
  142. ($sx >>) & 255,
  143. ($SX >> 8) & 255,
  144. $SX & 255,
  145. ($sy >>) & 255,
  146. ($sy >>) & 255,
  147. ($sy >> 8) & 255,
  148. $sy & 255,
  149. 8,
  150. 2,
  151. 0,
  152. 0,
  153. 0);
  154. Add_chunk ("IHDR");

  155. The following do not dare to translate, please refer yourself

  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*", 0x78, 0x01,
  173. 1,
  174. $len & 255,
  175. ($len >> 8) & 255,
  176. 255– ($len & 255),
  177. 255– (($len >> 8) & 255));
  178. $start = strlen ($data);
  179. $i 2 = 0;
  180. for ($h = 0; $h < $sy; $h + +) {
  181. $data. = chr (0);
  182. for ($w = 0; $w < $SX * 3; $w + +) {
  183. $data. = $pixels [$i 2++];
  184. }
  185. }

  186. Calculate a Adler32 checksum with the bytes Data[start. LEN-1]

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

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

  195. $data. = Chr (($adler >>) & 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. Print image

  203. Echo ($result);

  204. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.