php 验证码步骤和代码
来源:互联网 发布:思科软件下载 编辑:程序博客网 时间:2024/06/01 12:46
<?php
//本类只提供用php产生验证码的方法,不提供js代码,
//本类开源,是我无聊的时候写的,希望能够帮到你
//1:设置验证码属性
//2: 绘制基本框架
//3:产生随机字符串
//4:添加干扰
//5:绘制完成产生图片
class Verification
{
//1:设置验证码属性
private $border = 0;//边框
private $num = 4;//验证码位数
private $width = 60 ;//验证码宽度
private $higth = 20;//图片高度
private $fontsize = 6;//字体大小
private $content = "qwertyuiopasdfghjklzxcvbnm123456789";//验证码源
private $str = "";//当前字符串
public function __construct()
{
srand((double)microtime()*1000000); //初始化随机数种子;
}
//2: 绘制基本框架
//2.1创建图片
private function create()
{
$img = ImageCreate($this ->width,$this -> higth);
return $img;
}
//2.2填充颜色
private function color()
{
$img = $this -> create();
$bgcolor = ImageColorAllocate($img,255,255,255);//设置背景颜色
ImageFill($img,0,0,$bgcolor);
//设置边框颜色
if($this -> border)
{
$bordercolor = ImageColorAllocate($img,0,0,0);
ImageRectangle($img,0,0,$this -> width -1,$this -> higth -1,$bordercolor);
}
return $img;
}
//3:产生随机字符串
private function randstr()
{ $str = "";
$img = $this -> color();
for($i =0;$i<$this -> num;$i++)
{
$which = mt_rand(0,strlen($this -> content)-1 );
$code = substr($this -> content,$which,1);
$j = $i*15+2; //绘字符位置
$color = ImageColorAllocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
ImageChar($img,$this -> fontsize,$j,3,$code,$color);
$str .= $code;
}
$array = array();
$array["img"] = $img;
$array["str"] = $str;
return $array;
}
//4:添加干扰
public function disturbance()
{
$array = $this -> randstr();
//添加干扰线
for($i = 0;$i < $this -> num;$i++)
{
$color1 = ImageColorAllocate($array["img"],mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
ImageArc($array["img"], mt_rand(-5,$this -> width), mt_rand(-5,$this -> higth), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干扰线
}
for($i = 0;$i < $this -> num * 40;$i++)
{
$color2 = ImageColorAllocate($array["img"],mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
ImageSetPixel($array["img"] ,mt_rand(-5,$this -> width), mt_rand(-5,$this -> higth), $color2); //干扰点
}
// return $this -> num;
return $array;
}
}
//Header("Content-type: image/gif");
//$obj = new Verification();
//$array = $obj -> disturbance();
//Imagegif($array["img"]);
//ImageDestroy($array["img"]);
//echo $array["str"];
?>
//本类只提供用php产生验证码的方法,不提供js代码,
//本类开源,是我无聊的时候写的,希望能够帮到你
//1:设置验证码属性
//2: 绘制基本框架
//3:产生随机字符串
//4:添加干扰
//5:绘制完成产生图片
class Verification
{
//1:设置验证码属性
private $border = 0;//边框
private $num = 4;//验证码位数
private $width = 60 ;//验证码宽度
private $higth = 20;//图片高度
private $fontsize = 6;//字体大小
private $content = "qwertyuiopasdfghjklzxcvbnm123456789";//验证码源
private $str = "";//当前字符串
public function __construct()
{
srand((double)microtime()*1000000); //初始化随机数种子;
}
//2: 绘制基本框架
//2.1创建图片
private function create()
{
$img = ImageCreate($this ->width,$this -> higth);
return $img;
}
//2.2填充颜色
private function color()
{
$img = $this -> create();
$bgcolor = ImageColorAllocate($img,255,255,255);//设置背景颜色
ImageFill($img,0,0,$bgcolor);
//设置边框颜色
if($this -> border)
{
$bordercolor = ImageColorAllocate($img,0,0,0);
ImageRectangle($img,0,0,$this -> width -1,$this -> higth -1,$bordercolor);
}
return $img;
}
//3:产生随机字符串
private function randstr()
{ $str = "";
$img = $this -> color();
for($i =0;$i<$this -> num;$i++)
{
$which = mt_rand(0,strlen($this -> content)-1 );
$code = substr($this -> content,$which,1);
$j = $i*15+2; //绘字符位置
$color = ImageColorAllocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
ImageChar($img,$this -> fontsize,$j,3,$code,$color);
$str .= $code;
}
$array = array();
$array["img"] = $img;
$array["str"] = $str;
return $array;
}
//4:添加干扰
public function disturbance()
{
$array = $this -> randstr();
//添加干扰线
for($i = 0;$i < $this -> num;$i++)
{
$color1 = ImageColorAllocate($array["img"],mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
ImageArc($array["img"], mt_rand(-5,$this -> width), mt_rand(-5,$this -> higth), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干扰线
}
for($i = 0;$i < $this -> num * 40;$i++)
{
$color2 = ImageColorAllocate($array["img"],mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
ImageSetPixel($array["img"] ,mt_rand(-5,$this -> width), mt_rand(-5,$this -> higth), $color2); //干扰点
}
// return $this -> num;
return $array;
}
}
//Header("Content-type: image/gif");
//$obj = new Verification();
//$array = $obj -> disturbance();
//Imagegif($array["img"]);
//ImageDestroy($array["img"]);
//echo $array["str"];
?>
- php 验证码步骤和代码
- php验证码代码实例
- php jquery 验证码代码
- PHP验证码函数代码
- php生成验证码代码
- PHP验证码代码分享
- PHP验证手机号码和归属地 PHP函数代码
- JAVA学习代码——验证码生生成步骤
- php生成验证码图片代码
- php生成验证码图片代码
- PHP 验证码的实现代码
- PHP 身份证号码验证代码
- PHP验证码代码_php验证码类
- php验证码实现代码(3种)验证类
- Java 直接用jdbc连接数据库 sql2000 的步骤和验证代码
- php中文用户名验证代码
- php用户验证登陆代码
- php 登陆后台验证代码
- ipcs清理命令整理
- Android自定义发送短信实例
- 新浪微博Rsa2加密方式,登录
- Main方法
- [题解]ACMer 2013 Daily Training- 1st Mar for 11x&&12x
- php 验证码步骤和代码
- 项目2:日期结构体
- Structs2拦截器详解
- 关于在python中获取CGI之POST请求data块内容
- QTextEdit捕捉回车信号
- JAVA学习:高级加密标准(AES)
- read(), readline(), readlines()方法的使用
- Tomcat并发数优化的方法总结
- CRM