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 相关文章推荐
第十节--抽象方法和抽象类
Nov 16 PHP
Linux下ZendOptimizer的安装与配置方法
Apr 12 PHP
一家之言的经验之谈php+mysql扎实个人基本功
Mar 27 PHP
php的curl实现get和post的代码
Aug 23 PHP
php的memcached客户端memcached
Jun 14 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
Jun 29 PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
Jul 29 PHP
php操作access数据库的方法详解
Feb 22 PHP
laravel 5.4中实现无限级分类的方法示例
Jul 27 PHP
Yii2.0框架behaviors方法使用实例分析
Sep 30 PHP
Thinkphp 框架扩展之驱动扩展实例分析
Apr 27 PHP
PHP7 整型处理机制修改
Mar 09 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 删除数组元素
2009/01/16 PHP
php获取url字符串截取路径的文件名和扩展名的函数
2010/01/22 PHP
浅析php单例模式
2014/11/25 PHP
PHP环境搭建(php+Apache+mysql)
2016/11/14 PHP
老生常谈PHP面向对象之命令模式(必看篇)
2017/05/24 PHP
php计数排序算法的实现代码(附四个实例代码)
2020/03/31 PHP
用JS实现的一个include函数
2007/07/21 Javascript
Prototype RegExp对象 学习
2009/07/19 Javascript
jQuery插件jQuery-JSONP开发ajax调用使用注意事项
2013/11/22 Javascript
JavaScript实现梯形乘法表的方法
2015/04/25 Javascript
js实现密码强度检测【附示例】
2016/03/30 Javascript
js+css3制作时钟特效
2016/10/16 Javascript
Jquery Easyui自定义下拉框组件使用详解(21)
2020/12/31 Javascript
js数组去重的hash方法
2016/12/22 Javascript
基于JS实现移动端向左滑动出现删除按钮功能
2017/02/22 Javascript
JS生成随机打乱数组的方法示例
2017/12/23 Javascript
elementUI Tree 树形控件的官方使用文档
2019/04/25 Javascript
微信小程序自定义顶部组件customHeader的示例代码
2020/06/03 Javascript
Python中使用dom模块生成XML文件示例
2015/04/05 Python
python获取一组汉字拼音首字母的方法
2015/07/01 Python
python动态网页批量爬取
2016/02/14 Python
Python 爬虫学习笔记之多线程爬虫
2016/09/21 Python
python实现基于SVM手写数字识别功能
2020/05/27 Python
Flask框架Flask-Login用法分析
2018/07/23 Python
python3.4爬虫demo
2019/01/22 Python
详解Python的三种可变参数
2019/05/08 Python
Python 中Django安装和使用教程详解
2019/07/03 Python
Python实现多线程下载脚本的示例代码
2020/04/03 Python
零基础小白多久能学会python
2020/06/22 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021/01/09 Python
同学聚会老师邀请函
2014/01/28 职场文书
联谊活动总结
2014/08/28 职场文书
财务工作疏忽检讨书
2014/09/11 职场文书
如何使用vue3打造一个物料库
2021/05/08 Vue.js
Vue中foreach数组与js中遍历数组的写法说明
2021/06/05 Vue.js
Windows安装Anaconda3的方法及使用过程详解
2021/06/11 Python