Home > Backend Development > PHP Tutorial > A piece of code for php to generate dynamic image verification code

A piece of code for php to generate dynamic image verification code

WBOY
Release: 2016-07-25 09:03:16
Original
849 people have browsed it
  1. session_start();
  2. function random($len)
  3. {
  4. $srCStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  5. $strs="";
  6. for($i=0;$i<$ len;$i++)
  7. {
  8. $strs.=$srCStr[mt_rand(0,35)];
  9. }
  10. return $strs;
  11. }
  12. $str=random(4); //Randomly generated string
  13. $ width = 50; //The width of the verification code image
  14. $height = 25; //The height of the verification code image
  15. @header("Content-Type:image/png");
  16. $_SESSION["code"] = $str ;
  17. $im = imagecreate($width,$height);
  18. //Background color
  19. $back = imagecolorallocate($im,0xFF,0xFF,0xFF);
  20. //Blurry point color
  21. $pix = imagecolorallocate($im, 187,230,247);
  22. //Font color
  23. $font = imagecolorallocate($im,41,163,238);
  24. //Points for blurring
  25. for($i=0;$i<1000;$i++)
  26. {
  27. imagesetpixel($ im,mt_rand(0,$width),mt_rand(0,$height),$pix);
  28. }
  29. imagestring($im, 5, 7, 5,$str, $font);
  30. imagerectangle($im,0 ,0,$width-1,$height-1,$font);
  31. imagepng($im);
  32. imagedestroy($im);
  33. $_SESSION["code"] = $str;
  34. ?>
Copy the code

Summary: Since PHP 4.2.0, you no longer need to use the srand() or mt_srand() function to seed the random number generator, it is now done automatically. Articles you may be interested in: php generates N non-repeating random numbers php randomly generates a 4-digit verification code php function to randomly display images php example of randomly displaying images Example of php generating random password PHP function to generate random username and password PHP program for batch generating random usernames Two ways to randomly generate Welfare Lottery numbers using php Function to generate random numbers using PHP Code to generate a random string using php Example of php generating random numbers php function to generate random string php random password generation function php function to generate random password PHP function to generate random string php function to generate random password Several ways to generate random passwords with php



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template