Summary of methods for generating image verification codes using PHP (two methods) and summary of verification Codes
This article describes how PHP generates an image verification code. We will share this with you for your reference. The details are as follows:
1. generate an image of the addition Verification Code
Session_start ();/* define the header file as an image */header ("Content-type: image/png "); /* generate Verification Code * // * create an image to set the font color */$ im = imagecreate ($ w, $ h); $ red = imagecolorallocate ($ im, 255,255,255 ); $ white = imagecolorallocate ($ im, 255,255,255);/* randomly generate two numbers */$ num1 = rand (1, 20); $ num2 = rand (1, 20 ); $ _ SESSION ["administratorConfirmCode"] = $ num1 + $ num2;/* set the image background color */$ gray = imagecolorallocate ($ im, 118,151,199 ); $ black = imagecolorallocate ($ im , Mt_rand (0,100), mt_rand (0,100), mt_rand (0,100);/* create an image background */imagefilledrectangle ($ im, 0, 0,100, 24, $ black ); /* randomly generate a large number of points on the canvas */for ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ im, rand (0, $ w), rand (0, $ h), $ gray);}/* write the verification code to the image */imagestring ($ im, 5, 5, 4, $ num1, $ red); imagestring ($ im, 5, 30, 3, "+", $ red); imagestring ($ im, 5, 45, 4, $ num2, $ red); imagestring ($ im, 5, 70, 3, "=", $ red); imagestring ($ im, 5, 80, 2 ,"? ", $ White);/* output image */imagepng ($ im); imagedestroy ($ im );
2. Generate a character Verification Code image. [It is worth noting where the font is. Introduce the actual font path. Otherwise, the image may not display the verification code]
Session_start ();/* set the file Header to image output */Header ("Content-type: image/JPEG "); /* call the verification code generation function */$ str = "success"; $ result = ""; for ($ I = 0; $ I <$ length; $ I ++) {$ num [$ I] = rand (0, 61); $ result. = $ str [$ num [$ I];} $ text = $ result; $ _ SESSION ["administratorConfirmCode"] = $ text; /* set the image width and height */$ im_x = $ w; $ im_y = $ y;/* create an image */$ im = imagecreatetruecolor ($ im_x, $ im_y ); $ text_c = ImageColor Allocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); $ tmpC0 = mt_rand (100,255); $ tmpC1 = mt_rand (100,255 ); $ tmpC2 = maid (100,255); $ buttum_c = ImageColorAllocate ($ im, $ tmpC0, $ tmpC1, $ tmpC2); imagefill ($ im, 16, 13, $ buttum_c ); /* font file */$ font = _ WEB_DIR _. '/font/comic. ttf'; for ($ I = 0; $ I <strlen ($ text); $ I ++) {$ tmp = substr ($ text, $ I, 1 ); $ array = array (-1, 1); $ p = array_rand ($ array); $ an = $ array [$ p] * mt_rand (1, 10); // angle $ size = 28; imagettftext ($ im, $ size, $ an, 15 + $ I * $ size, 35, $ text_c, $ font, $ tmp);}/* write characters to the file */$ distortion_im = imagecreatetruecolor ($ im_x, $ im_y); imagefill ($ distortion_im, 16, 13, $ buttum_c ); for ($ I = 0; $ I <$ im_x; $ I ++) {for ($ j = 0; $ j <$ im_y; $ j ++) {$ rgb = imagecolorat ($ im, $ I, $ j); if (int) ($ I + 20 + sin ($ j/$ im_y * 2 * M_PI) * 10) <= imagesx ($ distortion_im) & (int) ($ I + 20 + sin ($ j/$ im_y * 2 * M_PI )* 10)> = 0) {imagesetpixel ($ distortion_im, (int) ($ I + 10 + sin ($ j/$ im_y * 2 * M_PI-M_PI * 0.1) * 4 ), $ j, $ rgb) ;}}/ * Number of interference element points */$ count = 160;/* create interference element points */for ($ I = 0; $ I <$ count; $ I ++) {$ randcolor = ImageColorallocate ($ distortion_im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); imagesetpixel ($ distortion_im, mt_rand () % $ im_x, mt_rand () % $ im_y, $ randcolor);}/* create interference lines */$ rand = mt_rand (5, 30 ); $ rand1 = mt_rand (15, 25); $ Rand2 = mt_rand (5, 10); for ($ yy = $ rand; $ yy <= + $ rand + 2; $ yy ++) {for ($ px =-80; $ px <= 80; $ px = $ px + 0.1) {$ x = $ px/$ rand1; if ($ x! = 0) {$ y = sin ($ x) ;}$ py = $ y * $ rand2; imagesetpixel ($ distortion_im, $ px + 80, $ py + $ yy, $ text_c) ;}}/* output the image to the browser in PNG format */ImagePNG ($ distortion_im);/* destroy the image */ImageDestroy ($ distortion_im ); imageDestroy ($ im );