<?php
#在php.ini取消注释:php_gd2.dll,重启web服务器
//开启session会话
session_start();
$checkCode="";
for($i=0;$i<4;$i++){
$checkCode.=dechex(rand(1, 15));
}
//验证码保存到session用于验证
$_SESSION["checkCode"]=$checkCode;
//指定宽高画矩形
$image=imagecreatetruecolor(110, 32);
//定义一个颜色
$color=imagecolorallocate($image, 0, 0, 0);
//填充背景颜色
imagefill($image, 0, 0, $color);
//声明一些待用的颜色
$white=imagecolorallocate($image, 255, 255, 255);
$blue=imagecolorallocate($image, 0, 0, 255);
$red=imagecolorallocate($image, 255, 0, 0);
$green=imagecolorallocate($image, 255, 0, 0);
//画噪音线
for ($i=0;$i<20;$i++){
imageline($image, rand(0, 11), rand(0, 30), rand(0, 110),rand(0, 30),imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)));
}
//画文字
imagestring($image, rand(1,5), rand(2, 80), rand(2, 10), $checkCode, $white);
//指定输出图片格式
header("content-type: image/png");
//显示图片
imagepng($image);
?>
php 生成验证码
最新推荐文章于 2021-11-07 00:25:17 发布