PHP verification code function implementation/*** generate verification code image */publicfunctionactionVerfiycode () {Header ("Content-type: image/gif"); $ border = 0; PHP verification code
/*** Generate verification code image */public function actionVerfiycode () {Header ("Content-type: image/gif"); $ border = 0; // Border 1 required: 0 not $ how = 4; // Number of digits of the verification code $ w = $ how * 15; // image width $ h = 20; // Image Height $ fontsize = 5; // font size $ alpha = "abcdefghijkmnopqrstuvwxyz"; // verification code content 1: letter $ number = "0123456789"; // verification code content 2: number $ randcode = ""; // verification code string initialization srand (double) microtime () * 1000000); // initialize random seed $ im = ImageCreate ($ w, $ h); // Create a verification image $ bgcolo R = ImageColorAllocate ($ im, 255,255,255); // set the background color ImageFill ($ im, 0, 0, $ bgcolor); // fill the background color if ($ border) {$ black = ImageColorAllocate ($ im, 0, 0, 0); // set the border color ImageRectangle ($ im, 0, 0, $ w-1, $ h-1, $ black); // draw a border} for ($ I = 0; $ I <$ how; $ I ++) {$ alpha_or_number = mt_rand (0, 1 ); // letter or number $ str = $ alpha_or_number? $ Alpha: $ number; $ which = mt_rand (0, strlen ($ str)-1); // specify the character $ code = substr ($ str, $ which, 1); // get the character $ j =! $ I? 4: $ j + 15; // The position of the painted character $ color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100 )); // the color of the character ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw the character $ randcode. = $ code; // add the verification code string to the string by bit} for ($ I = 0; $ I <5; $ I ++) // paint the background interference line {$ color1 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); // interference line color ImageArc ($ im, mt_rand (-5, $ w), mt_rand (-5, $ h), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line} for ($ I = 0; $ I <$ how * 40; $ I ++) // paint background interference points {$ color2 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); // disturbance point color ImageSetPixel ($ im, mt_rand (0, $ w), mt_rand (0, $ h), $ color2 ); // interference point} $ session = new CHttpSession; $ session-> open (); Yii: app ()-> session-> add ('randcode', $ randcode ); /* drawing end */Imagegif ($ im); ImageDestroy ($ im);/* drawing end */}
From: http://www.admin5.com/article/20080314/75984.shtml