<img class="yzm" src="../../public/yzm.php" onclick="this.src='../../public/yzm.php?rand='+Math.random()">
../../public/yzm.php这个是验证码函数的文件,把生成的验证码图片加载进来。验证码函数如下:
<?php
session_start();
// 1.创建画布资源
$img=imagecreatetruecolor(105,50);
// 2.准备颜色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,0,125,125);
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
// 3.填充画布
imagefill($img,0,0,$green);
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$str=join(array_slice($arr,0,4));
//4.干扰素
for($i=0;$i<30;$i++){
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$color);
}
//5.画字
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagettftext($img,20,0,20,35,$color,'font/comicbd.ttf',$str);
$_SESSION['yzm'] = !empty($_SESSION['yzm']) ? $_SESSION['yzm'] : null;
$_SESSION['yzm']=$str;
// 6.输出最终图像或保存最终图像
header('content-type:image/png');// 图片从浏览器上输出
imagepng($img);// 把图片保存到本地
// 7.释放画布资源
imagedestroy($img);
?>