Because the registration code is often used during registration to prevent malicious machine registration, here I am posting a basic image that generates an image verification code, which is very simple. if you need it, refer to image. func. php.
<? Php require_once ('string. func. php '); function verifyImage ($ type = 1, $ length = 4, $ pixel = 0, $ line = 0, $ sess_name = "verify") {session_start (); /* define the length and width */$ width = 80; $ height = 30;/* Create a canvas */$ image = imagecreatetruecolor ($ width, $ height ); /* This function is used to match the color of the image for other drawing functions. The image parameter indicates the image handle. The red, green, and blue parameters are the three primary colors of color. The value ranges from 0 to 255 .... here I define black and white */$ white = imagecolorallocate ($ image, 255,255,255); $ black = imagecolorallocate ($ image, 0 ); /* This function colors the closed rectangular area of the image. Parameters x1, y1, x2, and y2 are the coordinate of the diagonal line of the rectangle, respectively. The col parameter indicates the color to be coated */imagefilledrectangle ($ image, 1, 1, $ width-2, $ height-2, $ white ); /* The buildRandomString function is used to generate a verification code */$ chars = buildRandomString ($ type, $ length ); /* send the verification code to the session to determine whether the user input is correct */$ _ SESSION [$ sess_name] = $ chars;/* define the Library */$ fontfiles = array ('msyh. ttf', 'msyhbd. ttf', 'simsun. ttc ', 'simyou. ttf', 'sthupo. ttf', 'stkaiti. ttf', 'stliti. ttf');/* use a loop to write the verification code to the image one by one */for ($ I = 0; $ I <$ length; $ I ++) {$ size = mt_rand (14,18 ); $ Angle = mt_rand (-15, 15);/* the horizontal and vertical coordinates of the verification code */$ x = 5 + $ I * $ size; $ y = mt_rand (20, 26 ); $ color = imagecolorallocate ($ image, mt_rand (50,190), mt_rand (50,200), mt_rand (); $ fontfile = ".. /font /". $ fontfiles [mt_rand (0, count ($ fontfiles)-1)]; $ text = substr ($ chars, $ I, 1 ); /* This function writes TTF (TrueType Fonts) text to an image */imagettftext ($ image, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text) ;}if ($ pixel) {for ($ I = 0; $ I <50; $ I ++) {/* This function can Draw a bit on the image. The x and y parameters are the coordinates of the point to be painted. The col parameter represents the color of the point */imagesetpixel ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ black) ;}if ($ line) {for ($ I = 0; $ I <10; $ I ++) {$ color = imagecolorallocate ($ image, mt_rand (50,90), mt_rand (50,200), mt_rand (50,90);/* draw line segments */imageline ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), mt_rand (0, $ width-1), mt_rand (0, $ height-1 ), $ color) ;}}/* output in gif format */header ("content-type: image/gif "); /* create a GIF image and output it to the webpage */imagegif ($ image);/* release the memory associated with the image */imagedestroy ($ image );}
String. func. php
<? Php function buildRandomString ($ type = 1, $ length = 4) {if ($ type = 1) {/* join function converts an array to a string .. The join () function is the alias of the implode () function */$ chars = join ("", range ();} elseif ($ type = 2) {/* array_merge function merge array */$ chars = join ("", array_merge (range ("a", "z"), range ("", "Z");} elseif ($ type = 3) {$ chars = join ("", array_merge (range ("a", "z "), range ("A", "Z"), range (0, 9);} if ($ length> strlen ($ chars )) {exit ("the string length is not enough");}/* disrupt the string */$ chars = str_shuffle ($ chars); return substr ($ chars, 0, $ length) ;}?>