时间:2021-07-01 10:21:17 帮助过:4人阅读
class auth{
public function auth()
{
session_start(); //启动session
$str="abcdefghijkmnpqrstuvwxyz23456789"; /*生成一个4位随机数 */
for($i=0;$i<4;$i++){
$num.=substr($str,rand(0,29),1);
}
$_SESSION['code']=$num; //将随机数保存到session中
$im=imagecreate(60,25); //创建一个60*20的图像
$white=ImageColorAllocate($im,255,255,255); //设置图像的背景色为白色
$blue=ImageColorAllocate($im,0,0,255); //设置图像中文本颜色为蓝色
/* 将多个颜色不同的*号添加到图像中 */
for($i=1;$i<200;$i++){
$x=rand(1,60-9);
$y=rand(1,20-6);
$color=imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
imagechar($im,1,$x,$y,"*",$color);
}
/*将4位随机数添加到图像中,添加的位置不固定 */
$strx=rand(3,8);
for($i=0;$i<4;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($num,$i,1),$blue);
$strx+=rand(8,12);
}
header ("Content-type: image/gif"); //设置输出图像的格式
imagegif($im); //输出图像
imagedestroy($im); //释放图像资源
//return $_SESSION['code'];
exit;
}
}
$auth = new auth();
验证码: ![]()
点击图片刷新