PHP graphic verification code program

Source: Internet
Author: User
Tags image identifier imagejpeg
Recently, I have been asked how the graphic verification code in this Blog is made. the following is the source code of the php graphic verification code. please confirm. class. image. php * & nbsp; class. image. php & nbsp; ZhuWeiwe has recently been asked how the graphic verification code in this Blog is made. the following is the source code of the php graphic verification code.

Please confirm.

Class. image. php

/*
========================================================== ==============================
Class. image. php Zhu Weiwei
Copyright©2006-2008
Http://www.cncmm.com China. All Rights Reserved.
========================================================== ==============================
*/
Class Image
{
PRivate $ img;
Private $ width;
Private $ height;
Private $ color;
Private $ font;
Private $ mime = "image/jpeg ";

/* 1, GIF 2, JPEG 3, PNG */
Private $ type = 2;
Private $ angle = 0;
Private $ text;
Private $ textW;
Private $ textH;
Private $ size = 5;
Private $ tcolor;


Function _ construct ($ w = "100", $ h = "50 "){
$ This-> width = $ w;
$ This-> height = $ h;
}
 
Function create (){
If ($ this-> type! = 1 & function_exists ('imagecreatetruecolor ')){
// This function cannot be used in GIF file format.
// This function requires GD 2.0.1 or later.
// Returns an image identifier, representing a black image of x_size and y_size.
$ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
} Else {
$ This-> img = imagecreate ($ this-> width, $ this-> height );
}
}

/*
* 1 = GIF 2 = JPG
* 3 = PNG
*/
Function createFromFile ($ f ){
$ ImgInfo = getimagesize ($ f );
$ This-> type = $ imgInfo [2];

Switch ($ this-> type ){
Case 1:
$ This-> loadGifFile ($ f );
Break;
     
Case 2:
$ This-> loademedifile ($ f );
Break;
     
Case 3:
$ This-> loadPngFile ($ f );
Break;
     
Default:
Die ("This Image Type Not Support loadimage! ");
Exit ();
}
}
 
Function loadGifFile ($ f ){
/// 1.6 -- 2.0.28
// This function is unavailable when there is a GD Library between the two versions.
$ This-> img = imagecreatefromgif ($ f) or die ("Load Gif picture error! ");
$ This-> width = imagesx ($ this-> img );
$ This-> height = imagesy ($ this-> img );
$ This-> mime = "image/gif ";
$ This-> type = 1;
}
 
 
Function loadparts File ($ f ){
// JPEG support is only available when PHP is compiled with a GD-1.8 or a later version
$ This-> img = imagecreatefromjpeg ($ f) or die ("Load Jpeg picture error! ");
$ This-> width = imagesx ($ this-> img );
$ This-> height = imagesy ($ this-> img );
$ This-> mime = "image/jpeg ";
$ This-> type = 2;
}
 
 
Function loadPngFile ($ f ){
$ This-> img = imagecreatefrompng ($ f) or die ("Load Png picture error! ");
$ This-> width = imagesx ($ this-> img );
$ This-> height = imagesy ($ this-> img );
$ This-> mime = "image/png ";
$ This-> type = 3;
}


 
Function setBackgroundColor ($ r, $ g, $ B ){
# Rectangle filled with color
# The coordinates in the upper left corner are x1 and y1.
# The coordinates in the lower right corner are x2, y2
#0, 0 is the top left corner of the image
$ Color = imagecolorallocate ($ this-> img, $ r, $ g, $ B );
Return imagefilledrectangle ($ this-> img, 0, 0, $ this-> width, $ this-> height, $ color );
}
 
Function setColor ($ r, $ g, $ B ){
$ This-> color = imagecolorallocate ($ this-> img, $ r, $ g, $ B );
Return (-1 = $ this-> color )? (False): ($ this-> color );
}
 
 
Function setTextColor ($ r, $ g, $ B ){
$ This-> tcolor = imagecolorallocate ($ this-> img, $ r, $ g, $ B );
Return (-1 = $ this-> tcolor )? (False): ($ this-> tcolor );
}
 
  
Function setText ($ t ){
$ This-> text = $ t;
}

Function waterMark ($ r, $ g, $ B, $ alpha ){
$ This-> tcolor = imagecolorclosestalpha ($ this-> img, $ r, $ g, $ B, $ alpha );
}
  
 
Function setFontFile ($ f ){
$ This-> font = $ f;
}
 
Function addTrueTypeText ($ xoffset, $ yoffset ){
# $ X = ($ this-> width-$ this-> textW-10)> 0 )? ($ This-> width-$ this-> textW-10): (0 );
# $ Y = ($ this-> height-10)> 0 )? ($ This-> height-10): ($ this-> height );
// Use the TrueType font to write text to the image
// ** X, y coordinates are the coordinates in the lower left corner of the string, and the upper left corner of the entire image is 0, 0.
Imagettftext ($ this-> img,
$ This-> size,
$ This-> angle,
$ Xoffset,
$ Yoffset,
$ This-> tcolor,
$ This-> font,
$ This-> text );
}

Function addText (){
// Http://www.devdao.com/use color to draw string s to image
// ** X, y coordinate is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0.
// If the font is 1, 2, 3, 4, or 5, use the built-in font.
Imagestring ($ this-> img, $ font, $ x, $ y, $ this-> text, $ this-> tcolor );
}
 
Function setSize ($ s ){
$ This-> size = $ s;
}
 
Function setAngle ($ ){
$ This-> angle = $;
}
 
 
Function setWidth ($ w ){
$ This-> width = $ w;
}
 
 
Function setHeight ($ h ){
$ This-> height = $ h;
}
 
 
Function getWidth (){
Return $ this-> width;
}
 
 
Function getHeight (){
Return $ this-> height;
}

Function setPixel ($ x, $ y, $ color ){
Return imagesetpixel ($ this-> img, $ x, $ y, $ color );
}

Function setLine ($ x1, $ y1, $ x2, $ y2, $ color ){
Return imageline ($ this-> img, $ x1, $ y1, $ x2, $ y2, $ color );
}
 
Function createImageFile ($ f = "default "){
If (! Empty ($ f )){
Switch ($ this-> type ){
Case 1:
Imagegif ($ this-> img, $ f. ". gif", 80 );
Break;
       
Case 2:
Imagejpeg ($ this-> img, $ f. ". jpg", 80 );
Break;
       
Case 3:
Imagepng ($ this-> img, $ f. ". png", 80 );
Break;
       
Default:
Die ("This Image Type Not Support create! ");
Exit ();
}
Imagedestroy ($ this-> img );
}
}

                 
Function show (){
Header ("Content-type:". $ this-> mime );
Switch ($ this-> type ){
Case 1:
Imagegif ($ this-> img );
Break;
     
Case 2:
Imagejpeg ($ this-> img );
Break;
     
Case 3:
Imagepng ($ this-> img );
Break;
     
Default:
Die ("This Image Type Not Support show! ");
Exit ();
}
Imagedestroy ($ this-> img );
}
  
/* Zoom Out Picture */
Function roomOut ($ maxW, $ maxH ){
$ WW = ($ maxW) and ($ this-> width> $ maxW ));
$ HH = ($ maxH) and ($ this-> height> $ maxH ));
   
