支持中文字母数字、自定义字体php验证码代码

代码如下:

<?php
/*
* Captcha Class base on PHP GD Lib
* @author Design
* @version 1.0
* @demo
* include('captchaClass.php');
* $captchaDemo=new Captcha();
* $captchaDemo->createImage();
*/
class Captcha{
//@定义验证码图片高度
private $height;
//@定义验证码图片宽度
private $width;
//@定义验证码字符个数
private $textNum;
//@定义验证码字符内容
private $textContent;
//@定义字符颜色
private $fontColor;
//@定义随机出的文字颜色
private $randFontColor;
//@定义字体大小
private $fontSize;
//@定义字体
private $fontFamily;
//@定义背景颜色
private $bgColor;
//@定义随机出的背景颜色
private $randBgColor;
//@定义字符语言
private $textLang;
//@定义干扰点数量
private $noisePoint;
//@定义干扰线数量
private $noiseLine;
//@定义是否扭曲
private $distortion;
//@定义扭曲图片源
private $distortionImage;
//@定义是否有边框
private $showBorder;
//@定义验证码图片源
private $image;

//@Constructor 构造函数
public function Captcha(){
$this->textNum=4;
$this->fontSize=16;
$this->fontFamily='c:\windows\fontsSIMYOU.ttf';//设置中文字体,可以改成linux的目录
$this->textLang='en';
$this->noisePoint=30;
$this->noiseLine=3;
$this->distortion=false;
$this->showBorder=false;
}

//@设置图片宽度
public function setWidth($w){
$this->width=$w;
}

//@设置图片高度
public function setHeight($h){
$this->height=$h;
}

//@设置字符个数
public function setTextNumber($textN){
$this->textNum=$textN;
}

//@设置字符颜色
public function setFontColor($fc){
$this->fontColor=sscanf($fc,'#%2x%2x%2x');
}

//@设置字号
public function setFontSize($n){
$this->fontSize=$n;
}

//@设置字体
public function setFontFamily($ffUrl){
$this->fontFamily=$ffUrl;
}

//@设置字符语言
public function setTextLang($lang){
$this->textLang=$lang;
}

//@设置图片背景
public function setBgColor($bc){
$this->bgColor=sscanf($bc,'#%2x%2x%2x');
}

//@设置干扰点数量
public function setNoisePoint($n){
$this->noisePoint=$n;
}

//@设置干扰线数量
public function setNoiseLine($n){
$this->noiseLine=$n;
}

//@设置是否扭曲
public function setDistortion($b){
$this->distortion=$b;
}

//@设置是否显示边框
public function setShowBorder($border){
$this->showBorder=$border;
}

//@初始化验证码图片
public function initImage(){
if(empty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;}
if(empty($this->height)){$this->height=$this->fontSize*2;}
$this->image=imagecreatetruecolor($this->width,$this->height);
if(empty($this->bgColor)){
$this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
}else{
$this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
}
imagefill($this->image,0,0,$this->randBgColor);
}

//@产生随机字符
public function randText($type){
$string='';
switch($type){
case 'en':
$str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
for($i=0;$i<$this->textNum;$i++){
$string=$string.','.$str[mt_rand(0,29)];
}
break;
case 'cn':
for($i=0;$i<$this->textNum;$i++) {
$string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
}
$string=iconv('GB2312','UTF-8',$string); //转换编码到utf8
break;
}
return substr($string,1);
}

//@输出文字到验证码
public function createText(){
$textArray=explode(',',$this->randText($this->textLang));
$this->textContent=join('',$textArray);
if(empty($this->fontColor)){
$this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}else{
$this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
}
for($i=0;$i<$this->textNum;$i++){
$angle=mt_rand(-1,1)*mt_rand(1,20);
imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]);
}
}

//@生成干扰点
public function createNoisePoint(){
for($i=0;$i<$this->noisePoint;$i++){
$pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
}

}

//@产生干扰线
public function createNoiseLine(){
for($i=0;$i<$this->noiseLine;$i++) {
$lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
}
}

//@扭曲文字
public function distortionText(){
$this->distortionImage=imagecreatetruecolor($this->width,$this->height);
imagefill($this->distortionImage,0,0,$this->randBgColor);
for($x=0;$x<$this->width;$x++){
for($y=0;$y<$this->height;$y++){
$rgbColor=imagecolorat($this->image,$x,$y);
imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
}
}
$this->image=$this->distortionImage;
}

//@生成验证码图片
public function createImage(){
$this->initImage(); //创建基本图片
$this->createText(); //输出验证码字符
if($this->distortion){$this->distortionText();} //扭曲文字
$this->createNoisePoint(); //产生干扰点
$this->createNoiseLine(); //产生干扰线
if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框
imagepng($this->image);
imagedestroy($this->image);
if($this->distortion){imagedestroy($this->$distortionImage);}
return $this->textContent;
}

}
?>使用方法:

<?php
//session_start();
header("Content-type:image/png");
include('captcha5_class.php');
$captcha5=new Captcha();

//@设置验证码宽度
//$captcha5->setWidth(200);

//@设置验证码高度
//$captcha5->setHeight(50);

//@设置字符个数
$captcha5->setTextNumber(5);

//@设置字符颜色
//$captcha5->setFontColor('#ff9900');

//@设置字号大小
//$captcha5->setFontSize(25);

//@设置字体
$captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF');

//@设置语言
$captcha5->setTextLang('cn');

//@设置背景颜色
//$captcha5->setBgColor('#000000');

//@设置干扰点数量
//$captcha5->setNoisePoint(600);

//@设置干扰线数量
//$captcha5->setNoiseLine(10);

//@设置是否扭曲
//$captcha5->setDistortion(true);

//@设置是否显示边框
$captcha5->setShowBorder(true);

//输出验证码
$code=$captcha5->createImage();
//$_SESSION['captchaCode']['content']=$code;
//$_SESSION['captchaCode']['time']=microtime();
?>

(0)

