PHP image verification code creation example

Source: Internet
Author: User
Tags imagejpeg random seed
PHP image verification code creation example

  1. $ Authnum = '';
  2. $ Ychar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
  3. $ List = explode (",", $ ychar); // delimiter function
  4. For ($ I = 0; $ I <4; $ I ++ ){
  5. $ Randnum = rand (0, 35 );
  6. $ Authnum. = $ list [$ randnum]; // output as an array

Method 2: define a private function.

  1. Private function createCheckCode ()
  2. {
  3. For (I = 0; I CodeNum; I ++)
  4. {
  5. Number = rand (0, 2 );
  6. Switch (number)
  7. {
  8. Case 0: rand_number = rand (48, 57); break; // number
  9. Case 1: rand_number = rand (); break; // uppercase letter
  10. Case 2: rand_number = rand (97,122); break; // lowercase letter
  11. }
  12. $ Asc = sprintf ("% c", rand_number );
  13. $ Asc_number = asc_number.asc;
  14. }
  15. Return asc_number;
  16. }

Method 3: use a random seed to generate a php verification code.

  1. Srand (microtime () * 100000); // equivalent to a timer
  2. $ String = "abcdefghigklmnopqrstuvwxyz123456789 ";
  3. For ($ I = 0; $ I <4; $ I ++)
  4. {
  5. $ New_number. = $ string [rand (0, strlen ($ string)-1)]; // An array is generated immediately.
  6. }

Method 4,

  1. For ($ I = 0; $ I <4; $ I ++)
  2. {
  3. $ Rand. = dechex (rand (); // Convert decimal to hexadecimal
  4. }

Next I will go to the focus of this article: PHP GD Library: (provides IPI for a series of image processing functions to generate image processing images) enable the GD Library in php: php. in the ini configuration file, remove ";" from "; extension = php_gd2.dll". some GD library functions: 1. imagecreatetruecolor (int x_size, int Y_size) create a true color image 2. imagecolorallocate (resource image, int red, int green, int blue) allocates color to an image with three primary colors. imagestring (resource, font, int x, int y, content, color) plotting function 4. header ("Content-type: image/jpeg") the output function php header defines the header action. in php5, the following types are supported: 1, Content-type: xxxx/yyyy 2, Location: xxxx: yyyy/zzzz 3, Status: nnn xxxxxx xxxx/yyyy indicates the type of the content file, for example, image/gif image/jpeg image/png imagejpeg (), imagegif (), imagepang () 5. iamgeline (resource image, int x1, int y1, int x2, int y2, int color); draw line function, (int x, int y) start coordinate 6. imagesetpixel (resource image, int x, int y, int color) Function 7. imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) function 8. iconv ("gb 2312 "," UTF-8 "," string "); // you must first convert the text into a UTF-8 format php verification code to insert a Chinese character. here, you can refer to the php Chinese verification code generation method.

Example 1: code for randomly generating numbers and letters:

  1. // Che. php
  2. Session_start ();
  3. For ($ I = 0; $ I <4; $ I ++)
  4. {
  5. $ Rand. = dechex (rand (1, 15 ));
  6. }
  7. $ _ SESSION ['Check _ num'] = $ rand;
  8. $ Image = imagecreatetruecolor (50, 30 );
  9. $ Bg = imagecolorallocate ($ im, 0); // specifies the background color when the palette is used for the first time.
  10. $ Te = imagecolorallocate ($ im, 255,255,255 );
  11. Imagestring ($ image, 6, rand (0, 20), rand (0, 2), $ rand, $ te );
  12. Ob_clean (); // The Image "http: // localhost/**. PHP" is displayed on the php webpage because the verification code is generated.
  13. Header ("Content-type: image/jpeg"); imagejpeg ($ image );
  14. ?>

Example 2: draw the interference line code for the image:

  1. For ($ I = 0; $ I <8; $ I ++) // draw multiple lines
  2. {
  3. $ Cg = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // Generate random colors
  4. Imageline ($ im, rand (10, 40), 0, rand (10, 40), 20, $ cg );
  5. }

Example 3: draw the interference point code for the image:

  1. For ($ I = 0; $ I <80; $ I ++) // draw multiple points
  2. {
  3. Imagesetpixel ($ im, rand (0, 40), rand (0, 20), $ cg );
  4. }

Example 4: write the text into the image code:

  1. $ Str = array ('my', 'my', 'login', 'login'); // store the displayed Chinese characters

  2. For ($ I = 0; $ I <4; $ I ++)
  3. {
  4. $ Sss. = $ str [rand ()]; // randomly display Chinese characters
  5. }

  6. // $ Str = iconv ("gb2312", "UTF-8", $ str); // Chinese character encoding conversion, which does not seem to require me

  7. Imagettftext ($ im, 10, 0, rand (5, 60), rand (5, 60), $ te, "simhei. ttf", $ sss );//

0: font skew. "simhei. ttf": font style, which is generally placed under the root directory;

This completes the entire php image verification code production process. the script editor hopes that this article will help you to generate a php verification code using the gd Library.

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.