<?php
/**
* 安全的验证码
* @author 流水孟春 cmpan#qq.com
*
*/
header("Cache-control: private");
header("content-type: image/png");
$length = isset($_GET['length']) ? $_GET['length'] : 4; // 校验码长度/字符
// 生成校验码
//function confirmCode($length){
$codeSet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&';
$imageX = mt_rand(1, 25);
$imageY = mt_rand(1, 25);
$imageL = $imageX + $length*10 + 4; // 图片宽度
$imageH = $imageY + 20; // 图片高度
$noiseNum = 10*$length; // 杂点数量
$lineNum = $length - 3; // 干扰线数量
// 建立一幅 $imageL x $imageH 的图像
$image = imagecreate($imageL, $imageH);
$bg = imagecolorallocate($image, 255, 255, 255); // 白色背景
$textColor = imagecolorallocate($image, 0, 0, 255); // 蓝色文本
$rectangleColor = imagecolorallocate($image, 0, 168, 255); // 边框颜色
$noiseColor = imagecolorallocate($image, 0, 0, 255); // 杂点颜色
$lineColor = imagecolorallocate($image, 0, 0, 255); // 干扰线颜色
$code = ''; // 校验码
for ($i = 0; $i<$length; $i++) $code .= $codeSet[mt_rand(0, 41)];
$_SESSION['confirmCode'] = $code; // 把校验码保存到session
imagestring($image, 5, $imageX, $imageY, $code, $textColor); // 把校验码写入图像
//imagerectangle($image, 0, 0, $imageL-1, $imageH-1, $rectangleColor); // 添加边框
//加入干扰线
for($i=0; $i<$lineNum; $i++){
imageline($image, mt_rand(0, $imageL), mt_rand(0, $imageH),
mt_rand(0, $imageL), mt_rand(0, $imageH), $lineColor);
}
//加入杂点
for($i=0;$i<$noiseNum;$i++)
imagesetpixel($image,mt_rand(0,$imageL),mt_rand(0,$imageH),$noiseColor);
imagepng($image); // 输出图像
imagedestroy($image);
<?php
session_start();
header("Cache-control: private");
header("content-type: image/png");
$length = isset($_GET['length']) ? $_GET['length'] : 4; // 校验码长度/字符
// 生成校验码
//function confirmCode($length){
$codeSet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';//
$imageX = mt_rand(1, 25); // 左边距
$imageY = mt_rand(1, 25); // 上边距
$imageL = 60 + $length*10 + 4; // 图片宽度
$imageH = 50; // 图片高度
$noiseNum = 200*$length; // 杂点数量
$lineNum = $length - 2; // 干扰线数量
// 建立一幅 $imageL x $imageH 的图像
$image = imagecreate($imageL, $imageH);
$bg = imagecolorallocate($image, 255, 255, 255); // 白色背景
$textColor = imagecolorallocate($image, mt_rand(10,100), mt_rand(50,150), mt_rand(100,200)); // 随机颜色文本
$rectangleColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); // 边框颜色
$noiseColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); // 杂点颜色
$lineColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰线颜色
$code = ''; // 校验码
for ($i = 0; $i<$length; $i++) {
$tmp = $codeSet[mt_rand(0, 36)];
$code .= $tmp;
imagestring($image, 5, $imageX+$i*15, $imageY+ mt_rand(-4,4), $tmp, imagecolorallocate($image, mt_rand(100,140), mt_rand(120,150), mt_rand(150,180))); // 把校验码写入图像
}
$_SESSION['confirmCode'] = $code; // 把校验码保存到session
//imagerectangle($image, 0, 0, $imageL-1, $imageH-1, $rectangleColor); // 添加边框
//加入干扰线
for($i=0; $i<$lineNum; $i++){
imageline($image, mt_rand(1, $imageL), mt_rand(1, $imageH), mt_rand(1, $imageL), mt_rand(1, $imageH), $lineColor);
}
//加入杂点
for($i=0;$i<$noiseNum;$i++)
imagesetpixel($image,mt_rand(1,$imageL),mt_rand(1,$imageH),imagecolorallocate($image, mt_rand(200,255), mt_rand(180,255), mt_rand(190,255)));
imagepng($image); // 输出图像
imagedestroy($image);
?>