当前位置:Gxlcms > PHP教程 > 大气漂亮的验证码

大气漂亮的验证码

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

自己导入字体,可以按照自己的额需要随便修改。
字体建议是用粗的
  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<$cou;$i++){
  49. $x=rand(0,$this->width);
  50. $y=rand(0,$this->height);
  51. $jiaodu=rand(0,360);
  52. $fontsize=rand(8,15);
  53. $fonturl=$this->fonturlbg;
  54. $originalcode = $this->distrubcode;
  55. $countdistrub = strlen($originalcode);
  56. $dscode = $originalcode[rand(0,$countdistrub-1)];
  57. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));//干扰文字颜色
  58. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  59. }
  60. }
  61. private function set_code($im){
  62. $width=$this->width;
  63. $counts=$this->counts;
  64. $height=$this->height;
  65. $scode=$this->session;
  66. $y=floor($height/2)+floor($height/4);
  67. $fontsize=rand(30,35);
  68. $fonturl=$this->fonturl;
  69. $counts=$this->counts;
  70. for($i=0;$i<$counts;$i++){
  71. $char=$scode[$i];
  72. $x=floor($width/$counts)*$i+8;
  73. $jiaodu=rand(-20,30);
  74. $color = ImageColorAllocate($im,rand(0,200),rand(50,200),rand(100,240));//文字颜色
  75. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  76. }
  77. }
  78. private function sessioncode(){
  79. $originalcode = $this->distrubcode;
  80. $countdistrub = strlen($originalcode);
  81. $_dscode = "";
  82. $counts=$this->counts;
  83. for($j=0;$j<$counts;$j++){
  84. $dscode = $originalcode[rand(0,$countdistrub-1)];
  85. $_dscode.=$dscode;
  86. }
  87. return $_dscode;
  88. }
  89. }
  90. Header("Content-type: image/GIF");
  91. $imagecode=new Imagecode(160,50);
  92. $imagecode->imageout();

人气教程排行