Copy CodeThe code is as follows:
/** Default Home **/
Class Defaultcontroller extends AppController
{
Public Function index () {
$len = 5;
$str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789";
$im = Imagecreatetruecolor (70, 20);
$BGC = Imagecolorallocate ($im, 255, 255, 255);
$bgtxt = Imagecolorallocate ($im, 220, 220, 220);
Random Palette
$colors = Array (
Imagecolorallocate ($im, 255, 0, 0),
Imagecolorallocate ($im, 0, 200, 0),
Imagecolorallocate ($im, 0, 0, 255),
Imagecolorallocate ($im, 0, 0, 0),
Imagecolorallocate ($im, 255, 128, 0),
Imagecolorallocate ($im, 255, 208, 0),
Imagecolorallocate ($im, 98, 186, 245),
);
Fill background color
Imagefill ($im, 0, 0, $BGC);
Get numbers randomly
$verify = "";
while (strlen ($verify) < $len) {
$i = strlen ($verify);
$random = $str [rand (0, strlen ($STR))];
$verify. = $random;
Draw background text
Imagestring ($im, 6, ($i *10) +3, rand (0,6), $random, $bgtxt);
Draw Main text information
Imagestring ($im, 6, ($i *10) +3, rand (0,6), $random, $colors [rand (0, COUNT ($colors)-1)]);
}
Add Random noise
for ($i =0; $i <100; $i + +) {
$color = Imagecolorallocate ($im, Rand (50,220), Rand (50,220), Rand (50,220));
Imagesetpixel ($im, Rand (0,70), Rand (0,20), $color);
}
Save verification Code in $_session
Sess ("verify", $verify);
Output a picture and release the cache
Header (' content-type:image/png ');
Imagepng ($im);
Imagedestroy ($im);
}
};
?>
The above introduces the book Management system source code PHP generated random verification code image code, including the book Management system source code aspects, I hope the PHP tutorial interested in a friend helpful.