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 相关文章推荐
php4的session功能评述(一)
Oct 09 PHP
PHP无敌近乎加密方式!
Jul 17 PHP
ajax在joomla中的原生态应用代码
Jul 19 PHP
PHP 数组和字符串互相转换实现方法
Mar 26 PHP
php.ini 配置文件的深入解析
Jun 17 PHP
php中实现获取随机数组列表的自定义函数
Apr 02 PHP
php判断文件夹是否存在不存在则创建
Apr 09 PHP
php ci 获取表单中多个同名input元素值的代码
Mar 25 PHP
Yii2框架数据库简单的增删改查语法小结
Aug 31 PHP
php图像处理函数imagecopyresampled用法详解
Dec 02 PHP
JS(jQuery)实现聊天接收到消息语言自动提醒功能详解【提示“您有新的消息请注意查收”】
Apr 16 PHP
PHP连接SQL server数据库测试脚本运行实例
Aug 24 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中数据的批量导入(csv文件)
2006/10/09 PHP
php处理斐波那契数列非递归方法
2012/02/04 PHP
PHP调用VC编写的COM组件实例
2014/03/29 PHP
php操作redis中的hash和zset类型数据的方法和代码例子
2014/07/05 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
2016/11/17 PHP
php动态读取数据清除最右边距的方法
2017/04/12 PHP
些很实用且必用的小脚本代码
2006/06/26 Javascript
javascript 闭包疑问
2010/12/30 Javascript
从面试题学习Javascript 面向对象(创建对象)
2012/03/30 Javascript
js禁止回车提交表单的示例代码
2013/12/23 Javascript
JS出现失效的情况总结
2017/01/20 Javascript
原生js仿浏览器滚动条效果
2017/03/02 Javascript
vue-router中的hash和history两种模式的区别
2018/07/17 Javascript
傻瓜式解读koa中间件处理模块koa-compose的使用
2018/10/30 Javascript
详解关于Angular4 ng-zorro使用过程中遇到的问题
2018/12/05 Javascript
JavaScript回调函数callback用法解析
2020/01/14 Javascript
Python中的生成器和yield详细介绍
2015/01/09 Python
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
2015/04/05 Python
python生成随机密码或随机字符串的方法
2015/07/03 Python
Python遍历目录中的所有文件的方法
2016/07/08 Python
简单谈谈Python中的json与pickle
2017/07/19 Python
python+opencv实现高斯平滑滤波
2020/07/21 Python
python画微信表情符的实例代码
2019/10/09 Python
python多线程并发及测试框架案例
2019/10/15 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
Python实现大数据收集至excel的思路详解
2020/01/03 Python
Django项目uwsgi+Nginx保姆级部署教程实现
2020/04/19 Python
linux面试相关问题
2012/08/11 面试题
大学生求职简历的自我评价
2013/10/21 职场文书
文明单位申报材料
2014/12/23 职场文书
安全先进个人材料
2014/12/29 职场文书
离职证明格式样本
2015/06/12 职场文书
会议主持词结束语
2015/07/03 职场文书
大学升旗仪式主持词
2015/07/04 职场文书
解决Python中的modf()函数取小数部分不准确问题
2021/05/28 Python
解决persistence.xml配置文件修改存放路径的问题
2022/02/24 Java/Android