php verification code class php verification code

WBOY
Release: 2016-07-25 09:04:21
Original
1018 people have browsed it
  1. /**
  2. * Universal verification code class img.php
  3. * Version: V0.1
  4. * bbs.it-home.org 2013/3/1
  5. */
  6. class ValidateCode {
  7. private $charset="abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //Random factor
  8. private $code; //Verification code text
  9. codelen= 4; //Verification code displays several words
  10. private $width=130; //Verification code width
  11. private $height=50; //Verification code height
  12. private $img; //Verification code resource handle
  13. private $font; //Specified font
  14. private $fontsize=20; //Specified font size
  15. private $fontcolor; //Random font color
  16. //Construction class to write font
  17. public function __construct(){
  18. $this->font =ROOT_PATH.'/font/elephant.ttf';
  19. }
  20. //Create 4 random codes
  21. private function createCode(){
  22. $_leng=strlen($this->charset);
  23. for($i=1 ;$icodelen;$i++){
  24. $this->code.=$this->charset[mt_rand(0,$_leng)];
  25. }
  26. return $this-> code;
  27. }
  28. //Create a background
  29. private function createBg(){
  30. //Create a canvas for a resource jubing
  31. $this->img=imagecreatetruecolor($this->width,$this->height) ;
  32. //Background color
  33. $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
  34. //Draw a rectangle
  35. imagefilledrectangle($this-> img,0,$this->height,$this->width,0,$color);
  36. }
  37. //Create font
  38. private function createFont(){
  39. $_x=($this->width / $this->codelen); //Font length
  40. for ($i=0;$icodelen;$i++){
  41. //Text color
  42. $color=imagecolorallocate($this-> ;img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  43. //Resource handle font size tilt font length font height font color font specific text
  44. imagettftext($this->img,$this- >fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code [$i]);
  45. }
  46. }
  47. //Random lines
  48. private function createLine(){
  49. //Random lines
  50. for ($i=0;$i $color = imagecolorallocate( $this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  51. imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$ this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
  52. }
  53. //Random snowflakes
  54. for ($i=0; $i $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));
  55. imagestring($this->img,mt_rand( 1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
  56. }
  57. }
  58. //Output background
  59. private function outPut (){
  60. //Generate header
  61. header('ContentType:img/png');
  62. //Output image
  63. imagepng($this->img);
  64. //Destroy the result set
  65. imagedestroy($this-> ;img);
  66. }
  67. //External output
  68. public function doimg(){
  69. //Load background
  70. $this->createBg();
  71. //Load file
  72. $this->createCode();
  73. / /Load line
  74. $this->createLine();
  75. //Load font
  76. $this->createFont();
  77. //Load background
  78. $this->outPut();
  79. }
  80. //Get Verification code
  81. public function getCode(){
  82. return strtolower($this->code);
  83. }
  84. }
  85. ?>
Copy code

Call example: index.php php verification code class php verification code I recommend several articles about PHP verification codes to everyone: php random verification code php generates random verification code (picture and text) Use php to generate verification code with snowflake background A verification code written in php php code to generate dynamic image verification code



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!