Copy Code code as follows:
<?php
/** 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);
Drawing 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);
}
To deposit the verification code in $_session
Sess ("verify", $verify);
Output picture and release cache
Header (' content-type:image/png ');
Imagepng ($im);
Imagedestroy ($im);
}
};
?>