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

一个漂亮的PHP验证码

程序员文章站 2022-04-15 08:37:30
...
自己导入字体,可以按照自己的额需要随便修改。
一个漂亮的PHP验证码
  1. class Imagecode{
  2. private $width ;
  3. private $height;
  4. private $counts;
  5. private $distrubcode;
  6. private $fonturl;
  7. private $session;
  8. function __construct($width = 120,$height = 30,$counts = 5,$distrubcode="1235467890qwertyuipkjhgfdaszxcvbnm",$fonturl="C:\Windows\Fonts\TektonPro-BoldCond.otf"){
  9. $this->width=$width;
  10. $this->height=$height;
  11. $this->counts=$counts;
  12. $this->distrubcode=$distrubcode;
  13. $this->fonturl=$fonturl;
  14. $this->session=$this->sessioncode();
  15. session_start();
  16. $_SESSION['code']=$this->session;
  17. }
  18. function imageout(){
  19. $im=$this->createimagesource();
  20. $this->setbackgroundcolor($im);
  21. $this->set_code($im);
  22. $this->setdistrubecode($im);
  23. ImageGIF($im);
  24. ImageDestroy($im);
  25. }
  26. private function createimagesource(){
  27. return imagecreate($this->width,$this->height);
  28. }
  29. private function setbackgroundcolor($im){
  30. $bgcolor = ImageColorAllocate($im, rand(200,255),rand(200,255),rand(200,255));//±3?°??é?
  31. imagefill($im,0,0,$bgcolor);
  32. }
  33. private function setdistrubecode($im){
  34. $count_h=$this->height;
  35. $cou=floor($count_h*2);
  36. for($i=0;$i $x=rand(0,$this->width);
  37. $y=rand(0,$this->height);
  38. $jiaodu=rand(0,360);
  39. $fontsize=rand(8,15);
  40. $fonturl=$this->fonturl;
  41. $originalcode = $this->distrubcode;
  42. $countdistrub = strlen($originalcode);
  43. $dscode = $originalcode[rand(0,$countdistrub-1)];
  44. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));
  45. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  46. }
  47. }
  48. private function set_code($im){
  49. $width=$this->width;
  50. $counts=$this->counts;
  51. $height=$this->height;
  52. $scode=$this->session;
  53. $y=floor($height/2)+floor($height/4);
  54. $fontsize=rand(30,35);
  55. $fonturl="C:\Windows\Fonts\AdobeGothicStd-Bold.otf";//$this->fonturl;
  56. $counts=$this->counts;
  57. for($i=0;$i $char=$scode[$i];
  58. $x=floor($width/$counts)*$i+8;
  59. $jiaodu=rand(-20,30);
  60. $color = ImageColorAllocate($im,rand(0,50),rand(50,100),rand(100,140));
  61. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  62. }
  63. }
  64. private function sessioncode(){
  65. $originalcode = $this->distrubcode;
  66. $countdistrub = strlen($originalcode);
  67. $_dscode = "";
  68. $counts=$this->counts;
  69. for($j=0;$j $dscode = $originalcode[rand(0,$countdistrub-1)];
  70. $_dscode.=$dscode;
  71. }
  72. return $_dscode;
  73. }
  74. }
  75. Header("Content-type: image/GIF");
  76. $imagecode=new Imagecode(160,50);
  77. $imagecode->imageout();
复制代码

验证码, PHP