一个php验证码的封装类

php中文网
发布: 2016-07-25 08:58:47
原创
1221人浏览过
  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. //通过调用该方法获取随机创建的验证码字符串

    AI封面生成器
    AI封面生成器

    专业的AI封面生成工具,支持小红书、公众号、小说、红包、视频封面等多种类型,一键生成高质量封面图片。

    AI封面生成器 108
    查看详情 AI封面生成器
  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. ?>

    立即学习PHP免费学习笔记(深入)”;

复制代码

验证码类的调用示例:

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


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号