Heim  >  Artikel  >  Backend-Entwicklung  >  一个php验证码的封装类

一个php验证码的封装类

WBOY
WBOYOriginal
2016-07-25 08:58:47969Durchsuche
  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; $idisturbColorNum; $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$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
  57. imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
  58. }
  59. }
  60. private function createCheckCode(){
  61. //这里主要产生随机码,从2开始是为了区分1和l
  62. $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  63. $string='';
  64. for($i=0; $i codeNum; $i++){
  65. $char=$code{rand(0, strlen($code)-1)};
  66. $string.=$char;
  67. }
  68. return $string;
  69. }
  70. private function outputText($fontFace=""){
  71. for($i=0; $icodeNum; $i++){
  72. $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
  73. if($fontFace==""){
  74. $fontsize=rand(3, 5);
  75. $x=floor($this->width/$this->codeNum)*$i+3;
  76. $y=rand(0, $this->height-15);
  77. imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
  78. }else{
  79. $fontsize=rand(12, 16);
  80. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  81. $y=rand($fontSize+5, $this->height);
  82. imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
  83. }
  84. }
  85. }
  86. //输出图象信息

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

验证码类的调用示例:

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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn