- /**
- * Generic verification code class img. php
- * Version: V0.1
- * Bbs.it-home.org 2013/3/1
- */
- Class ValidateCode {
- Private $ charset = "abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // random factor
- Private $ code; // verification code text
- Private $ codelen = 4; // several texts are displayed in the verification code
- Private $ width = 130; // verification code width
- Private $ height = 50; // verification code height
- Private $ img; // verification code resource handle
- Private $ font; // The specified font.
- Private $ fontsize = 20; // The specified font size
- Private $ fontcolor; // random font color
- // Font of the constructor class
- Public function _ construct (){
- $ This-> font = ROOT_PATH. '/font/elephant. ttf ';
- }
- // Create 4 random codes
- Private function createCode (){
- $ _ Leng = strlen ($ this-> charset );
- For ($ I = 1; $ I <= $ this-> codelen; $ I ++ ){
- $ This-> code. = $ this-> charset [mt_rand (0, $ _ leng)];
- }
- Return $ this-> code;
- }
- // Create a background
- Private function createBg (){
- // Create a canvas for a resource jubing
- $ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
- // Background color
- $ Color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 ));
- // Draw a rectangle
- Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );
- }
- // Create a font
- Private function createFont (){
- $ _ X = ($ this-> width/$ this-> codelen); // font length
- For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
- // Text color
- $ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
- // Resource handle font size skew font length font height font color font specific text
- Imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30), $ _ x * $ I + mt_rand (1.4), $ this-> height, $ color, $ this-> font, $ this-> code [$ I]);
- }
- }
- // Random line
- Private function createLine (){
- // Random line
- For ($ I = 0; $ I <6; $ I ++ ){
- $ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
- Imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color );
- }
- // Random snowflake
- For ($ I = 0; $ I <45; $ I ++ ){
- $ Color = imagecolorallocate ($ this-> img, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 ));
- Imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color );
- }
- }
- // Output background
- Private function outPut (){
- // Generate the header
- Header ('contenttype: img/png ');
- // Output image
- Imagepng ($ this-> img );
- // Destroy the result set
- Imagedestroy ($ this-> img );
- }
- // Output externally
- Public function doimg (){
- // Load the background
- $ This-> createBg ();
- // Load the file
- $ This-> createCode ();
- // Load the line
- $ This-> createLine ();
- // Load the font
- $ This-> createFont ();
- // Load the background
- $ This-> outPut ();
- }
- // Obtain the verification code
- Public function getCode (){
- Return strtolower ($ this-> code );
- }
- }
- ?>
Call example: index. php recommends several articles on php verification code: php random verification code php generates random verification code (text) use php to generate a verification code with a snowflake background. a verification code written by php to generate a dynamic image verification code. |