A wrapper class for PHP verification code

WBOY
Release: 2016-07-25 08:58:47
Original
984 people have browsed it
  1. /**

  2. * Verification code class
  3. * edit bbs.it-home.org
  4. */
  5. class ValidationCode {
  6. private $width;
  7. private $height;
  8. private $codeNum;
  9. private $image; //image Resources
  10. private $disturbColorNum;
  11. private $checkCode;
  12. function __construct($width=80, $height=20, $codeNum=4){
  13. $this->width=$width;
  14. $this->height= $height;
  15. $this->codeNum=$codeNum;
  16. $this->checkCode=$this->createCheckCode();
  17. $number=floor($width*$height/15);
  18. if($number > 240-$codeNum){

  19. $this->disturbColorNum= 240-$codeNum;
  20. }else{
  21. $this->disturbColorNum=$number;
  22. }< /p>
  23. }

  24. //Output the image to the browser by accessing this method
  25. function showImage($fontFace=""){
  26. //The first step: Create the image background
  27. $this->createImage ();
  28. //Step 2: Set the interference element
  29. $this->setDisturbColor();
  30. //Step 3: Draw random text into the image
  31. $this->outputText($fontFace);
  32. //Step 4: Output image
  33. $this->outputImage();
  34. }

  35. //Get the randomly created verification code string by calling this method

  36. function getCheckCode(){
  37. return $this->checkCode;
  38. }
  39. private function createImage(){
  40. //Create image resource//bbs.it-home.org
  41. $this->image=imagecreatetruecolor($this->width, $this->height);
  42. //Random background color
  43. $backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
  44. // Add color to the background
  45. imagefill($this->image, 0, 0, $backColor);
  46. //Set the border color
  47. $border=imagecolorallocate($this->image, 0, 0, 0);
  48. //Draw a rectangular border
  49. imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
  50. }
  51. private function setDisturbColor() {
  52. for($i=0; $i<$this->disturbColorNum; $i++){
  53. $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  54. imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
  55. }
  56. for($i=0; $i<10; $i++){
  57. $color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
  58. imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20 , 200), 55, 44, $color);
  59. }
  60. }
  61. private function createCheckCode(){
  62. //The random code is mainly generated here, starting from 2 to distinguish 1 and l
  63. $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  64. $string='';
  65. for($i=0; $i < $this->codeNum; $i++){
  66. $char=$code{rand(0, strlen($code)-1)};
  67. $string.=$char;
  68. }
  69. return $string;
  70. }
  71. private function outputText($fontFace=""){
  72. for($i=0; $i<$this->codeNum; $i++) {
  73. $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
  74. if($fontFace==""){
  75. $fontsize =rand(3, 5);
  76. $x=floor($this->width/$this->codeNum)*$i+3;
  77. $y=rand(0, $this->height-15 );
  78. imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
  79. }else{
  80. $fontsize=rand(12, 16) ;
  81. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  82. $y=rand($fontSize+5, $this->height);
  83. imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
  84. }
  85. }
  86. }

  87. //Output image information

  88. private function outputImage() {
  89. if(imagetypes() & IMG_GIF){
  90. header("Content-Type:image/gif");
  91. imagepng ($this->image);
  92. }else if(imagetypes() & IMG_JPG){
  93. header("Content-Type:image/jpeg");
  94. imagepng($this->image); //bbs. it-home.org
  95. }else if(imagetypes() & IMG_PNG){
  96. header("Content-Type:image/png");
  97. imagepng($this->image);
  98. }else if(imagetypes() & IMG_WBMP){
  99. header("Content-Type:image/vnd.wap.wbmp");
  100. imagepng($this->image);
  101. }else{
  102. die("PHP does not support image creation");
  103. }
  104. }
  105. function __destruct(){
  106. imagedestroy($this->image);
  107. }
  108. }
  109. ?>

Copy code

An example of calling the verification code class:

  1. session_start();
  2. include "validationcode.class.php";
  3. $code=new ValidationCode(80, 20, 4);
  4. $code->showImage(); / /Output to the page for registration or login
  5. $_SESSION["code"]=$code->getCheckCode(); //Save the verification code to the server
  6. ?>
Copy 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!