This article illustrates the method of generating image verification code in PHP. Share to everyone for your reference, specific as follows:
1. Generate addition operation Verification code picture
Session_Start ()/* Definition header file for picture/header ("Content-type:image/png");/* Generate Verification code * *
Create a picture set the font color/* $im = Imagecreate ($w, $h);
$red = Imagecolorallocate ($im, 255, 255, 255);
$white = Imagecolorallocate ($im, 255, 255, 255);
* * Randomly generated two digits/$num 1 = rand (1, 20);
$num 2 = rand (1, 20);
$_session ["administratorconfirmcode"] = $num 1+ $num 2;
/* Set Picture background color */$gray = Imagecolorallocate ($im, 118, 151, 199);
$black = Imagecolorallocate ($im, Mt_rand (0), Mt_rand (0), Mt_rand (0, 100));
/* Create picture background * * * Imagefilledrectangle ($im, 0, 0, $black);
* * To generate a large number of random points on the canvas/for ($i = 0; $i < $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $gray);/* Write the calculation captcha to the picture.
Imagestring ($im, 5, 5, 4, $num 1, $red);
Imagestring ($im, 5, 3, "+", $red);
Imagestring ($im, 5, 4, $num 2, $red);
Imagestring ($im, 5, 3, "=", $red);
Imagestring ($im, 5, 2, "?", $white);
/* Output Picture * * Imagepng ($IM);
Imagedestroy ($im);
2, generate character verification code picture "It is worth noting where the font, you need to introduce the actual font path, otherwise, may appear the image can not display the verification code"
Session_Start ();
/* Set file header for picture output */Header ("Content-type:image/jpeg");
/* Call Generate validation code function * * $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789";
$result = "";
for ($i =0; $i < $length; $i + +) {$num [$i]=rand (0,61);
$result. = $str [$num [$i]];
} $text = $result;
$_session ["administratorconfirmcode"] = $text;
/* Set the width and height of the picture * * * $im _x = $w;
$im _y = $y;
/* Create picture * * * $im = Imagecreatetruecolor ($im _x, $im _y);
$text _c = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));
$tmpC 0=mt_rand (100,255);
$tmpC 1=mt_rand (100,255);
$tmpC 2=mt_rand (100,255);
$buttum _c = imagecolorallocate ($im, $tmpC 0, $tmpC 1, $tmpC 2);
Imagefill ($im, $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, $text _c, $font, $tmp); /* Write characters to File/* $distortion _im = Imagecreatetruecolor ($im _x,$im _y);
Imagefill ($distortion _im, $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) *
>=0) {imagesetpixel ($distortion _im, (int) ($i +10+sin ($j/$im _y*2*m_pi-m_pi*0.1) *4), $j, $rgb);
}}/* The 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);
$rand 1 = mt_rand (15,25);
$rand 2 = Mt_rand (5,10);
for ($yy = $rand; $yy <=+ $rand +2; $yy + +) {for ($px =-80; $px <=80; $px = $px +0.1) {$x = $px/$rand 1;
if ($x!=0) {$y =sin ($x);
$py = $y * $rand 2;
Imagesetpixel ($distortion _im, $px +80, $py + $yy, $text _c); }/* To export the image to the browser/imagepng in PNG format ($distortioN_IM);
/* Destroy image * * Imagedestroy ($distortion _im);
Imagedestroy ($im);
More about PHP Interested readers can view the site topics: "PHP graphics and pictures Operating skills summary", "PHP Mathematical Calculation Skills Summary", "PHP coding and transcoding Operation skills Summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.