This article mainly introduces the verification code file class implemented by php. The example analyzes the skills of php to generate the verification code file, which is of great practical value, you can refer to the example in this article to describe the verification code file class implemented by php. Share it with you for your reference. The details are as follows:
<? Php/*** @ file * @ version 1.0 * @ author @ * @ brief verification code file class **/class CCheckCodeFile {// Number of verification code digits private $ mCheckCodeNum = 4; // The generated verification code is private $ mCheckCode = ''; // the verification code picture is private $ mCheckImage =''; // The interference pixel is private $ mDisturbColor = ''; // the image width of the verification code is private $ mCheckImageWidth = '80'; // the image width of the verification code is private $ mCheckImageHeight = '20 '; /***** @ brief output header **/private function OutFileHeader () {header ("Content-type: image/png");}/***** @ bri Ef generates the verification code **/private function CreateCheckCode () {$ this-> mCheckCode = strtoupper (substr (md5 (rand (), 0, $ this-> mCheckCodeNum )); return $ this-> mCheckCode;}/***** @ brief generate 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 the image. **/p Rivate 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 sets the size of the verification code Image ** @ param $ width ** @ param $ height **/public function SetCheckImageWH ($ width, $ height) {if ($ width = ''| $ height ='') return false; $ t His-> 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 output 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-> SetChec KImageWH (); // set the size of the image displaying the verification code $ c_check_code_image-> OutCheckImage ();?>
I hope this article will help you with php programming.