php实现随机数字、字母的验证码

  可自定义生成验证码文字的大小、数量、干扰项等等,也可以自定义验证文字的字体。。。

  废话不多说,直接上代码:

1、classgd.class.php
<?php
Class Captcha{
private $_fontfile='';
private $_size=36;
private $_width=200;
private $_height=100;
private $_length=4;
private $_image=null;
private $_snow=0;
private $_pixel=0;
private $_line=0;
public function __construct($config=array()){
if(is_array($config)&&count($config)>0){
if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
$this->_fontfile=$config['fontfile'];
}else{
return false;
}
if(isset($config['size'])&&$config['size']>0){
$this->_size=(int)$config['size'];
}
if(isset($config['width'])&&$config['width']>0){
$this->_width=(int)$config['width'];
}
if(isset($config['height'])&&$config['height']>0){
$this->_height=(int)$config['height'];
}
if(isset($config['length'])&&$config['length']>0){
$this->_length=(int)$config['length'];
}
if(isset($config['snow'])&&$config['snow']>0){
$this->_snow=(int)$config['snow'];
}
if(isset($config['pixel'])&&$config['pixel']>0){
$this->_pixel=(int)$config['pixel'];
}
if(isset($config['line'])&&$config['line']>0){
$this->_line=(int)$config['line'];
}
$this->_image=imagecreatetruecolor($this->_width,$this->_height);
return $this->_image; }
else{
return false;
}
}
public function getCaptcha(){
$white=imagecolorallocate($this->_image,255,255,255);
imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
$str=$this->_generateStr($this->_length);
if(false===$str){
return false;
}
$fontfile=$this->_fontfile;
for($i=0;$i<$this->_length;$i++){
$size=$this->_size;
$angle=mt_rand(-30,30);
$x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
$y=ceil($this->_height/1.5);
$color=$this->_getRandColor();
//针对中文字符截取
//$text=mb_substr($str,$i,1,'utf-8');
$text=$str{$i};
imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
}
if($this->_snow){
$this->_getSnow(); }else{
if($this->_pixel){
$this->_getPixel(); }
if($this->_line){
$this->_getLine();
}
}
header('content-type:image/png');
imagepng($this->_image);
imagedestroy($this->_image);
return strtolower($str);
}
private function _getSnow(){
for($i=1;$i<=$this->_snow;$i++){
imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
}
}
private function _getPixel(){
for($i=1;$i<=$this->_pixel;$i++){
imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
}
}
private function _getLine(){
for($i=1;$i<=$this->_line;$i++){
imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); }
}
private function _generateStr($length=4){
if($length<1 || $length>30){
return false;
}
$chars=array(
'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
1,2,3,4,5,6,7,8,9
);
$str=join('',array_rand(array_flip($chars),$length));
return $str;
}
private function _getRandColor(){
return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
}
}
?>

2、testCaptcha.php

<?php
require_once 'classgd.class.php';
$config=array(
'fontfile'=>'fonts/simfang.ttf', //引入字体文件
//'snow'=>50,
'pixel'=>100,
'line'=>10
);
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>

php实现随机数字、字母的验证码的更多相关文章

  1. 随机发送n位数字+字母的验证码

    ''' 随机发送n位数字+字母的验证码 ''' import random def get_verified(length): code = '' for i in range(length): nu ...

  2. javascript原生 实现数字字母混合验证码

    实现4位数 数字字母混合验证码(数字+大写字母+小写字母) ASCII 字符集中得到3个范围: 1. 48-57 表示数字0-9 2. 65-90 表示大写字母 3. 97-122 表示小写字母 范围 ...

  3. QTP生成随机数字+字母

    以下函数实现随机生成17位数(包括字母和数字),仍有改进的空间,可根据具体要求适当修改 Dim targetstring '调用返回函数给变量.Function过程通过函数名返回一个值 targets ...

  4. C# 生成四位数字字母混合验证码

    private static void Rand() { var arr = new List<string>(); ; i < ; i++) { arr.Add(i.ToStrin ...

  5. 用js做数字字母混合的随机四位验证码

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. Servlet实现数字字母验证码图片(二)

    Servlet实现数字字母验证码图片(二): 生成验证码图片主要用到了一个BufferedImage类,如下:

  7. js随机生成字母数字组合的字符串 随机动画数字

    效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...

  8. Java随机生成定长纯数字或数字字母混合数

    (转)Java随机生成定长纯数字或数字字母混合数 运行效果图: 具体实现代码

  9. 绘制字母和数字组合的验证码(原生php)

    <?php $font = array('font/FZZQJW.TTF','font/STHUPO.TTF');//字体 $str = '689acdefhjkmnpqrtuvwxyACDEF ...

随机推荐

  1. c#基础学习(0629)之导出Excel方法

    给予NPOI插件的方法,所以首先要下载NPOI插件:vs项目中点击“项目”==>“管理NoGet程序包”==>搜索“NPOI”然后下载==>using引入Controller代码: ...

  2. ASP.NET Url 重写

    protected void Application_BeginRequest(object sender, EventArgs e) { string url = Request.AppRelati ...

  3. .net面试题[转载]

    1.简述private.protected.public.internal修饰符的访问权限. private:私有成员,在类的内部才可以访问. protected:保护成员,该类内部和继承类中可以访问 ...

  4. 如何编写makefile文件

    最近一直在学习makefile是如何编写的.       当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make ...

  5. Facebook回应追踪无账号用户:源于网站插件漏洞

    近日,比利时隐私独立委员一项调查披露Facebook跟踪收集大量网站用户的上网足迹,同时触及那些没有注册Facebook账号的访客. 对此,Facebook公司回应这些恶劣跟踪行为在于社交媒体在安装插 ...

  6. Jsp&Servlet入门级项目全程实录第8讲

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.添加dao public int studentAdd(Connection con,Student studen ...

  7. [日常] nginx与location规则

    =========================================================================2018年3月28日 记录: location = / ...

  8. [PHP] 重回基础(Array相关函数)

    使用函数array_keys(),得到数组中所有的键,参数:数组 $arr=array(); $arr['one']="one"; $arr['two']="two&qu ...

  9. MySQL 批量删除相同前缀的表

    sql 命令批量生成drop命令 需要批量删除表,而MySQL又没有提供相关的功能:一般我们建表也都会使用相同前缀,那么,在不使用工具的情况下可以选择使用sql生成批量删除命令: 如删除以 " ...

  10. 常见排序算法总结 -- java实现

    常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...