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程序中的常见漏洞进行攻击
Oct 09 PHP
落伍首发 php+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
Dec 16 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
php单例模式实现(对象只被创建一次)
Dec 05 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
Jul 05 PHP
如何让搜索引擎抓取AJAX内容解决方案
Aug 25 PHP
PHP magento后台无法登录问题解决方法
Nov 24 PHP
php对xml文件的增删改查操作实现方法分析
May 19 PHP
php实现微信发红包功能
Jul 13 PHP
PHP实现基于3DES算法加密解密字符串示例
Aug 24 PHP
PHP PDO数据库操作预处理与注意事项
Mar 16 PHP
Smarty缓存机制实例详解【三种缓存方式】
Jul 20 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
超强分页类2.0发布,支持自定义风格,默认4种显示模式
2007/01/02 PHP
php获取某个目录大小的代码
2008/09/10 PHP
php中常用字符串处理代码片段整理
2011/11/07 PHP
php+mysql大量用户登录解决方案分析
2014/12/29 PHP
PHP实现将textarea的值根据回车换行拆分至数组
2015/06/10 PHP
PHP基于phpqrcode生成带LOGO图像的二维码实例
2015/07/10 PHP
图片之间的切换
2006/06/26 Javascript
关于使用 jBox 对话框的提交不能弹出问题解决方法
2012/11/07 Javascript
jQuery获取Select选择的Text和Value(详细汇总)
2013/01/25 Javascript
JQuery删除DOM节点的方法
2015/06/11 Javascript
HTML5之WebSocket入门3 -通信模型socket.io
2015/08/21 Javascript
探索angularjs+requirejs全面实现按需加载的套路
2016/02/26 Javascript
javascript设计模式之module(模块)模式
2016/08/19 Javascript
原生JS实现首页进度加载动画
2016/09/14 Javascript
JS中Safari浏览器中的Date
2017/07/17 Javascript
Element-UI Table组件上添加列拖拽效果实现方法
2018/04/14 Javascript
vue-router3.0版本中 router.push 不能刷新页面的问题
2018/05/10 Javascript
vue 列表页跳转详情页获取id以及详情页通过id获取数据
2019/03/27 Javascript
探究Python中isalnum()方法的使用
2015/05/18 Python
在Django中管理Users和Permissions以及Groups的方法
2015/07/23 Python
Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
2018/03/19 Python
使用Python横向合并excel文件的实例
2018/12/11 Python
python 实现敏感词过滤的方法
2019/01/21 Python
pyQt5实时刷新界面的示例
2019/06/25 Python
Python双链表原理与实现方法详解
2020/02/22 Python
Python Pygame实现俄罗斯方块
2021/02/19 Python
css3给背景图片加颜色遮罩的方法
2019/11/05 HTML / CSS
泰国在线书店:SE-ED
2020/06/21 全球购物
党员大会主持词
2014/04/02 职场文书
篮球比赛拉拉队口号
2014/06/10 职场文书
2014年图书室工作总结
2014/12/09 职场文书
学校元旦晚会开场白
2014/12/14 职场文书
安全责任书
2015/01/29 职场文书
欢迎新生标语2015
2015/07/16 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
pytorch 使用半精度模型部署的操作
2021/05/24 Python