Php image verification code class & lt ;? Php *** image verification code class * generates an image verification code. the verification code contains numbers and uppercase letters. the session stores the md5-encrypted verification code. ** usage: ** $ captchanewCatpcha (); * $ captcha-& gt; buildAndExportImage (); ** author: luoji php image verification code class
BuildAndExportImage (); ** prepared by: luojing * creation Time: 11:42:12 */class Captcha {private $ width; // width private $ height; // height private $ codeNum; // The number of characters in the verification code is private $ image; // the verification code image resource is private $ sessionKey; // The name saved in the session is private $ captcha; // the verification code string const charWidth = 10; // change the width of a single character based on the size of the output character/*** create a verification code class, initialize related parameters * @ param $ width image width * @ param $ height image height * @ param $ codeNum verification code character count * @ param $ sessionKey name saved in session */Function _ construct ($ width = 50, $ height = 20, $ codeNum = 4, $ sessionKey = 'captcha ') {$ this-> width = $ width; $ this-> height = $ height; $ this-> codeNum = $ codeNum; $ this-> sessionKey = $ sessionKey; // ensure the minimum height and width if ($ height <20) {$ this-> height = 20;} if ($ width <($ codeNum * self :: charWidth + 10) {// the left and right sides each retain 5 pixel gaps $ this-> width = $ codeNum * self: charWidth + 10 ;}} /*** construct and output the verification code Image */public function buildAndExpor TImage () {$ this-> createImage (); $ this-> setDisturb (); $ this-> setCaptcha (); $ this-> exportImage ();} /*** construct the image and set the background color */private function createImage () {// create the image $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height); // create the background color $ bg = imagecolorallocate ($ this-> image, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 )); // fill in the background color imagefilledrectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height- 1, $ bg);}/*** set interference element */private function setDisturb () {// Set interference point for ($ I = 0; $ I <150; $ I ++) {$ color = imagecolorallocate ($ this-> image, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 )); imagesetpixel ($ this-> image, mt_rand (5, $ this-> width-10), mt_rand (5, $ this-> height-3), $ color );} // Set the interference line for ($ I = 0; $ I <10; $ I ++) {$ color = imagecolorallocate ($ this-> image, mt_rand (150,220 ), mt_rand (150, 2 20), mt_rand (150,220); imagearc ($ this-> image, mt_rand (-10, $ this-> width), mt_rand (-10, $ this-> height), mt_rand (30,300), mt_rand (20,200), 55, 44, $ color );} // create a border color $ border = imagecolorallocate ($ this-> image, mt_rand (0, 50), mt_rand (0, 50), mt_rand (0, 50 )); // draw the border imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );} /*** generate and draw the verification code */private function setCaptcha () {$ str = '23 456789ABCDEFGHJKLMNPQRSTUVWXYZ '; // Generate the verification code character for ($ I = 0; $ I <$ this-> codeNum; $ I ++) {$ this-> captcha. =$ str {mt_rand (0, strlen ($ str)-1) };}// draw the verification code for ($ I = 0; $ I <strlen ($ this-> captcha); $ I ++) {$ color = imagecolorallocate ($ this-> image, mt_rand (0,200), mt_rand (0,200 ), mt_rand (0,200); $ x = floor ($ this-> width-10)/$ this-> codeNum ); $ x = $ x * $ I + floor ($ x-self: charWidth)/2) + 5; $ y = mt_rand (2, $ this-> Height-20); imagechar ($ this-> image, 5, $ x, $ y, $ this-> captcha {$ I}, $ color );}} /** output image, the verification code is saved to the session */private function exportImage () {if (imagetypes () & IMG_GIF) {header ('content-type: image/gif '); imagegif ($ this-> image);} else if (imagetypes () & IMG_PNG) {header ('content-type: image/png '); imagepng ($ this-> iamge);} else if (imagetypes () & IMG_JPEG) {header ('content-type: image/jpeg '); imagepng ($ this-> ia Mge);} else {imagedestroy ($ this-> image); die ("Don't support image type! ") ;}// Save the verification code information to the session, md5 encryption if (! Isset ($ _ SESSION) {session_start () ;}$ _ SESSION [$ this-> sessionKey] = md5 ($ this-> captcha ); imagedestroy ($ this-> image);} function _ destruct () {unset ($ this-> width, $ this-> height, $ this-> codeNum, $ this-> captcha );}}