1 function _code($_code_length = 4, $_width = 75, $_height = 25){
 2     for($i=0;$i<$_code_length;$i++){
 3         $_nmsg .= dechex(mt_rand(0,15));
 4     }
 5     $_SESSION["code"] = $_nmsg;
 6 
 7     $_img = imagecreatetruecolor($_width, $_height);
 8 
 9     $_white = imagecolorallocate($_img, 250, 250, 250);
10 
11     imagefill($_img, 0, 0, $_white);
12 
13     $_gray = imagecolorallocate($_img, 196, 196, 196);
14 
15     imagerectangle($_img, 0, 0, $_width-1, $_height-1, $_gray);
16 
17     for ($i=0; $i < 6; $i++) { 
18         $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
19         imageline($_img, mt_rand(0,$_width), mt_rand(0, $_height),mt_rand(0,$_width), mt_rand(0, $_height), $_md_color);
20     }
21 
22     for ($i=0; $i < 50; $i++) { 
23         $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
24         imagestring($_img, 1, mt_rand(1,$_width-5), mt_rand(1, $_height-5), "*", $_md_color);
25     }
26 
27     for ($i=0; $i < $_code_length ; $i++) { 
28         $_md_color = imagecolorallocate($_img, mt_rand(0,102), mt_rand(0,102), mt_rand(0,102));
29         imagestring($_img, 5, $i * $_width/$_code_length+ mt_rand(1, 10), mt_rand(1, $_height/2), $_SESSION["code"][$i], $_md_color);
30     }
31 
32     header("Content-Type:image/png");
33 
34     imagepng($_img);
35 
36     imagedestroy($_img);
37 }