欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

大气漂亮的验证码

程序员文章站 2022-05-29 21:54:23
...
自己导入字体,可以按照自己的额需要随便修改。
字体建议是用粗的
  1. class Imagecode{
  2. private $width ;
  3. private $height;
  4. private $counts;
  5. private $distrubcode;
  6. private $fonturl;
  7. private $fonturlbg;
  8. private $session;
  9. /**
  10. * Enter description here...
  11. *
  12. * @param unknown_type $width 宽度
  13. * @param unknown_type $height 高度
  14. * @param unknown_type $counts 字符个数
  15. * @param unknown_type $distrubcode 随机字符范围
  16. * @param unknown_type $fonturl 严重码字体
  17. * @param unknown_type $fonturlbg 干扰字符字体
  18. */
  19. function __construct($width = 120,$height = 30,$counts = 5,$distrubcode="1235467890qwertyuipkjhgfdaszxcvbnm",$fonturl="msyhbd.ttf",$fonturlbg='f.ttf'){
  20. $this->width=$width;
  21. $this->height=$height;
  22. $this->counts=$counts;
  23. $this->distrubcode=$distrubcode;
  24. $this->fonturl=$fonturl;
  25. $this->fonturlbg=$fonturlbg ? $fonturlbg :$fonturl;//未设置干扰字体时使用
  26. $this->session=$this->sessioncode();
  27. session_start();
  28. $_SESSION['code']=$this->session;
  29. }
  30. function imageout(){
  31. $im=$this->createimagesource();
  32. $this->setbackgroundcolor($im);
  33. $this->set_code($im);
  34. $this->setdistrubecode($im);
  35. ImageGIF($im);
  36. ImageDestroy($im);
  37. }
  38. private function createimagesource(){
  39. return imagecreate($this->width,$this->height);
  40. }
  41. private function setbackgroundcolor($im){
  42. $bgcolor = ImageColorAllocate($im, rand(200,255),rand(200,255),rand(200,255));//±3?°??é?
  43. imagefill($im,0,0,$bgcolor);
  44. }
  45. private function setdistrubecode($im){
  46. $count_h=$this->height;
  47. $cou=floor($count_h*2);
  48. for($i=0;$i $x=rand(0,$this->width);
  49. $y=rand(0,$this->height);
  50. $jiaodu=rand(0,360);
  51. $fontsize=rand(8,15);
  52. $fonturl=$this->fonturlbg;
  53. $originalcode = $this->distrubcode;
  54. $countdistrub = strlen($originalcode);
  55. $dscode = $originalcode[rand(0,$countdistrub-1)];
  56. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));//干扰文字颜色
  57. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  58. }
  59. }
  60. private function set_code($im){
  61. $width=$this->width;
  62. $counts=$this->counts;
  63. $height=$this->height;
  64. $scode=$this->session;
  65. $y=floor($height/2)+floor($height/4);
  66. $fontsize=rand(30,35);
  67. $fonturl=$this->fonturl;
  68. $counts=$this->counts;
  69. for($i=0;$i $char=$scode[$i];
  70. $x=floor($width/$counts)*$i+8;
  71. $jiaodu=rand(-20,30);
  72. $color = ImageColorAllocate($im,rand(0,200),rand(50,200),rand(100,240));//文字颜色
  73. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  74. }
  75. }
  76. private function sessioncode(){
  77. $originalcode = $this->distrubcode;
  78. $countdistrub = strlen($originalcode);
  79. $_dscode = "";
  80. $counts=$this->counts;
  81. for($j=0;$j $dscode = $originalcode[rand(0,$countdistrub-1)];
  82. $_dscode.=$dscode;
  83. }
  84. return $_dscode;
  85. }
  86. }
  87. Header("Content-type: image/GIF");
  88. $imagecode=new Imagecode(160,50);
  89. $imagecode->imageout();
复制代码