很多站长朋友们都不太清楚php验证码识别类,今天小编就来给大家整理php验证码识别类,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP图形验证码识别 2、 php 验证码类,怎么改变验证码形状 3、 请问PHP生成验证码的类怎么写? 4、 php 调用验证码为一个类 怎么调用 PHP图形验证码识别1.验证码取出,转到8位或者24位位图
2.位图处理,二值化,RGB设定阀值小于阀值时为1否则为0 1为黑神色,0为白色
3.位图处理,去噪点干扰,利用二值化的位图,八方向法,一个孤立的噪点周围八个方向的点都是0白色。
4.干扰线,定义边界均为白色的区域,若干像素,让该区域在位图移动,如果进入区域内的黑色点小于某值时认定为噪点或干扰线。
5.分割。简单平均分布可以直接固定分割。复杂的有粘连的利用投影,求字符数+1个极小值或极大值。极小值之间最小距离<极小值到前一个极小值距离<极小值之间最大距离。最大距离和最小距离按照字符长度来目测,一点一点的对比得出适当的值。
虽然此法可以解决部分粘连验证码,但是对于一些变态变形的公共区域比较多的验证码是无效的。
如果想知道更多分割方法,请到百度文库,搜索验证码分割。
6.识别。建立特征库,或者利用神经网络自动学习。
然后比对,字节或者文本均可。相似度自己设定,一般在90%以上
这些理论知识都学习明白了,基本就可以去做识别验证码了。
验证码最最重要且最难的一点就是分割。
有些方法不需要分割也可以借鉴一下。
php 验证码类,怎么改变验证码形状直接上代码:
复制代码 代码如下:
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 130;//宽度
private $height = 50;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 20;//指定字体大小
private $fontcolor;//指定字体颜色
//构造方法初始化
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片}
//生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];}
}
//生成背景
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);}
//生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);}
}
//生成线条、雪花
private function createLine() {
//线条
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);}
//雪花
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);}
}
//输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
return strtolower($this->code);
}
}
输出实例:
使用方法:
1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;2、新建一个名为 captcha.php 的文件进行调用该类;captcha.php
复制代码 代码如下:
session_start();
require './ValidateCode.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。
$_vc = new ValidateCode(); //实例化一个对象$_vc->doimg();
$_SESSION['authnum_session'] = $_vc->getCode();//验证码保存到SESSION中3、引用到页面中,代码如下:
复制代码 代码如下:
<img title="点击刷新" src="/data/upload/help/202303/13/e9719592dfc75770b2b320906205ae16.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
4、一个完整的验证页面,代码如下:
复制代码 代码如下:
<?php
session_start();
//在页首先要开启session,
//error_reporting(2047);
session_destroy();
//将session去掉,以每次都能取新的session值;//用seesion 效果不错,也很方便
?>
<html>
<head>
<title>session 图片验证实例</title>
<style type="text/css">
#login p{
margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}
#login img{
cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head>
<body>
<form id="login" action="" method="post">
<p>此例为session验证实例</p>
<p>
<span>验证码:</span>
<input type="text" name="validate" value="" size=10>
<img title="点击刷新" src="/data/upload/help/202303/13/e9719592dfc75770b2b320906205ae16.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
//打印上一个session;
//echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";if($validate!=$_SESSION["authnum_session"]){//判断session值与用户输入的验证码是否一致;echo "<font color=red>输入有误</font>";
}else{
echo "<font color=green>通过验证</font>";}
}
?>
请问PHP生成验证码的类怎么写?<?php
class Code{
// 1. 定义各个成员 有宽、高、画布、字数、类型、画类型
private $width; //宽度
private $height; //高度
private $num; //验证码字数
private $imgType; //生成图片类型
private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合
private $hb; //画布
public $codestr; // 验证码字串
public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this->width = $num*20;
$this->height = $height;
$this->num = $num;
$this->imgType = $imgType;
$this->Type = $Type;
$this->codestr = $this->codestr();
$this->zuhe();
}
// 2. 定义随机获取字符串函数
private function codestr(){
switch($this->Type){
case 1: // 类型为1 获取1-9随机数
$str = implode("",array_rand(range(0,9),$this->num));
break;
case 2: // 类型为2 获取a-z随机小写字母
$str = implode("",array_rand(array_flip(range(a,z)),$this->num));
break;
case 3: // 类型为3 获取数字,小写字母,大写字母 混合
for($i=0;$i<$this->num;$i++){
$m = rand(0,2);
switch($m){
case 0:
$o = rand(48,57);
break;
case 1:
$o = rand(65,90);
break;
case 2:
$o = rand(97,122);
break;
}
$str .= sprintf("%c",$o);
}
break;
}
return $str;
}
// 3. 初始化画布图像资源
private function Hb(){
$this->hb = imagecreatetruecolor($this->width,$this->height);
}
// 4. 生成背景颜色
private function Bg(){
return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));
}
// 5. 生成字体颜色
private function Font(){
return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));
}
// 6. 填充背景颜色
private function BgColor(){
imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());
}
// 7. 干扰点
private function ganrao(){
$sum=floor(($this->width)*($this->height)/3);
for($i=0;$i<$sum;$i++){
imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());
}
}
// 8. 随机直线 弧线
private function huxian(){
for($i=0;$i<$this->num;$i++){
imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());
}
}
// 9. 写字
private function xiezi(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i;
$y=rand(1,$this->height-15);
imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
}
}
// 10. 输出
private function OutImg(){
$shuchu="image".$this->imgType;
$header="Content-type:image/".$this->imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this->hb);
}else{
exit("GD库没有此类图像");
}
}
// 11. 拼装
private function zuhe(){
$this->Hb();
$this->BgColor();
$this->ganrao();
$this->huxian();
$this->xiezi();
$this->OutImg();
}
public function getCodeStr(){
return $this->codestr;
}
}
$a = new Code();
$a ->getCodeStr();
?>
php 调用验证码为一个类 怎么调用1,php验证码类
<?php
// usage:
/*
显示验证码:
<img src="/data/upload/help/202303/13/4ad9346bec449dfeac0ccb94d1b39cc2.png">
检查验证码:
检查输入的验证码与 $_SESSION['login'] 中保存的值是否相等。
*/
error_reporting(E_ALL);
session_start();
(!isset($_GET['cap']))?die('Error !'):1;
$captcha_array=array('login.png','contact.png','comment.png');
(!in_array($_GET['cap'],$captcha_array))?die('Error !'):1;
$captcha_cod=new captcha(basename($_GET['cap'],'.png')) ;
//验证码类
class captcha
{
private $session_name;
private $image_width;
private $image_height;
private $cod_length;
private $cod_mode;
private $font_path;
private $avtage_font_size;
private $sec_cod;
private $res_image;
function __construct($name,$width=200,$height=50,$length=5,$mod=2,$font='arial.ttf',$av_font_size=25)
{
$this-> session_name = $name ;
$this-> image_width = $width ;
$this-> image_height = $height ;
$this-> cod_length = $length ;
$this-> mode = $mod ;
$this-> font_path = $font ;
$this-> avrage_font_size = $av_font_size ;
$this->Gen_Cod();
}
function Write_Text($text)
{
$x_pos=10;
for($pos=0;$pos<strlen($text);$pos++) {
imagettftext($this->res_image,rand($this->avrage_font_size -2,$this->avrage_font_size +2),
rand(-40,+40),$x_pos,rand(35,$this->image_height - $this->avrage_font_size),
imagecolorallocate($this->res_image,rand(0,150),rand(0,150),rand(0,150)),
$this->font_path,$text[$pos]);
$x_pos+=($this->image_width/$this->cod_length);
}
}
function Draw_Line()
{
//
for($pos=0;$pos<$this->image_height;$pos+=8)
imageline($this->res_image,0,$pos,$this->image_width,$pos,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230)));
//
for($pos=0;$pos<$this->image_width;$pos+=8)
imageline($this->res_image,$pos,0,$pos,$this->image_height,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230)));
}
function Gen_Cod()
{
//generate rand cod :
//mode:1 => 0-9 , mode:2 => 0-9 , a-z
($this->mode==1) ? $this->sec_cod=substr((string)rand(1000000000,9999999999),0,$this->cod_length) :
$this->sec_cod=substr(md5(rand(1000000000,9999999999)),0,$this->cod_length);
//set session :
$_SESSION[$this->session_name] = $this->sec_cod ;
//creat image :
$this->res_image=imagecreatetruecolor( $this->image_width , $this->image_height );
//fill color:
imagefilledrectangle($this->res_image,0,0,$this->image_width,$this->image_height,imagecolorallocate($this->res_image,255,255,255));
//write text :
$this->Write_Text($this->sec_cod);
//draw line :
$this->Draw_Line();
//output :
imagejpeg($this->res_image);
header('content-type:image/jpeg');
//destroy:
imagedestroy($this->res_image);
}
}
2,php验证码类的调用示例:
<?php
session_start();
if(isset($_POST['captchacod'])){
if($_SESSION['login']==$_POST['captchacod'])echo'<a style="color:green">Your Entered Cod Was Correct</a><br>';
else echo'<a style="color:red">Your Entered Cod Was Incorrect</a><br>';
}
?>
<img src="/data/upload/help/202303/13/4ad9346bec449dfeac0ccb94d1b39cc2.png" >
<form action="<?php echo $_SERVER['PHP_SELF']; //safe it later (xss)?>" method="post">
<a>INPUT TEXT :</a><br>
<input type="text" name="captchacod"><br>
<input type="submit" value="check"><br>
</form>
关于php验证码识别类的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php验证码识别类 php验证码源码的详细内容...