好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

php验证码识别类 php验证码源码

很多站长朋友们都不太清楚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验证码源码的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did196551
更新时间:2023-04-26   阅读:27次

上一篇: 亳州php培训课程 php课程哪里培训

下一篇:php语法订单制作 php订单提醒功能

最新资料更新

  • 1.php解析img PHP解析器
  • 2.php图书管理系统 php图书管理系统全部代码
  • 3.php的sql累加 sql实现累加
  • 4.phpoutfile的简单介绍
  • 5.php相除保留到分 php除法保留小数
  • 6.php人员查询系统 php信息查询系统
  • 7.redis缓存类php php redis页面缓存
  • 8.php代码的缺点 php的缺点是什么
  • 9.php英国时区代码 英国时区缩写
  • 10.php微信爬虫 微信爬虫访问是什么
  • 11.php区xml文件 php处理xml数据
  • 12.php获取数组坐标 php获取数组的值
  • 13.php物业台账公式 物业台账是什么意思
  • 14.php视频太大怎么传输 php上传视频压缩
  • 15.phpajax日历 php如何生成一年的日历
  • 16.包含asp和php互通的词条
  • 17.php降低curl版本 php怎么升级版本
  • 18.PHP中嵌入script php嵌入html有哪几种方法
  • 19.php接收xml异常 php处理xml数据
  • 20.php复杂语法 php基本语法

CopyRight:2016-{hedonghua:year}{hedonghua:sitegs} 备案ICP:湘ICP备09009000号-16 {hedonghua:sitejym}
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2026 「好得很程序员自学网」
[ SiteMap ]