Home > Backend Development > PHP Tutorial > A beautiful PHP verification code

A beautiful PHP verification code

WBOY
Release: 2016-07-25 08:46:34
Original
1208 people have browsed it
自己导入字体,可以按照自己的额需要随便修改。
A beautiful PHP verification code
  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:WindowsFontsTektonPro-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<$cou;$i++){
  37. $x=rand(0,$this->width);
  38. $y=rand(0,$this->height);
  39. $jiaodu=rand(0,360);
  40. $fontsize=rand(8,15);
  41. $fonturl=$this->fonturl;
  42. $originalcode = $this->distrubcode;
  43. $countdistrub = strlen($originalcode);
  44. $dscode = $originalcode[rand(0,$countdistrub-1)];
  45. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));
  46. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  47. }
  48. }
  49. private function set_code($im){
  50. $width=$this->width;
  51. $counts=$this->counts;
  52. $height=$this->height;
  53. $scode=$this->session;
  54. $y=floor($height/2)+floor($height/4);
  55. $fontsize=rand(30,35);
  56. $fonturl="C:WindowsFontsAdobeGothicStd-Bold.otf";//$this->fonturl;
  57. $counts=$this->counts;
  58. for($i=0;$i<$counts;$i++){
  59. $char=$scode[$i];
  60. $x=floor($width/$counts)*$i+8;
  61. $jiaodu=rand(-20,30);
  62. $color = ImageColorAllocate($im,rand(0,50),rand(50,100),rand(100,140));
  63. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  64. }
  65. }
  66. private function sessioncode(){
  67. $originalcode = $this->distrubcode;
  68. $countdistrub = strlen($originalcode);
  69. $_dscode = "";
  70. $counts=$this->counts;
  71. for($j=0;$j<$counts;$j++){
  72. $dscode = $originalcode[rand(0,$countdistrub-1)];
  73. $_dscode.=$dscode;
  74. }
  75. return $_dscode;
  76. }
  77. }
  78. Header("Content-type: image/GIF");
  79. $imagecode=new Imagecode(160,50);
  80. $imagecode->imageout();
复制代码

验证码, PHP


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template