php 生成验证码程序

  1. <?php
  2. $consts = 'bcdfghjkmnpqrstwxyz';
  3. $vowels = 'aei23456789';
  4. for ($x = 0; $x < 6; $x++)
  5. {
  6. $const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
  7. $vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
  8. }
  9. $radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
  10. $_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
  11. / t up image, the first number is the width and the second is the height
  12. $imageX = strlen($radomstring)*5.5; //the image width
  13. $imageY = 20; //the image height
  14. $im = imagecreatetruecolor($imageX,$imageY);
  15. //creates two variables to store color
  16. $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
  17. $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
  18. imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
  19. imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
  20. imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
  21. $foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);
  22. $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
  23. $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);
  24. //fill image with bgcolor
  25. imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
  26. //writes string
  27. imagestring($im, 5, 3, floor(rand(0,5))-1, $string[0], $foregroundArr[rand(0,3)]);
  28. imagestring($im, 5, 13, floor(rand(0,5))-1, $string[1], $foregroundArr[rand(0,3)]);
  29. imagestring($im, 5, 23, floor(rand(0,5))-1, $string[2], $foregroundArr[rand(0,3)]);
  30. imagestring($im, 5, 33, floor(rand(0,5))-1, $string[3], $foregroundArr[rand(0,3)]);
  31. for($i=0; $i<strlen($string); $i++)
  32. {
  33. if(mt_rand(0,ord(substr($string,$i,1)))%2 == 0)
  34. {
  35. $string{$i} = ' ';
  36. }
  37. }
  38. imagestring($im, 2, 2, 0, $string, $foreground2);
  39. //strikethrough
  40. $border = imagecolorallocate($im, 133, 153, 193);
  41. //imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
  42. imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
  43. $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
  44. for ($i=0;$i<20;$i++)
  45. {
  46. imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
  47. }
  48. //rotate
  49. $middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
  50. //random shapes
  51. for ($x=0; $x<15;$x++)
  52. {
  53. if(mt_rand(0,$x)%2==0)
  54. {
  55. imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
  56. imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
  57. }
  58. else
  59. {
  60. imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
  61. imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
  62. }
  63. }//开源代码phpfensi.com
  64. //output to browser
  65. header("content-type:image/png\r\n");
  66. imagepng($im);
  67. imagedestroy($im);
  68. ?>