当前位置:Gxlcms > PHP教程 > 一个php验证码的封装类

一个php验证码的封装类

时间:2021-07-01 10:21:17 帮助过:29人阅读

  1. /**

  2. * 验证码类
  3. * edit bbs.it-home.org
  4. */
  5. class ValidationCode {
  6. private $width;
  7. private $height;
  8. private $codeNum;
  9. private $image; //图像资源
  10. private $disturbColorNum;
  11. private $checkCode;
  12. function __construct($width=80, $height=20, $codeNum=4){
  13. $this->width=$width;
  14. $this->height=$height;
  15. $this->codeNum=$codeNum;
  16. $this->checkCode=$this->createCheckCode();
  17. $number=floor($width*$height/15);

  18. if($number > 240-$codeNum){

  19. $this->disturbColorNum= 240-$codeNum;
  20. }else{
  21. $this->disturbColorNum=$number;
  22. }

  23. }

  24. //通过访问该方法向浏览器中输出图像
  25. function showImage($fontFace=""){
  26. //第一步:创建图像背景
  27. $this->createImage();
  28. //第二步:设置干扰元素
  29. $this->setDisturbColor();
  30. //第三步:向图像中随机画出文本
  31. $this->outputText($fontFace);
  32. //第四步:输出图像
  33. $this->outputImage();
  34. }

  35. //通过调用该方法获取随机创建的验证码字符串

  36. function getCheckCode(){
  37. return $this->checkCode;
  38. }
  39. private function createImage(){
  40. //创建图像资源 //bbs.it-home.org
  41. $this->image=imagecreatetruecolor($this->width, $this->height);
  42. //随机背景色
  43. $backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
  44. //为背景添充颜色
  45. imagefill($this->image, 0, 0, $backColor);
  46. //设置边框颜色
  47. $border=imagecolorallocate($this->image, 0, 0, 0);
  48. //画出矩形边框
  49. imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
  50. }
  51. private function setDisturbColor(){
  52. for($i=0; $i<$this->disturbColorNum; $i++){
  53. $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  54. imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
  55. }
  56. for($i=0; $i<10; $i++){
  57. $color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
  58. imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
  59. }
  60. }
  61. private function createCheckCode(){
  62. //这里主要产生随机码,从2开始是为了区分1和l
  63. $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  64. $string='';
  65. for($i=0; $i < $this->codeNum; $i++){
  66. $char=$code{rand(0, strlen($code)-1)};
  67. $string.=$char;
  68. }
  69. return $string;
  70. }
  71. private function outputText($fontFace=""){
  72. for($i=0; $i<$this->codeNum; $i++){
  73. $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
  74. if($fontFace==""){
  75. $fontsize=rand(3, 5);
  76. $x=floor($this->width/$this->codeNum)*$i+3;
  77. $y=rand(0, $this->height-15);
  78. imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
  79. }else{
  80. $fontsize=rand(12, 16);
  81. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  82. $y=rand($fontSize+5, $this->height);
  83. imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
  84. }
  85. }
  86. }

  87. //输出图象信息

  88. private function outputImage() {
  89. if(imagetypes() & IMG_GIF){
  90. header("Content-Type:image/gif");
  91. imagepng($this->image);
  92. }else if(imagetypes() & IMG_JPG){
  93. header("Content-Type:image/jpeg");
  94. imagepng($this->image); //bbs.it-home.org
  95. }else if(imagetypes() & IMG_PNG){
  96. header("Content-Type:image/png");
  97. imagepng($this->image);
  98. }else if(imagetypes() & IMG_WBMP){
  99. header("Content-Type:image/vnd.wap.wbmp");
  100. imagepng($this->image);
  101. }else{
  102. die("PHP不支持图像创建");
  103. }
  104. }
  105. function __destruct(){
  106. imagedestroy($this->image);
  107. }
  108. }
  109. ?>

验证码类的调用示例:

  1. session_start();
  2. include "validationcode.class.php";
  3. $code=new ValidationCode(80, 20, 4);
  4. $code->showImage(); //输出到页面中供 注册或登录使用
  5. $_SESSION["code"]=$code->getCheckCode(); //将验证码保存到服务器中
  6. ?>

人气教程排行