Php implements the verification code function. the verification code function is everywhere. next we use php to implement the verification code function. here we extract the verification code into a class, if you use the verification code function later, you can directly introduce this type of file and create an instance of this type, you can use the verification code as follows:
Verification code file vcode. class. php
Width = $ width; $ this-> height = $ height; $ this-> num = $ num; $ this-> code = $ this-> createCode (); $ this-> pointnum= 100; $ this-> lineNum = 10; $ this-> fontFile = "STLITI. TTF ";}/*** is used to set member attributes * @ param string $ key member attribute name * @ param mixed $ value member attribute value * @ return object returns its own object $ this, it can be used for coherent operations */public function set ($ key, $ val) {// get_class_vars () to obtain an array composed of attributes in the class // get_class () returned object class name if (array_key_exists ($ key, get_class_vars (get_class ($ this) {$ this-> setOption ($ key, $ val );} return $ this;} // Set the private function setOption ($ key, $ value) {$ this-> $ key = $ value;} // Obtain the verification code public function getCode () {return $ this-> code;} // output Image public function outImg () {// create image $ this-> createImage (); // draw verification code $ this-> drawCode (); // draw interference elements $ this-> drawDisturbColor (); // output image $ this-> printImg ();} // draw the verification code private function drawCode () {$ this-> fontFile = ". /font /". $ this-> fontFile; for ($ I = 0; $ I <$ this-> num; $ I ++) {// Set the random color $ randColor = imagecolorallocate ($ this-> img, rand (0,128), rand (0,128), rand (0,128 )); // font size $ fontSize = rand (20, 23); // font horizontal position $ x = ($ this-> width/$ this-> num) * $ I; // horizontal position $ y = rand ($ fontSize, imagefontheight ($ fontSize) + 3); // draw the font imagettftext ($ this-> img, $ fontSize, 0, $ x, $ y, $ randColor, $ this-> fontFile, $ this-> code {$ I}) ;}// draw interference element private function drawDisturbColor () {// draw interference points for ($ I = 0; $ I <$ this-> pointNum; $ I ++) {// Set the random color $ randColor = imagecolorallocate ($ this-> img, rand (0,255), rand (0,255), rand (0,255 )); // Image point imagesetpixel ($ this-> img, rand (1, $ this-> width-2), rand (1, $ this-> height-2 ), $ randColor);} // draw the interference line for ($ I = 0; $ I <$ this-> lineNum; $ I ++) {// Set the random color $ randColor = imagecolorallocate ($ this-> img, rand (0,200), rand (0,200), rand (0,200 )); // draw line imageline ($ this-> img, rand (1, $ this-> width-2), rand (1, $ this-> height-2 ), rand (1, $ this-> height-2), rand (1, $ this-> width-2), $ randColor );}} // create an image private function createImage () {// create a true color image $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height ); // set the background color $ bgColor = imagecolorallocate ($ this-> img, rand (200,255), rand (200,255), rand (200,255 )); // fill in the background color imagefill ($ this-> img, $ bgColor); // set the border color $ borderColor = imagecolorallocate ($ this-> img, 0 ); // draw a border imagerectangle ($ this-> img, 0, $ this-> width-1, $ this-> height-1, $ borderColor );} // output image private function printImg () {if (imagetypes () & IMG_PNG) {// for pngheader ("Content-Type: image/png "); imagepng ($ this-> img);} else if (imagetypes () & IMG_JPG) {// for jpgheader ("Content-Type: image/jpeg "); imagejpeg ($ this-> img, null, 100);} else if (imagetypes () & IMG_GIF) {// for Gifheader ("Content-Type: image/gif "); imagegif ($ this-> img);} else if (imagetypes () & IMG_WBMP) {// for WBMPheader ('content-Type: image/vnd. wap. wbmp '); imagewbmp ($ this-> img);} else {die ('No image support in this PHP server'); }}// create a verification code private function createCode () {// Default string $ codes = "123456789 abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"; // Generate a verification code $ code = ""; for ($ I = 0; $ I <$ this-> num; $ I ++) {$ code. = $ codes {rand (0, strlen ($ codes)-1) };}return $ code;} // destructor used to destroy Image resources function _ destruct () {imagedestroy ($ this-> img );}}
Here, the function used to draw fonts is imagettftext (), because this function can customize the font style. it can be seen from the code that the passed parameters have a font file attribute, if you do not like this function, you can use the imagestring () function, but I personally think the default font size of this function is not good. Or the custom font looks more comfortable. Next we will call the verification code class.
GetCode (); // $ vcode-> set ("pointNum", 10); // number of custom interference points // $ vcode-> set ("lineNum ", 10); // number of custom interference lines // $ vcode-> set ("fontFile", "wawa. ttf "); // custom font file // output image $ vcode-> outImg ();
The verification code is implemented here. you can also view the verification code by directly calling this file. the following uses a simple logon form to use this verification code.
Alert ('verification code is correct! '); Script ";} else {echo"