<?php
check_code();
//随机生成验证码的字符或数字
function check_code($width=100, $height=50,$num=4, $type='jpeg')
{
$i=imagecreate($width,$height);
$string='';
for($j=0;$j<$num;$j++)
{
$rand=mt_rand(0,2);
switch($rand)
{
case 0:
$ascii=mt_rand(48,57);
break;
case 1:
$ascii=mt_rand(65,90);
break;
case 2:
$ascii=mt_rand(97,122);
break;
}
$string.=sprintf('%c' ,$ascii);//ascii字符解码
}
//生成验证码背景颜色(randbg():函数调用)
imagefilledrectangle($i,0,0,$width,$height,randbg($i));
//生成随机干扰(randpix():函数调用)
for($j=0;$j<50;$j++)
{
imagesetpixel($i,mt_rand(0,$width),mt_rand(0,$height) ,randpix($i));
}
//写字
for($j=0;$j<$num;$j++)
{
$x=floor($width/$num)*$j+2;
$y=mt_rand(3,$height-15);
imagechar($i,5,$x,$y,$string[$j],randpix($i));
}
//图片格式
$fuc='image'.$type;
$have='cotent-type:image'.$type;
if(function_exists($fuc))
{
header($have);
$fuc($i);
}
else
{
echo '不支持图片类型';
}
imagedestroy($i);
return $string;
}
//背景颜色函数功能模块
function randbg($i)
{
return imagecolorallocate($i,mt_rand(135,255) ,mt_rand(135,255),mt_rand(135,255));
}
//干扰元素或字符颜色函数功能模块
function randpix($i)
{
return imagecolorallocate($i,mt_rand(0,135) ,mt_rand(0,135),mt_rand(0,135));
}
?>