- Class CCheckCodeFile
- {
- // Number of digits of the verification code
- Private $ mCheckCodeNum = 4;
- // Generated verification code
- Private $ mCheckCode = '';
- // Verification code Image
- Private $ mCheckImage = '';
- // Interference pixel
- Private $ mDisturbColor = '';
- // The image width of the verification code
- Private $ mCheckImageWidth = '80 ';
- // The image width of the verification code
- Private $ mCheckImageHeight = '20 ';
- /**
- *
- * @ Brief output header
- *
- */
- Private function OutFileHeader ()
- {
- Header ("Content-type: image/png ");
- }
- /**
- *
- * @ Brief generate the verification code
- *
- */
- Private function CreateCheckCode ()
- {
- $ This-> mCheckCode = strtoupper (substr (md5 (rand (), 0, $ this-> mCheckCodeNum ));
- Return $ this-> mCheckCode;
- }
- /**
- *
- * @ Brief generate a verification code Image
- *
- */
- Private function CreateImage ()
- {
- $ This-> mCheckImage = @ imagecreate ($ this-> mCheckImageWidth, $ this-> mCheckImageHeight );
- Imagecolorallocate ($ this-> mCheckImage, 200,200,200 );
- Return $ this-> mCheckImage;
- }
- /**
- *
- * @ Brief: sets the interference pixels of an image.
- *
- */
- Private function SetDisturbColor ()
- {
- For ($ I = 0; $ I <= 128; $ I ++)
- {
- $ This-> mDisturbColor = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,255), rand (0,255 ));
- Imagesetpixel ($ this-> mCheckImage, rand (2,128), rand (), $ this-> mDisturbColor );
- }
- }
- /**
- *
- * @ Brief: set the size of the verification code image.
- *
- * @ Param $ width
- *
- * @ Param $ height
- *
- */
- Public function SetCheckImageWH ($ width, $ height)
- {
- If ($ width = ''| $ height ='') return false;
- $ This-> mCheckImageWidth = $ width;
- $ This-> mCheckImageHeight = $ height;
- Return true;
- }
- /**
- *
- * @ Brief Upload the verification code one by one on the verification code image.
- *
- */
- Private function WriteCheckCodeToImage ()
- {
- For ($ I = 0; $ I <= $ this-> mCheckCodeNum; $ I ++)
- {
- $ Bg_color = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,128), rand (0,255 ));
- $ X = floor ($ this-> mCheckImageWidth/$ this-> mCheckCodeNum) * $ I;
- $ Y = rand (0, $ this-> mCheckImageHeight-15 );
- Imagechar ($ this-> mCheckImage, 5, $ x, $ y, $ this-> mCheckCode [$ I], $ bg_color );
- }
- }
- /**
- *
- * @ Brief outputs the verification code Image
- *
- */
- Public function OutCheckImage ()
- {
- $ This-> OutFileHeader ();
- $ This-> CreateCheckCode ();
- $ This-> CreateImage ();
- $ This-> SetDisturbColor ();
- $ This-> WriteCheckCodeToImage ();
- Imagepng ($ this-> mCheckImage );
- Imagedestroy ($ this-> mCheckImage );
- }
- }
- $ C_check_code_image = new CCheckCodeFile ();
- // $ C_check_code_image-> SetCheckImageWH (, 50); // you can specify the image size of the verification code.
- $ C_check_code_image-> OutCheckImage ();
- ?>
|