php两款生成图形验证码的代码

验证码在php中是经常会用到了,下面我们为你提供了一些关于php验证码生成类与程序,直接复制下去就可以直接用的,下面看代码.

  1. <?php
  2. session_start();
  3. Header("Content-type: image/gif");
  4. class SecurityCode
  5. {
  6. private $codes = '';
  7. function __construct()
  8. {
  9. $code = '0-1-2-3-4-5-6-7-8-9-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';
  10. $codeArray = explode('-',$code);
  11. shuffle($codeArray);
  12. $this->codes = implode('',array_slice($codeArray,0,4));
  13. }
  14. public function CreateImg()
  15. {
  16. $_SESSION['check_pic'] = $this->codes;
  17. $img = imagecreate(70,25);
  18. imagecolorallocate($img,222,222,222);
  19. $testcolor1 = imagecolorallocate($img,255,0,0);
  20. $testcolor2 = imagecolorallocate($img,51,51,51);
  21. $testcolor3 = imagecolorallocate($img,0,0,255);
  22. $testcolor4 = imagecolorallocate($img,255,0,255);
  23. for ($i = 0; $i < 4; $i++)
  24. {
  25. imagestring($img,rand(5,6),8 + $i * 15,rand(2,8),$this->codes[$i],rand(1,4));
  26. }
  27. imagegif($img);
  28. }
  29. }
  30. $code = new SecurityCode();
  31. $code->CreateImg();
  32. $code = NULL;
  33. ?>
  34. //代码二
  35. <?
  36. session_start();
  37. for($i=0; $i<4; $i++){
  38. $rand.= dechex(rand(1,15));
  39. } //开源代码phpfensi.com
  40. $_SESSION[check_pic]=$rand;
  41. //echo $_SESSION[check_pic];
  42. // 设置图片大小">图片大小
  43. $im = imagecreatetruecolor(100,30);
  44. // 设置颜色
  45. $bg=imagecolorallocate($im,0,0,0);
  46. $te=imagecolorallocate($im,255,255,255);
  47. // 把字符串写在图像左上角
  48. imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
  49. // 输出图像
  50. header("Content-type:image/jpeg");
  51. imagejpeg($im);
  52. ?>