/** 返回图片验证码 @param 验证码session中的名字 */ function image_code($session_name = 'image_code'){ header('content-type:image/gif'); //图片输出格式 $image_width = 80; // 图片大小 $image_height = 30; $text_x = 15; // 文字偏移量 $text_y = 25; $font_file = 'style/font/LiSu.ttf'; //字体 $font_size = 15; //字号 // 创建随机并获取随机字符串,去掉0,o,1,l,i 等不易识别的字符。 $codes = array('2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','t','u','v','w','z','y','z'); $code_num = array_rand($codes, 4); $code = ''; foreach ($code_num as $key => $value) { $code .= $codes[$value]; } // 放入把code加密并放入session中,根据需要选择加密方式 $encrypt_code = util_encrypt($code); session_set($session_name, $encrypt_code); $image = imagecreate($image_width, $image_height); //创建图片 $white = imagecolorallocate($image, 200, 200, 200); //灰色 $black = imagecolorallocate($image, 0, 0, 0); //白色 $red = imagecolorallocate($image, 255, 0, 0); //红色 imagefill($image, 0, 0, $white); //填充 // 把验证码添加到图片中 imagettftext($image, $font_size, 0, $text_x, $text_y, $black, $font_file, $code); // 加入噪点 for ($i = 0; $i < 80; $i++) { imagesetpixel($image, rand(0, 80), rand(0, 30), $black); imagesetpixel($image, rand(0, 80), rand(0, 30), $white); imagesetpixel($image, rand(0, 80), rand(0, 30), $red); } imagegif($image); imagedestroy($image); }