php实现生成验证码实例分享


Posted in PHP onApril 10, 2016

image.func.php

<?php 
require_once('string.func.php');
 
 function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){ 
 
  session_start();
  /*定义长度和宽度*/
$width=80;
$height=30;
/* 创建画布*/
$image=imagecreatetruecolor($width, $height);
 
/*本函数用来匹配图形的颜色,供其它绘图函数使用。参数 image 表示图形的 handle。参数 red、green、blue 是色彩三原色,其值从 0 至 255....我在此定义黑色和白色*/
$white=imagecolorallocate($image, 255, 255, 255);
$black=imagecolorallocate($image,0,0,0);
 
/*本函数将图片的封闭长方形区域着色。参数 x1、y1 及 x2、y2 分别为矩形对角线的坐标。参数 col 表示欲涂上的颜色*/
imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);
 
/*buildRandomString函数用来生成一个验证码*/
$chars=buildRandomString($type,$length);
 
/*将验证码给session以便用来判断用户输入是否正确*/
$_SESSION[$sess_name]=$chars;
 
/*定义字体库*/
$fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');
 
 
/*用循环来将验证码一个一个的写入图片中*/
for($i=0;$i<$length;$i++)
{
  $size=mt_rand(14,18);
  $angle=mt_rand(-15,15);
 
  /*验证码的横坐标与纵坐标*/
  $x=5+$i*$size;
  $y=mt_rand(20,26);
 
  $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90));
  $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
  $text=substr($chars,$i,1);
 
  /*本函数将 TTF (TrueType Fonts) 字型文字写入图片*/
 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
}
if($pixel)
{
for($i=0;$i<50;$i++)
{
  /*本函数可在图片上绘出一点。参数 x、y 为欲绘点的坐标,参数 col 表示该点的颜色*/
  imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black);
}}
if($line)
{
  for($i=0;$i<10;$i++)
  {
  $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90));
  /*画线段*/
  imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);
}
}
/*以gif形式输出*/
header("content-type:image/gif");
/*建立GIF图 并输出到网页*/
imagegif($image);
/*释放与 image 关联的内存*/
imagedestroy($image);
}

string.func.php

<?php 
 
function buildRandomString($type=1,$length=4){
 
if($type==1)
{
  /*join函数把数组转换为字符串。。join() 函数是 implode() 函数的别名*/
  $chars=join("",range(0,9));
}elseif ($type==2) {
  /*array_merge函数合并数组*/
  $chars=join("",array_merge(range("a","z"),range("A","Z")));
}elseif($type==3)
{
  $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
}
 
if($length>strlen($chars))
{
  exit("字符串长度不够");
}
/*打乱字符串*/
$chars=str_shuffle($chars);
return substr($chars,0,$length);
 
}
 ?>
PHP 相关文章推荐
使用 eAccelerator加速PHP代码的目的
Mar 16 PHP
php运行出现Call to undefined function curl_init()的解决方法
Nov 02 PHP
深入PHP autoload机制的详解
Jun 09 PHP
9条PHP编程小知识及易犯的小错误
Jan 22 PHP
php动态添加url查询参数的方法
Apr 14 PHP
详解PHP编码转换函数应用技巧
Oct 22 PHP
php实现的简单数据库操作Model类
Nov 16 PHP
PHP检测接口Traversable用法详解
Dec 29 PHP
PHP基于redis计数器类定义与用法示例
Feb 08 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 PHP
基于PHP实现邮箱验证激活过程详解
Oct 28 PHP
PHP如何解决微信文章图片防盗链
Dec 09 PHP
PHP实现简单实用的分页类代码
Apr 08 #PHP
PHP文件上传类实例详解
Apr 08 #PHP
PHP实现基于mysqli的Model基类完整实例
Apr 08 #PHP
php生成带logo二维码方法小结
Apr 08 #PHP
PHP使用PDO操作数据库的乱码问题解决方法
Apr 08 #PHP
PHP中__autoload和Smarty冲突的简单解决方法
Apr 08 #PHP
初识ThinkPHP控制器
Apr 07 #PHP
You might like
用在PHP里的JS打印函数
2006/10/09 PHP
PHP新手用的Insert和Update语句构造类
2012/03/31 PHP
ThinkPHP的截取字符串函数无法显示省略号的解决方法
2014/06/25 PHP
php基础设计模式大全(注册树模式、工厂模式、单列模式)
2015/08/31 PHP
PHP+Mysql+Ajax实现淘宝客服或阿里旺旺聊天功能(前台页面)
2017/06/16 PHP
TP5框架安全机制实例分析
2020/04/05 PHP
javascript alert乱码的解决方法
2013/11/05 Javascript
jquery使用jxl插件导出excel示例
2014/04/14 Javascript
深入理解JavaScript系列(18):面向对象编程之ECMAScript实现
2015/03/05 Javascript
jQuery插件slick实现响应式移动端幻灯片图片切换特效
2015/04/12 Javascript
javascript实现相同事件名称,不同命名空间的调用方法
2015/06/26 Javascript
FullCalendar日历插件应用之数据展现(一)
2015/12/23 Javascript
Bootstrap 布局组件(全)
2016/07/18 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
Websocket协议详解及简单实例代码
2016/12/12 Javascript
Bootstrap源码解读导航条(7)
2016/12/23 Javascript
Bootstrap面板(Panels)的简单实现代码
2017/03/17 Javascript
基于JavaScript实现数码时钟效果
2020/03/30 Javascript
使用 Node.js 模拟滑动拼图验证码操作的示例代码
2017/11/02 Javascript
Angular实现的table表格排序功能完整示例
2017/12/22 Javascript
vue 使用自定义指令实现表单校验的方法
2018/08/28 Javascript
Koa 使用小技巧(小结)
2018/10/22 Javascript
jQuery实现本地存储
2020/12/22 jQuery
python通过colorama模块在控制台输出彩色文字的方法
2015/03/19 Python
使用FastCGI部署Python的Django应用的教程
2015/07/22 Python
在Python程序中操作MySQL的基本方法
2015/07/29 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
python 用正则表达式筛选文本信息的实例
2018/06/05 Python
Python使用paramiko操作linux的方法讲解
2019/02/25 Python
python读写csv文件方法详细总结
2019/07/05 Python
python异步编程 使用yield from过程解析
2019/09/25 Python
英国最大的老式糖果店:A Quarter Of
2017/04/08 全球购物
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
请假条怎么写
2014/04/10 职场文书
2014年教师节寄语
2014/08/11 职场文书
学生自我鉴定格式及范文
2014/09/16 职场文书