Php verification code Image program

Source: Internet
Author: User
Tags border color drawtext md5 rand

<? Php verification code Image program
/*
* File name: class. safeCode. php
* Class name: safeCode
* Purpose: to generate a verification code image for a web application
* Current version: 1.0.2
* Author: NoAngels
* Contact: flare_1023@163.com QQ: 82535599 MSN: atizu@hotmail.com
* Development Date: July 15, June 20, 2008
* Last Updated: July 15, June 21, 2008
* Update content:
* Version 1.0.2
* 1. Updated support for linux hosts and set the GDFONTPATH environment variable,
* Putenv ('gdfontpath = '. realpath ('.'));
* 2. Avoid using this method when the antialias function is not supported. Avoid using the GD library of earlier versions.
* Version 1.0.1
* 1. Updated the image of the specified type. Supported types include: png, jpeg, and gif. The default value is png (the best effect)
* 2. Optimized some code.
*/
Class safeCode {
Function _ construct (){
# Set the default safe security protection code
$ This->__ safe = substr (md5 (time (), 0, 4 );
        }
Function _ destruct (){
# Releasing resources
Unset ($ this->__ img, $ this->__ canvasWidth, $ this->__ canvasHeight, $ this->__ codeLength, $ this->__ spacePerChar, $ this->__ code, $ this->__ safe, $ this->__ background, $ this->__ borderColor, $ this->__ colors, $ this->__ font, $ this->__ sessionName );
        }
Function setCanvas ($ width = 200, $ height = 60 ){
# Set the canvas width and height. Default values are available.
$ This->__ canvasWidth = $ width;
$ This->__ canvasHeight = $ height;
$ This->__ img = imagecreatetruecolor ($ width, $ height );
Return;
        }
Function setSafe ($ char ){
# Set the security code. If the security code is not called, the system will set it by itself.
$ This->__ safe = $ char;
Return;
        }
Function setCodeLength ($ num = 8 ){
# Set the verification code length
$ This->__ codeLength = $ num;
        }       
Function setFont ($ fontName ){
# Set the font used for drawing. The font file must be in the same directory as the class file.
$ This->__ font = $ fontName;
Return;
        }
Function setBackground ($ arg1 = 255, $ arg2 = 255, $ arg3 = 255 ){
# Set the background color
$ This->__ background = imagecolorallocate ($ this->__ img, $ arg1, $ arg2, $ arg3 );
Return;
        }
Function setBorderColor ($ arg1 = 128, $ arg2 = 128, $ arg3 = 128 ){
# Set the border color
$ This->__ borderColor = imagecolorallocate ($ this->__ img, $ arg1, $ arg2, $ arg3 );
        }
Function setFontColor ($ arr ){
# Set the color used for plotting. The parameter is an array of colors.
Foreach ($ arr as $ color ){
$ This->__ colors [] = imagecolorallocate ($ this->__ img, $ color [0], $ color [1], $ color [2]);
                }
Return;
        }       
Function setSession ($ sessionName = 'safecode '){
# Set the session variable name. The default value is $ _ SESSION ['safecode'].
$ This->__ sessionName = $ sessionName;
# $ _ SESSION [$ sessionName] = $ this->__ code;
Return;
        }
Function display ($ type = 'PNG '){
# Display image. The Display type can be specified. Currently, png (default), jpeg, and gif are supported.
$ This->__ setSpacePerChar ();
Imagefilledrectangle ($ this->__ img, 1, 1, $ this->__ canvasWidth-2, $ this->__ canvasHeight-2, $ this->__ background );
Imagerectangle ($ this->__ img, 0, 0, $ this->__ canvasWidth-1, $ this->__ canvasHeight-1, $ this->__ borderColor );
$ This->__ drawText ($ this->__ colors, $ this->__ getCode (), $ this->__ font );
# Fixed linux and earlier version GD Library problems
If (function_exists (imageantialias )){
Imageantialias ($ this->__ img, true );
                }
$ _ SESSION [$ this->__ sessionName] = $ this->__ code;
Switch ($ type ){
Case 'PNG ':
Header ('content-type: image/png ');
Imagepng ($ this->__ img );
Case 'jpeg ':
Header ('content-type: image/jpeg ');
Imagejpeg ($ this->__ img, '', 75 );
Case 'GIF ':
Header ('content-type: image/gif ');
Imagegif ($ this->__ img );
Default:
Header ('content-type: image/png ');
Imagepng ($ this->__ img );
                }
Imagegif ($ this->__ img );
        }       
Private function _ setSpacePerChar (){
# Private Function, set the character interval
$ This->__ spacePerChar = $ this->__ canvasWidth/$ this->__ codeLength;
Return;
        }
Private function _ getCode (){
# Obtain the verification code
$ This->__ code = substr (md5 (time (). $ this->__ safe), rand (0, 24), $ this->__ codeLength );
Return $ this->__ code;
        }
Private function _ drawText ($ colors, $ code, $ fontName ){
# Start plotting
# Set GBFONTPATH to the current directory. Otherwise, an error will be reported in linux.
Putenv ('gdfontpath = '. realpath ('.'));
For ($ I = 0; $ I <strlen ($ code); $ I ++ ){
$ Color = $ colors [$ I % count ($ colors)];
Imagettftext (
$ This->__ img,
24 + rand (0, 8 ),
-20 + rand (0, 40 ),
($ I + 0.3) * $ this->__ spacePerChar,
$ This->__ canvasHeight-20 + rand (0, 20 ),
$ Color,
$ FontName,
$ Code {$ I}
);
                }
# Drawing random solid lines
For ($ I = 0; I I <400; $ I ++ ){
$ X1 = rand (5, $ this->__ canvasWidth-5 );
$ Y1 = rand (5, $ this->__ canvasHeight-5 );
$ X2 = $ x1-4 + rand (0, 8 );
$ Y2 = $ y1-4 + rand (0, 8 );
Imageline ($ this->__ img, $ x1, $ y1, $ x2, $ y2, $ this->__ colors [rand (0, count ($ this->__ colors)-1)]);
                }
Return;
        }
Private $ __img = NULL;
Private $ __canvaswidth = 120;
Private $ __canvasheight = 80;
Private $ __codelength = 8;
Private $ __spaceperchar = 0;
Private $ __code = 12345678;
Private $ __safe = 2008;
Private $ __background = NULL;
Private $ __bordercolor = NULL;
Private $ __colors = array ();
Private $ __font = 'Arial. Ttf ';
Private $ __sessionname = '';
}
?>

Usage.

<? Php
# Test File
Include_once ('class. safeCode. Php ');
Session_start ();
$ SafeCode = new safeCode;
$ SafeCode-> setCanvas (200, 50 );
$ SafeCode-> setCodeLength (8 );
$ SafeCode-> setSafe ('000000'); # sets the security code. If no default value is set, it is dynamically generated.
$ SafeCode-> setBackground ();
$ SafeCode-> setBorderColor ();
$ SafeCode-> setFont ('Arial. Ttf'); # set the font. It must be in the same directory. The font file
$ Colors = array (128, 64,192), array (192, 64,128), array (108,192, 64 ));
$ SafeCode-> setFontColor ($ colors );
$ SafeCode-> setSession ();
$ SafeCode-> display ('jpeg ');

?>

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.