Home  >  Article  >  Backend Development  >  Attractive and beautiful verification code

Attractive and beautiful verification code

WBOY
WBOYOriginal
2016-07-25 08:49:13943browse
Import the fonts yourself and modify them according to your needs.
It is recommended to use bold font
  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 width
  13. * @param unknown_type $height height
  14. * @param unknown_type $counts number of characters
  15. * @param unknown_type $distrubcode random character range
  16. * @param unknown_type $fonturl Severe code font
  17. * @param unknown_type $fonturlbg Interference character font
  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;//Use when interference font is not set
  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));//Interference text color
  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));//Text color
  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();
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn