php生成图片验证码的方法


Posted in PHP onApril 15, 2016

本文为大家分享了php生成图片验证码的方法,供大家参考,具体内容如下

首先从指定字符集合中随机抽取固定数目的字符,以一种不规则的方法画在画布上,再适当添加一些干扰点和干扰元素,最后将图片输出,一张崭新的验证码就完成了。

前端代码如下:

<!DOCTYPE html>
<html>
<head>
 <meta http-equiv="content-type" content="text/html;charset=utf-8">
 <title>This is a test!</title>
 <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
 <form name="form">
  <input type="text" placeholder="账号"/><br/>
  <input type="password" placeholder="密码"/><br/>
  <input type="text" placeholder="验证码"/>
  <img id="verImg" src="libs/verification.php"/>
  <a href="#" class="change" onclick="changeVer()">点击刷新</a><br/>
  <input type="submit" value="登录"/>
 </form>
 <script type="text/javascript">
 //刷新验证码
 function changeVer(){
  document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
 }
 </script>
</body>
</html>

php脚本文件验证码的代码如下:

<?php
 
session_start();
//开启session记录验证码数据
 
vCode(4, 15);//设置验证码的字符个数和图片基础宽度
 
//vCode 字符数目,字体大小,图片宽度、高度
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
 
 !$width && $width = $num * $size * 4 / 5 + 15;
 !$height && $height = $size + 10;
 
 //设置验证码字符集合
 $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
 //保存获取的验证码
 $code = '';
 
 //随机选取字符
 for ($i = 0; $i < $num; $i++) {
  $code .= $str[mt_rand(0, strlen($str)-1)];
 }
 
 //创建验证码画布
 $im = imagecreatetruecolor($width, $height);
 
 //背景色
 $back_color = imagecolorallocate($im, mt_rand(0,100),mt_rand(0,100), mt_rand(0,100));
 
 //文本色
 $text_color = imagecolorallocate($im, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
 
 imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
 
 
  // 画干扰线
 for($i = 0;$i < 5;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
 }
 
  // 画干扰点
 for($i = 0;$i < 50;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
 }
 
 //随机旋转角度数组
 $array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
 
  // 输出验证码
 // imagefttext(image, size, angle, x, y, color, fontfile, text)
 @imagefttext($im, $size , array_rand($array), 12, $size + 6, $text_color, 'c:\WINDOWS\Fonts\simsun.ttc', $code);
 $_SESSION["VerifyCode"]=$code;
 //no-cache在每次请求时都会访问服务器
 //max-age在请求1s后再次请求会再次访问服务器,must-revalidate则第一发送请求会访问服务器,之后不会再访问服务器
 // header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
 header("Cache-Control: no-cache");
 header("Content-type: image/png;charset=gb2312");
 //将图片转化为png格式
 imagepng($im);
 imagedestroy($im);
}
?>

效果图:

php生成图片验证码的方法

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助,大家学会编写php图片验证码

PHP 相关文章推荐
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
Aug 21 PHP
php生成略缩图代码
Jul 16 PHP
PHP如何利用P3P实现跨域
Aug 24 PHP
单点登录 Ucenter示例分析
Oct 29 PHP
php实例分享之html转为rtf格式
Jun 02 PHP
php使用GeoIP库实例
Jun 27 PHP
PHP页面实现定时跳转的方法
Oct 31 PHP
Laravel实现构造函数自动依赖注入的方法
Mar 16 PHP
PHP实现Huffman编码/解码的示例代码
Apr 20 PHP
thinkPHP5框架分页样式类完整示例
Sep 01 PHP
php判断目录存在的简单方法
Sep 26 PHP
tp5.1框架数据库子查询操作实例分析
May 26 PHP
PHP抓取淘宝商品的用户晒单评论+图片+搜索商品列表实例
Apr 14 #PHP
php上传大文件设置方法
Apr 14 #PHP
什么是OneThink oneThink后台添加插件步骤
Apr 13 #PHP
java模拟PHP的pack和unpack类
Apr 13 #PHP
php远程下载类分享
Apr 13 #PHP
Thinkphp和onethink实现微信支付插件
Apr 13 #PHP
PHP MSSQL 分页实例
Apr 13 #PHP
You might like
php中通过正则表达式下载内容中的远程图片的函数代码
2012/01/10 PHP
ThinkPHP实现跨模块调用操作方法概述
2014/06/20 PHP
PHP微信开发之文本自动回复
2016/06/23 PHP
PHP单例模式与工厂模式详解
2017/08/29 PHP
Laravel5.5 实现后台管理登录的方法(自定义用户表登录)
2019/09/30 PHP
捕获关闭窗口的脚本
2009/01/10 Javascript
Jquery iframe内部出滚动条
2010/02/11 Javascript
Jquery加载时从后台读取数据绑定到dropdownList实例
2013/06/09 Javascript
禁止页面刷新让F5快捷键及右键都无效
2014/01/22 Javascript
nodejs创建web服务器之hello world程序
2015/08/20 NodeJs
使用pcs api往免费的百度网盘上传下载文件的方法
2016/03/17 Javascript
bootstrap配合Masonry插件实现瀑布式布局
2017/01/18 Javascript
Bootstrap导航中表单简单实现代码
2017/03/06 Javascript
javascript 中select框触发事件过程的分析
2017/08/01 Javascript
Js经典案例的实例代码
2018/05/10 Javascript
vue上传图片到oss的方法示例(图片带有删除功能)
2018/09/27 Javascript
使用webpack搭建vue项目实现脚手架功能
2019/03/15 Javascript
微信小程序日历插件代码实例
2019/12/04 Javascript
.netcore+vue 实现压缩文件下载功能
2020/09/24 Javascript
Python使用Pycrypto库进行RSA加密的方法详解
2016/06/06 Python
CentOS下使用yum安装python-pip失败的完美解决方法
2017/08/16 Python
PHP实现发送和接收JSON请求
2018/06/07 Python
在Python中过滤Windows文件名中的非法字符方法
2019/06/10 Python
Python操作MongoDb数据库流程详解
2020/03/05 Python
基于nexus3配置Python仓库过程详解
2020/06/15 Python
css3进阶之less实现星空动画的示例代码
2019/09/10 HTML / CSS
HTML5实现无刷新修改URL的方法
2019/11/14 HTML / CSS
应届生幼儿园求职信
2013/11/12 职场文书
厂办主管岗位职责范本
2014/02/28 职场文书
公司授权委托书
2014/04/04 职场文书
餐饮周年庆活动方案
2014/08/14 职场文书
小学趣味运动会加油稿
2014/09/25 职场文书
技术员岗位职责
2015/02/04 职场文书
php实例化对象的实例方法
2021/11/17 PHP
Win11筛选键导致键盘失灵怎么解决? Win11关闭筛选键的技巧
2022/04/08 数码科技
SpringBoot详解自定义Stater的应用
2022/07/15 Java/Android