Php generates a random code segment

Source: Internet
Author: User
Php generates a random code segment

  1. /*

  2. * Note: generate a random code and display the random code 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. // Function for generating digital images
  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. // Add text to the image

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

  66. // Chunk: Layer

  67. // Length: 4 bytes: used to calculate chunk
  68. // Chunk type: 4 bytes
  69. // Chunk data: length bytes
  70. // CRC: 4 bytes: cyclic verification

  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) & 0 xffffff;
  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. // Add the result to $ result

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

  95. // Main program

  96. $ Sx = 55;

  97. $ Sy = 21;
  98. $ Pixels = rand (, 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-(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. // Create a cyclic verification table

  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 );

  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. // Do not translate the text below. please refer to it 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, 32 k 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. $ 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. // Print the image

  203. Echo ($ result );

  204. ?>

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.