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 相关文章推荐
mysql 全文搜索 技巧
Apr 27 PHP
php !function_exists(&quot;T7FC56270E7A70FA81A5935B72EACBE29&quot;))代码解密
Jan 07 PHP
5种PHP创建数组的实例代码分享
Jan 17 PHP
使用php记录用户通过搜索引擎进网站的关键词
Feb 13 PHP
php无限极分类递归排序实现方法
Nov 11 PHP
php读取远程gzip压缩网页的方法
Dec 29 PHP
PHP+MySQL存储数据常见中文乱码问题小结
Jun 13 PHP
php可变长参数处理函数详解
Feb 22 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 PHP
PHP删除数组中指定值的元素常用方法实例分析【4种方法】
Aug 21 PHP
PHP读取目录树的实现方法分析
Mar 22 PHP
PHP面向对象程序设计之对象的遍历操作示例
Jun 12 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
深入file_get_contents函数抓取内容失败的原因分析
2013/06/25 PHP
ThinkPHP之foreach标签使用概述
2014/06/30 PHP
php删除左端与右端空格的方法
2014/11/29 PHP
浅析Yii2集成富文本编辑器redactor实例教程
2016/04/25 PHP
PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)
2016/09/11 PHP
PHP中__set()实例用法和基础讲解
2019/07/23 PHP
通过javascript的匿名函数来分析几段简单有趣的代码
2010/06/29 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
浏览器的JavaScript引擎的识别方法
2013/10/20 Javascript
jQuery删除节点用法示例(remove方法)
2016/09/08 Javascript
Javascript函数中的arguments.callee用法实例分析
2016/09/16 Javascript
js面向对象实现canvas制作彩虹球喷枪效果
2016/09/24 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
微信小程序删除处理详解
2017/08/16 Javascript
angularjs实现猜大小功能
2017/10/23 Javascript
用Node编写RESTful API接口的示例代码
2018/07/04 Javascript
Vue动态加载异步组件的方法
2018/11/21 Javascript
javascript事件监听与事件委托实例详解
2019/08/16 Javascript
python动态加载变量示例分享
2014/02/17 Python
分享Python开发中要注意的十个小贴士
2016/08/30 Python
python实现人人自动回复、抢沙发功能
2018/06/08 Python
基于Python实现用户管理系统
2019/02/26 Python
Python for循环与range函数的使用详解
2019/03/23 Python
python tkinter实现界面切换的示例代码
2019/06/14 Python
关于python 跨域处理方式详解
2020/03/28 Python
python如何导入依赖包
2020/07/13 Python
Python爬虫抓取指定网页图片代码实例
2020/07/24 Python
基于Python的一个自动录入表格的小程序
2020/08/05 Python
Python内置函数property()如何使用
2020/09/01 Python
Python将list元素转存为CSV文件的实现
2020/11/16 Python
Kiehl’s科颜氏西班牙官方网站:源自美国的植物护肤品牌
2020/02/22 全球购物
某公司C#程序员面试题笔试题
2014/05/26 面试题
哈弗商学院毕业生求职信
2014/02/26 职场文书
公司新人试用期自我评价
2014/09/17 职场文书
HTML5简单实现添加背景音乐的几种方法
2021/05/12 HTML / CSS
浅谈MySql update会锁定哪些范围的数据
2022/06/25 MySQL