<PHP>图像验证码

这篇博客介绍了如何使用PHP创建一个包含随机4位字符的图像验证码。首先,通过array_merge和shuffle函数生成并打乱字符数组,选取前4位作为验证码。接着,创建一个空画布,分配背景和字体颜色,绘制矩形,使用TTF字体写入验证码。然后添加干扰像素点,并最终输出PNG格式的图像。最后,博客提供了源代码链接。

PHP生成一个随机4个字符的图像验证码

源代码获取:
https://github.com/akh5/PHP/blob/master/identify_code.php

在这里插入图片描述

生成随机4位字符串

//将数字字母合并到一个数组
$arr = array_merge(range('A','Z'),range('a','z'),range('0','9'));
shuffle($arr); //打乱数组
//选出数组的前四位作为验证码
for($i=0;$i<4;$i++)
{
    $str.=$arr[$i];
}
  • 通过array_merge函数将范围为A~Z,a~z,0~9的数组合为一个数组
  • shuffle打乱数组
  • 最后选取数组的前2位作为验证码,字符串拼接

创建一个空画布,并分配颜色

//声明浏览器格式
header("Content-Type:image/png");
//创建画布
$width = 150;
$height = 50;
$img = imagecreatetruecolor($width,$height);
//设置随机颜色颜色
$color1 = imagecolorallocate($img,mt_rand(0,100),mt_rand(50,150),mt_rand(100,200));
$color2 = imagecolorallocate($img,mt_rand(150,255),mt_rand(150,255),mt_rand(150,255));
  • 声明浏览器格式
  • imagecreatetruecolor创建画布,设定宽高
  • imagecolorallocate设定随机颜色,color1深色作为背景,color2浅色作为字体颜色

绘制矩形,并往图像上写入TTF字体字符串

//ttf字体绝对路径
$fontpath = "E:/wamp64/www/day4/font/msyh.ttc";
//在画布中绘制矩形
imagefilledrectangle($img,0,0,$width,$height,$color1);
//添加ttf字体,生成随机的验证码
imagettftext($img,28,0,20,40,$color2,$fontpath,$str);
  • 设定TTF字体的绝对路径
  • imagefilledrectangle绘制一个矩形填充画布
  • imagettftext添加字体,并调整字体的位置,倾斜角度

生成像素点干扰,输出图像

//生成许多个像素点作为干扰
for($i=0;$i<200;$i++)
{
    $color3 = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$color3);
}

//显示图像
imagepng($img);

//销毁图像
imagedestroy($img);

  • 循环输出像素点
  • 显示png格式图像
  • 销毁图像
<?php session_start();// 开启session require("afunctions.php");// 引入函数 checkLogined();// 判断管理员是否登录 ?> <!doctype html> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> <title>后台管理</title> <link rel="stylesheet" href="css/mystyle.css" type="text/css"/> </head> <body> <div class="admin_head"> <h3>后台管理系统</h3> </div> <div class="admin_content"> <!--左侧列表--> <div class="menu"> <div class="cont"> <div class="title">管理员</div> <ul class="mList"> <li> <h3><span>+</span>用户管理</h3> <dl> <dd><a class='pageA' href="admin_user/addUser.php" target="mainFrame">添加用户</a></dd> <dd><a class='pageA' href="admin_user/userList.php" target="mainFrame">用户列表</a></dd> </dl> </li> <li> <h3><span>+</span>商品管理</h3> <dl> <dd><a class='pageA' href="admin_goods/addGoods.php" target="mainFrame">添加商品</a></dd> <dd><a class='pageA' href="admin_goods/goodsList.php" target="mainFrame">商品列表</a></dd> </dl> </li> <li> <h3><span>+</span>订单管理</h3> <dl> <dd><a class='pageA' href="admin_order/orderList.php" target="mainFrame">订单列表</a></dd> </dl> </li> <li> <h3><span>+</span>系统管理</h3> <dl> <dd><a class='pageA' href="index.php">返回首页</a></dd> </dl> <dl> <dd><a class='pageA' href="loginAction.php?act=logout">退出登录</a></dd> </dl> </li> </ul> </div> </div> <div class="main"> <!--右侧内容--> <div class="cont"> <!-- <div class="title">后台管理</div> --> <!-- 嵌套网页开始 --> <iframe src="adminMain.php" frameborder="0" name="mainFrame" width="100%" height="800"></iframe> <!-- 嵌套网页结束 --> </div> </div> </div> </body> </html>
01-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值