PHP Verification Code class PHP Verification code

Source: Internet
Author: User
Tags php write
  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. Private $codelen = 4; Verification code displays several text
  10. Private $width = 130; Verification Code width
  11. Private $height = 50; Verification Code Height
  12. Private $img; Validation code resource handle
  13. Private $font; The specified font
  14. Private $fontsize = 20; The specified font size
  15. Private $fontcolor; Font Color random
  16. Construct a class to write fonts
  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; $i <= $this->codelen; $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. Creating fonts
  38. Private Function CreateFont () {
  39. $_x= ($this->width/$this->codelen); Font length
  40. for ($i =0; $i < $this->codelen; $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 <6; $i + +) {
  51. $color = Imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
  52. Imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $color);
  53. }
  54. Random Snowflakes
  55. for ($i =0; $i <45; $i + +) {
  56. $color = Imagecolorallocate ($this->img,mt_rand (220,255), Mt_rand (220,255), Mt_rand (220,255));
  57. Imagestring ($this->img,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color);
  58. }
  59. }
  60. Output background
  61. Private Function OutPut () {
  62. Generate headers
  63. Header (' contenttype:img/png ');
  64. Output picture
  65. Imagepng ($this->img);
  66. Destroying result sets
  67. Imagedestroy ($this->img);
  68. }
  69. External output
  70. Public Function doimg () {
  71. Load background
  72. $this->CREATEBG ();
  73. Loading files
  74. $this->createcode ();
  75. Loading lines
  76. $this->createline ();
  77. Loading fonts
  78. $this->createfont ();
  79. Load background
  80. $this->output ();
  81. }
  82. Get Verification Code
  83. Public Function GetCode () {
  84. Return Strtolower ($this->code);
  85. }
  86. }
  87. ?>
Copy Code

Invocation Example: index.php for you to recommend a few articles about PHP verification code: PHP Random Verification code PHP generated random verification Code (graphics) with a snowflake background verification code PHP write a verification code PHP to generate a code of dynamic picture Verification code

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.