Share the image verification code file written in PHP ,. PHP-compiled image verification code file sharing, applicable to custom verification code! Php ** Tochangethislicenseheader, chooseLicenseHeadersinProjectProperties. * share the image verification code file written in Tochange PHP,
Applicable to custom verification codes!
<? Php/** To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */Class Image {private $ img; public $ width = 85; public $ height = 25; public $ code; public $ code_len = 4; public $ code_str = "Hangzhou "; public $ bg_color = '# DCDCDC'; public $ font_size = 16; public $ font = 'font. ttf'; public $ font_color = '#000000'; // Create a verification code hunger character to create public function create_code () {$ code = ''; for ($ I = 0; $ I <$ this-> code_len; $ I ++) {$ code. = $ this-> code_str [mt_rand (0, strlen ($ this-> code_str)-1)];} return $ this-> code = $ code ;} // output Image public function getImage () {$ w = $ this-> width; $ h = $ this-> height; $ bg_color = $ this-> bg_color; $ img = imagecreatetruecolor ($ w, $ h); $ bg_color = imagecolorallocate ($ img, hexdec (substr ($ bg_color, 1, 2), hexdec (substr ($ bg_color, 3, 2), hexdec (substr ($ bg_color, 5, 2); imagefill ($ img, 0, 0, $ bg_color); $ this-> img = $ img; $ this-> create_font (); $ this-> create_pix (); $ this-> show_code ();} // write the verification code public function create_font () {$ this-> create_code (); $ color = $ this-> font_color; $ font_color = imagecolorallocate ($ this-> img, hexdec (substr ($ color, 1, 2 )), hexdec (substr ($ color, 3, 2), hexdec (substr ($ color, 5, 2); $ x = $ this-> width/$ this-> code_len; for ($ I = 0; $ I <$ this-> code_len; $ I ++) {$ txt_color = imagecolorallocate ($ this-> img, mt_rand (0,100 ), mt_rand (0,150), mt_rand (0,200); imagettftext ($ this-> img, $ this-> font_size, mt_rand (-30, 30 ), $ x * $ I + mt_rand (3, 6), mt_rand ($ this-> height/1.2, $ this-> height), $ txt_color, $ this-> font, $ this-> code [$ I]); // imagestring ($ this-> img, $ this-> font_size, $ x * $ I + mt_rand (3, 6 ), mt_rand (0, $ this-> height/4), $ this-> code [$ I], $ font_color) ;}$ this-> font_color = $ font_color ;} // draw the interference line public function create_pix () {$ pix_color = $ this-> font_color; for ($ I = 0; $ I <100; $ I ++) {imagesetpixel ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ pix_color );} for ($ j = 0; $ j <4; $ j ++) {imagesetthickness ($ this-> img, mt_rand (1, 2 )); imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ pix_color) ;}// get the verification code public function getCode () {return strtoupper ($ this-> code);} // output the verification code private function show_code () {header ("Content-type: image/png "); imagepng ($ this-> img); imagedestroy ($ this-> img );}}
Let's take a look at the code:
Generate an image verification code. the verification code contains numbers and uppercase letters, and the md5 encrypted verification code is stored in the session.
<? 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: * $ captcha = new Catpcha (); * $ captcha-> buildAndExportImage (); ** author: luojing * creation Time: 11:42:12 */class Captcha {private $ width; // width private $ height; // height private $ codeNum; // Number of verification code characters private $ image; // verification code image Resource private $ sessionKey; // The name private $ captcha saved in the session; // verification code string const charWidth = 10; // The width of a single character, which is changed based on the size of the output character/*** creates 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 buildAndExportImage () {$ this-> createImage (); $ this-> setDisturb (); $ this-> setCaptcha (); $ this-> exportImage ();}/*** construct an image and set the background color */private function createImage () {// create an image $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height ); // create a 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);}/*** sets the interference element */private function setDisturb () {// sets the 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,220), 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 a verification code */private function setCaptcha () {$ str = '23456789abcdefghjklmnpqrstuvwxyz '; // 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-> iamge);} 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 );}}
Example 3:
<? Php class ValidationCode {private $ width; private $ height; private $ codeNum; private $ image; // image Resource private $ disturbColorNum; private $ checkCode; function _ construct ($ width = 80, $ height = 20, $ codeNum = 4) {$ this-> width = $ width; $ this-> height = $ height; $ this-> codeNum = $ codeNum; $ this-> checkCode = $ this-> createCheckCode (); $ number = floor ($ width * $ height/15 ); if ($ number> 240-$ codeNum) {$ this-> disturbColorNum = 240-$ codeNum;} else {$ this-> disturbColorNum = $ number ;}} // output the image function showImage ($ fontFace = "") to the browser by accessing this method {// Step 1: create the image background $ this-> createImage (); // Step 2: Set the interference element $ this-> setDisturbColor (); // Step 3: randomly draw the text $ this-> outputText ($ fontFace) to the image ); // Step 4: output the image $ this-> outputImage ();} // call this method to obtain the randomly created verification code string function getCheckCode () {return $ this-> checkCode;} private function createImage () {// create an image resource $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height); // random background color $ backColor = imagecolorallocate ($ this-> image, rand (225,255), rand (225,255), rand (225,255 )); // add the background color imagefill ($ this-> image, 0, 0, $ backColor); // set the border color $ border = imagecolorallocate ($ this-> image, 0, 0, 0); // draw the rectangular border imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border);} private function setDisturbColor () {for ($ I = 0; $ I <$ this-> disturbColorNum; $ I ++) {$ color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255); imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color) ;}for ($ I = 0; $ I <10; $ I ++) {$ color = imagecolorallocate ($ this-> image, rand (200,255), rand (200,255), rand (200,255 )); imagearc ($ this-> image, rand (-10, $ this-> width), rand (-10, $ this-> height), rand (30,300 ), rand (20,200), 55, 44, $ color) ;}} private function createCheckCode () {// here the random code is mainly generated, from 2 to distinguish 1 from l $ code = "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ"; $ string = ''; for ($ I = 0; $ I <$ this-> codeNum; $ I ++) {$ char = $ code {rand (0, strlen ($ code)-1)}; $ string. = $ char;} return $ string;} private function outputText ($ fontFace = "") {for ($ I = 0; $ I <$ this-> codeNum; $ I ++) {$ fontcolor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 )); if ($ fontFace = "") {$ fontsize = rand (3, 5); $ x = floor ($ this-> width/$ this-> codeNum) * $ I + 3; $ y = rand (0, $ this-> height-15); imagechar ($ this-> image, $ fontsize, $ x, $ y, $ this-> checkCode {$ I}, $ fontcolor);} else {$ fontsize = rand (12, 16 ); $ x = floor ($ this-> width-8)/$ this-> codeNum) * $ I + 8; $ y = rand ($ fontSize + 5, $ this-> height); imagettftext ($ this-> image, $ fontsize, rand (-30, 30), $ x, $ y, $ fontcolor, $ fontFace, $ this-> checkCode {$ I}) ;}} private function outputImage () {if (imagetypes () & IMG_GIF) {header ("Content-Type: image/gif "); imagepng ($ this-> image);} else if (imagetypes () & IMG_JPG) {header (" Content-Type: image/jpeg "); imagepng ($ this-> image);} else if (imagetypes () & IMG_PNG) {header ("Content-Type: image/png "); imagepng ($ this-> image);} else if (imagetypes () & IMG_WBMP) {header ("Content-Type: image/vnd. wap. wbmp "); imagepng ($ this-> image);} else {die (" PHP does not support image creation ") ;}} function _ destruct () {imagedestroy ($ this-> image );}}
Use:
Test and call verification code
Code. php
<? Phpsession_start (); include "validationcode. class. php "; $ code = new ValidationCode (80, 20, 4); $ code-> showImage (); // output to the page for registration or login $ _ SESSION ["code"] = $ code-> getCheckCode (); // Save the verification code to the server
Authorization, applicable to the custom verification code class! Php/** To change this license header, choose License Headers in Project Properties. * To change...