php 验证码类 php 验证码

原创
2016-07-25 09:04:21 738浏览
  1. /**
  2. * 通用验证码类 img.php
  3. * 版本:V0.1
  4. * bbs.it-home.org 2013/3/1
  5. */
  6. class ValidateCode {
  7. private $charset="abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //随机因子
  8. private $code; //验证码文字
  9. private $codelen=4; //验证码显示几个文字
  10. private $width=130; //验证码宽度
  11. private $height=50; //验证码高度
  12. private $img; //验证码资源句柄
  13. private $font; //指定的字体
  14. private $fontsize=20; //指定的字体大小
  15. private $fontcolor; //字体颜色 随机
  16. //构造类 编写字体
  17. public function __construct(){
  18. $this->font=ROOT_PATH.'/font/elephant.ttf';
  19. }
  20. //创建4个随机码
  21. private function createCode(){
  22. $_leng=strlen($this->charset);
  23. for($i=1;$i<=$this->codelen;$i++){
  24. $this->code.=$this->charset[mt_rand(0,$_leng)];
  25. }
  26. return $this->code;
  27. }
  28. //创建背景
  29. private function createBg(){
  30. //创建画布 给一个资源jubing
  31. $this->img=imagecreatetruecolor($this->width,$this->height);
  32. //背景颜色
  33. $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
  34. //画出一个矩形
  35. imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
  36. }
  37. //创建字体
  38. private function createFont(){
  39. $_x=($this->width / $this->codelen); //字体长度
  40. for ($i=0;$i<$this->codelen;$i++){
  41. //文字颜色
  42. $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  43. //资源句柄 字体大小 倾斜度 字体长度 字体高度 字体颜色 字体 具体文本
  44. imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code[$i]);
  45. }
  46. }
  47. //随机线条
  48. private function createLine(){
  49. //随机线条
  50. for ($i=0;$i<6;$i++){
  51. $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  52. imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
  53. }
  54. //随机雪花
  55. for ($i=0;$i<45;$i++){
  56. $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));
  57. imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
  58. }
  59. }
  60. //输出背景
  61. private function outPut(){
  62. //生成标头
  63. header('ContentType:img/png');
  64. //输出图片
  65. imagepng($this->img);
  66. //销毁结果集
  67. imagedestroy($this->img);
  68. }
  69. //对外输出
  70. public function doimg(){
  71. //加载背景
  72. $this->createBg();
  73. //加载文件
  74. $this->createCode();
  75. //加载线条
  76. $this->createLine();
  77. //加载字体
  78. $this->createFont();
  79. //加载背景
  80. $this->outPut();
  81. }
  82. //获取验证码
  83. public function getCode(){
  84. return strtolower($this->code);
  85. }
  86. }
  87. ?>
复制代码

调用示例:index.php 验证码 为大家推荐几篇有关php验证码的文章: php随机验证码 php生成随机验证码(图文) 用php生成带有雪花背景的验证码 php写的一个验证码 php生成动态图片验证码的一段代码



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php替换网址中参数变量的代码 下一条:php file_get_contents函数的使用问题

相关文章

查看更多