相关推荐

  • php实现的数字验证码及数字运算验证码

    本文实例讲述了php实现的数字验证码及数字运算验证码.分享给大家供大家参考.具体如下: 1. 数字验证码: <?php //第一个实例是数字验证码,最常见的验证码.多少个数字可以由自己决定. //$num是生成的验证码包含几个数字 getValidate(4,60,20); function getValidate($num,$w,$h){ $code = ""; for($i=0;$i<$num;$i++){ $code .= rand(0,9); } //code变量最

  • php生成数字字母的验证码图片

    php生成数字字母的验证码图片 <?php header ('Content-Type: image/png'); $image=imagecreatetruecolor(100, 30); $color=imagecolorallocate($image, 255, 255, 255); imagefill($image, 20, 20, $color); //只含有数字 // for($i=0;$i<4;$i++){ // $font=6; // $x=rand(5,10)+$i*100/

  • php生成4位数字验证码的实现代码

    在php中实现验证码还是很方便的,关键点在于掌握php gd库与session的用法. 纵观网上php 生成验证码的例子,无不是php gd库与session相结合,并利用php 生成随机数的方法来完成. PHP验证码,可以分为很多种,包括 php 图片验证码,php 随机验证码,以及php 中文验证码等,根据不同的应用场合来使用不同的验证码. 这里分享一个php数字验证码,供大家参考. 4位数字验证码 /* *Filename:authpage.php */ session_start();

  • php中文字母数字验证码实现代码

    英文同数字 <?php Header("Content-type:image/png"); //定义header,声明图片文件,最好是png,无版权之扰;  //生成新的四位整数验证码 session_start();//开启session; $authnum_session = '';  $str = 'abcdefghijkmnpqrstuvwxyz1234567890';  //定义用来显示在图片上的数字和字母; $l = strlen($str); //得到字串的长度; 

  • php数字运算验证码的实现代码

    本文实例讲述了php实现数字运算验证码的方法,具体实现方法如下: <?php getValidate(100,30); function getValidate($w,$h){ $img = imagecreate($w,$h); $gray = imagecolorallocate($img,255,255,255); $black = imagecolorallocate($img,rand(0,200),rand(0,200),rand(0,200)); $red = imagecolor

  • PHP写的简单数字验证码实例

    用PHP写的随机生成的5位数字验证码 $yzm = ""; for($i=0;$i<5;$i++) { $a = rand(0,9); $yzm.= $a; } echo $yzm; 以上这篇PHP写的简单数字验证码实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • 支持中文、字母、数字的PHP验证码

    本文以实例演示5种验证码,并介绍生成验证码的函数.PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中. session_start(); getCode(4,60,20); function getCode($num,$w,$h) { $code = ""; for ($i = 0; $i < $num; $i++) { $code .= rand(0, 9); } //4位验证码也可以用rand(1000,9999)直接生成 //将生成

  • 支持中文字母数字、自定义字体php验证码代码

    复制代码 代码如下: <?php /* * Captcha Class base on PHP GD Lib * @author Design * @version 1.0 * @demo * include('captchaClass.php'); * $captchaDemo=new Captcha(); * $captchaDemo->createImage(); */ class Captcha{ //@定义验证码图片高度 private $height; //@定义验证码图片宽度 p

  • JS验证逗号隔开可以是中文字母数字

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <script type="text/javascript"> var refid='dasdasd,dadsad'; var reg =/^([\u0391-\uFFE5\d\w,])*([\u0391-\uFFE5\d\w]+)$/; if(refid != "") { if(reg.exec(refid)) { alert('验证通过'); }else { alert('验证失败'); } }

  • ASP 支持中文的len(),left(),right()的函数代码

    比如一个汉字也只会算一个字节,在排版时如果全是汉字,好说,反正没什么差别,但是如果被操作的字符串有汉字又有英文字母时,就不方便了,以下三个函数可以代替ASP自带的相关函数. 也有注意的地方,如果用在循环中,因不变量"i"也是常用于循环的变量,执行以下函数时,"i"的值会发生变化,如果是调用他的循环中用到相同的变量,会产生未知的结果,请换用其它变量名. 以下用法和len(),left(),right()一样. 程序代码 复制代码 代码如下: Function Strl

  • python 脚本生成随机 字母 + 数字密码功能

    下面一段代码给大家介绍python 脚本生成随机 字母 + 数字密码功能,具体代码如下所述: #coding:utf-8 import random,string def GetPassword(length): # 随机生成数字个数 Ofnum=random.randint(1,length) Ofletter=length-Ofnum # 选中ofnum个数字 slcNum=[random.choice(string.digits) for i in range(Ofnum)] # 选中of

  • Python3使用tesserocr识别字母数字验证码的实现

    一.背景 最近有个需求是从一个后台的留言网站爬取留言数据,后台管理网站必然涉及到了登录,登录就有个验证码的问题必须得解决,由于验证码是从后端生成的,并且不了解其生成规则,那就只能通过图像识别技术来做验证码识别了!通过查阅资料发现Python中的的tesserocr这个库好像使用的比较多,所以对这个库进行了一番研究,并且实现了那个后台网站验证码的识别. 二.准备工作 1. 安装tesserocr 由于我使用的Python版本是python3.5,所以一下所有操作都是基于python3的,如果有py

  • PHP实现字母数字混合验证码功能

    一款简单的PHP实现字母数字混合验证码,支持自定义验证码.验证码图片.宽度.高度.个数.背景图片等 验证码调用地址:Application\Home\Controller\CodeController.class.php Vendor('Vcode.Vcode', '', '.class.php'); $config = array("width" => 100, "height" => 36, "count" => 4, &q

  • win10安装tesserocr配置 Python使用tesserocr识别字母数字验证码

    链接:https://pan.baidu.com/s/1l2yiba7ZTPUTf41ZnJ4PYw 提取码:t3bq win10安装tesserocr 首先需要下载tesseract,它为tesserocr提供底层支持.具体下载官方路径:https://github.com/UB-Mannheim/tesseract/wiki,选择对应的系统版本,可以选择一个相对不带dev的稳定版本下载,如:tesseract-ocr-setup-3.05.02-20180621.exe.然后一路安装,唯一记

  • Android判断字符串中是否含字母、中文或数字

    Android 中文俗称安卓或安致,是一个以Linux为基础的开放源代码移动操作系统,主要用于智能手机和平板电脑,由Google成立的Open Handset Alliance(OHA,开放手持设备联盟)持续领导与开发中.目前Google发布Android的最新正式版本为Android 6.0"Marshmallow". Android系统最初由安迪·鲁宾(Andy Rubin)等人开发制作,最初开发这个系统的目的是创建一个数码相机的先进操作系统:但是后来发现市场需求不够大,加上智能手

  • php+正则将字符串中的字母数字和中文分割

    如:$str="php如何将字 符串中322的字母数字sf f45d和中文_分割?";按数字或字母分割. 复制代码 代码如下: $str = "php如何将字 符串中322的字母数字Asf f45d和中文_分割?";$arr = preg_split("/([a-zA-Z0-9]+)/", $str, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);  print_r($arr); 结果 复制

随机推荐