Generate Verification Code
idea : First define the CAPTCHA function GetCode ()
Draw a verification code
$num = 4;//character length
GetCode ($num, 2);
1. Create canvas, assign color Imagecreatetruecolor ()
$height
$width = $num *20; Assume that each word has a size of 18
$im = Imagecreatetruecolor ($width, $height);//Create a true Color canvas
$BG = Imagecolorallocate ($im, Rand (200,250), Rand (250,255), rand (150, 255));//define Background color image
$color [] = Imagecolorallocate ($im, 240,240,240);//define Font Color "can be stored as an array, define some dark font"
2. Start Painting (everything is done on the canvas $im.)
Imagefill ($im, 0,0, $BG); Area fill (fills the background to the canvas)
Imagerectangle ($im, 0,0, $width-1, $height-1, $color [rand (0,3)]);//Define a border
Draw Verification Code: verbatim output
for ()
Imagettftext ($im, Rand (16,18), Rand ( -40,40), 8+ (18* $i), $color [Rand (0,3)], "MSYH.TTC", $str [$i]);
Random addition of interference points (points custom)
for () {
$c = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//Interference point color
Imagesetpixel ($im, rand (0, $width), rand (0, $height), $c);
Random addition of interference lines (number of lines self-defined)
for () {
$c = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//Interference Line Color
Imageline ($im, rand (0, $width), rand (0, $height), rand (0, $width), rand (0, $height), $c);
3. Output image
Header ("Content-type:image/png");//Set the response header (cannot have output before)
Imagepng ($im);
4. Destroying pictures
Imagedestroy ($im);
Custom function, get verification code
function GetCode ($m =4, $type =1)
{
$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$t = Array (9, strlen ($STR)-1);//Type Division
$c = "";
for ($i =0; $i < $m; $i + +)
$c. = $str [rand (0, $t [$type])];
Call verification code, onclick can achieve click image Refresh
php.20-Verification Code Generation