If ($ WW or $ HH ){
If ($ WW ){
$ WRatio = $ maxW/$ this-> width;
$ WResize = true;
}
If ($ HH ){
$ HRatio = $ maxH/$ this-> height;
$ HResize = true;
}
     
If ($ wResize and $ hResize ){
If ($ wRatio <$ hRatio ){
$ Ratio = $ wRatio;
} Else {
$ Ratio = $ hRatio;
}
} Elseif ($ wResize ){
$ Ratio = $ wRatio;
} Elseif ($ hResize ){
$ Ratio = $ hRatio;
}
} Else {
$ Ratio = 1;
}
   
$ NewWidth = $ this-> width * $ ratio;
$ NewHeight = $ this-> height * $ ratio;
   
If (function_exists ("imagecopyresampled ")){
$ Newimg = imagecreatetruecolor ($ newWidth, $ newHeight );
Imagecopyresampled ($ newimg, $ this-> img, 0, 0, 0, 0, $ newWidth, $ newHeight, $ this-> width, $ this-> height );
} Else {
$ Newimg = imagecreate ($ newWidth, $ newHeight );
Imagecopyresized ($ newimg, $ this-> img, 0, 0, 0, 0, $ newWidth, $ newHeight, $ this-> width, $ this-> height );
}
$ This-> img = $ newimg;
$ This-> width = $ newWidth;
$ This-> height = $ newHeight;
}
}
Imgverifycode. php

/*
========================================================== ==============================
Imgverifycode. php Zhu Weiwei
Copyright©2006-2008
Http://www.cncmm.com China. All Rights Reserved.
========================================================== ==============================
*/
Require ('class. image. php ');
# Char number
$ N = 4;
# Font size
$ S = 18;
# Font padding
$ P = 5;
# Font angle-a,
$ A = 15;
# Image width
$ W = ($ s + $ p) * $ n + $ p;
# Image height;
$ H = intval ($ s * 1.5 );

$ C = ($ h + $ s)/2;
$ O = abs ($ h-$ S-2 * $ p)/2;
$ R = 1;

$ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
# $ Str = '1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxy ';
# $ Str = '20140901 ';

$ Len = strlen ($ str)-1;
$ Code = '';


$ Im = new Image ($ w, $ h );
$ Im-> create ();
$ Im-> setBackgroundColor (mt_rand (0xdd, 0xff), mt_rand (0xdd, 0xff), mt_rand (0xdd, 0xff ));

$ Im-> setSize ($ s );

For ($ I = $ p; $ I <$ w-$ p; $ I = $ I + $ s + $ p ){
$ Char = $ str {mt_rand (0, $ len )};
$ Code = $ code. $ char;
 
$ Im-> setFontFile ($ CFG-> dirroot. '/font/'. mt_rand (2, 5). '. ttf ');
 
$ Im-> setText ($ char );
$ Im-> setAngle (mt_rand (-$ a, $ ));
$ Im-> setTextColor (mt_rand (0x66), mt_rand (0x66), mt_rand (0x66 ));
$ Im-> addTrueTypeText ($ I, mt_rand ($ c-$ o, $ c + $ o ));
For ($ j = 0; $ j <8 * $ r; $ j ++ ){
$ Xs = mt_rand ($ I-$ s/2 + $ p, $ I + $ s/2-$ p );
$ Ys = mt_rand ($ p, $ h-$ p );
$ Color = $ im-> setColor (mt_rand (0x66), mt_rand (0x66), mt_rand (0x66 ));
$ Im-> setLine ($ xs, $ ys, $ xs + mt_rand (-$ s/2, $ s/2), $ ys + mt_rand (-$ s/2, $ s/2), $ color );
}
}

$ SESS = new session ();
$ SESS-> setParameter ('verifycode', $ code );

For ($ I = 0; $ I <200 * $ r; $ I ++ ){
$ Color = $ im-> setColor (mt_rand (0x66), mt_rand (0x66), mt_rand (0x66 ));
$ Im-> setPixel (mt_rand (0, $ w), mt_rand (0, $ h), $ color );
}

$ Im-> show ();

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.