A very useful verification code tool class

WBOY
Release: 2016-07-25 08:47:39
Original
1129 people have browsed it
  1. /**
  2. * Verification code class
  3. */
  4. class Base_Tool_Verify{
  5. /**
  6. * How to generate verification code
  7. */
  8. public static function createCode(){
  9. //Generate verification code image
  10. // All numbers
  11. $str = "D,B,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,W,J,K,M,M ,N,K,P,Q,R,S,T,U,V,W,X,Y,Z"; //The characters to be displayed can be added or deleted by yourself
  12. $list = explode(",", $ str);
  13. $cmax = count($list) - 1;
  14. $verifyCode = '';
  15. for ( $i=0; $i < 4; $i++ ){
  16. $randnum = mt_rand(0, $cmax );
  17. $verifyCode .= $list[$randnum]; //Take out the characters and combine them into the verification code characters we want
  18. }
  19. $_SESSION['code'] = $verifyCode; //Put the characters into SESSION
  20. $im = imagecreate(80,28); //Generate an image
  21. $black = imagecolorallocate($im, 0,0,0); //This and the following three items are the set colors
  22. $white = imagecolorallocate($im , 244,249,244);
  23. $gray = imagecolorallocate($im, rand(200,255),rand(200,255),rand(200,255));
  24. $red = imagecolorallocate($im, rand(200,255), rand(200,255), rand( 200,255));
  25. $rand_color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
  26. $rand_color2 = imagecolorallocate($im, rand(0,255), rand(0,255), rand( 0,255));
  27. imagefill($im,0,0,$white); //Fill the image with color
  28. $pix=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  29. mt_srand();
  30. for($i=0;$i<100;$i++)
  31. {
  32. imagesetpixel($im,mt_rand(0,180),mt_rand(0,35),$pix);
  33. }
  34. imageline($ im, 0,rand(0,28), 80, rand(0,28), $rand_color);
  35. imageline($im, 0,rand(0,28), 80, rand(0,28), $rand_color2 );
  36. //Draw the verification code into the picture
  37. $mycolor = imagecolorallocate($im, 0, 78, 152);
  38. $path = API_PATH.DS.'Base'.DS.'Tool';
  39. putenv(' GDFONTPATH=' . $path);
  40. $font = 'simhei.ttf';
  41. $arrstr = str_split($verifyCode);
  42. imagettftext($im, 20, rand(0,50), 10, rand(20,30 ), $mycolor, $font, $arrstr[0]);
  43. imagettftext($im, 15, rand(0,50), 20, rand(20,30), $mycolor, $font, $arrstr[1] );
  44. imagettftext($im, 15, rand(0,50), 30, rand(20,30), $mycolor, $font, $arrstr[2]);
  45. imagettftext($im, 15, rand(0 ,50), 40, rand(20,30), $mycolor, $font, $arrstr[3]);
  46. $font2=imagecolorallocate($im,41,163,238);
  47. imagerectangle($im,0,0,1, 1,$font2);
  48. imagepng($im);
  49. imagedestroy($im);
  50. }
  51. }
Copy code


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