// 画图像 $im = imagecreatetruecolor($width, $height); // 定义要用到的颜色 $back_color = imagecolorallocate($im, 235, 236, 237); $boer_color = imagecolorallocate($im, 118, 151, 199); $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); // 画背景 imagefilledrectangle($im, 0, 0, $width, $height, $back_color); // 画边框 imagerectangle($im, 0, 0, $width - 1, $height - 1, $boer_color); // 画干扰线 for ($i = 0; $i < 5; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagearc($im, mt_rand(-$width, $width), mt_rand(-$height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color); } // 画干扰点 for ($i = 0; $i < 50; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color); } // 画验证码 @imagefttext($im, $size, 0, 5, $size + 3, $text_color, 'simsunb.ttf', $code); $_SESSION[$sess_name] = $code; header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png"); imagepng($im); imagedestroy($im); } session_start(); vcode(isset($_REQUEST["name"]) ? $_REQUEST["name"] : "VerifyCode", 4, 15); //4个数字,显示大小为15
//dechex — 十进制转换为十六进制 } //把生成的验证码保存在SESSION超级全局数组中 $_SESSION['captcha'] = $code; //创建画布 $img = imagecreatetruecolor($width, $high); //填充背景色为白色 $backcolor = imagecolorallocate($img, '255', '255', '255'); imagefill($img, '0', '0', $backcolor); //添加黑色边框 $bordercolor = imagecolorallocate($img, 0, 0, 0); imagerectangle($img, 0, 0, $width - 1, $high - 1, $bordercolor); //随机画线条 for ($i = 0; $i < $line_num; $i++) { imageline($img, mt_rand(0, $width * 0.1), mt_rand(0, $high), mt_rand($width * 0.9, $width), mt_rand(0, $high), imagecolorallocate($img, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255))); } //随机打雪花 for ($i = 0; $i < $snow_num; $i++) { imagechar($img, 1, mt_rand(0, $width), mt_rand(0, $high), '*', imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255))); } //画验证码 for ($b = 0; $b < strlen($_SESSION['captcha']); $b++) { imagechar($img, 5, $b * $width / $num + mt_rand(5, 10), mt_rand(2, $high / 2), $_SESSION['captcha'][$b], imagecolorallocate($img, mt_rand(10, 150), mt_rand(10, 150), mt_rand(0, 100))); } ob_clean(); //清空输出缓冲区 imagepng($img); imagedestroy($img); } vcode(85, 50, 5);
$im = imagecreatetruecolor($width, $height); // 准备颜色 $bgcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $interpixelcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $interlinecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); // 填充画板 imagefill($im, 0, 0, $bgcolor); // 准备文字 $strarr = array_merge(range(0, 9), range(a, z), range(A, Z)); shuffle($strarr); $str = join('', array_slice($strarr, 0, $vstrnum)); // 将验证码的值保存到session里 $_SESSION['vcode'] = $str; $angle = 0; //角度 imagettftext($im, $fontsize, $angle, $x, $y, $fontcolor, $fontfile, $str); for ($i = 0; $i < $interpixelnum; $i++) { imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $interpixelcolor); } for ($i = 0; $i < $interlinenum; $i++) { imageline($im, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $interlinecolor); } // 输出图片 header("content-type:image/png;charset=utf8"); imagepng($im); // 关闭资源 imagedestroy($im); } vcode(100, 30, 20, 25, 'fonts/simkai.ttf', 22, 0, 4, 300, 5);
<?php session_start(); include_once 'inc/vcode.inc.php'; $_SESSION['vcode'] = vcode(100, 40, 30, 4);