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. [转]Using TRY...CATCH in Transact-SQL

    本文转自:https://technet.microsoft.com/en-us/library/ms179296(v=sql.105).aspx Using TRY...CATCH in Trans ...

  2. Guidlines and rules About Overwriting hashCode()

    Preface "The code is more what you’d call guidelines than actual rules" – truer words were ...

  3. zoj Continuous Login

    Continuous Login Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Pierre is rec ...

  4. 对JSON数据的解析(数据推送接口)

    package com.liuqi.www; import java.util.HashMap; import java.util.Map; import org.springframework.st ...

  5. spring boot入门笔记 (一) - 一个简单的说明+一个案例

    spring boot 简化开发:把平时开发者最常用的到一些步骤,按照开发者的习惯,把能包装的就包装成一些固有的工具类(就比如我们之前连接数据库时常写的DB工具类).当然,是在原有的spring框架的 ...

  6. YII 用gii生成modules模块下的mvc

    1.生成model ModelPath设置为: application.modules.[moduleName].models 2.生成CURD ModelClass设置为: application. ...

  7. C语言程序设计基础知识点概括

    C语言程序设计基础知识点概括 C语言程序设计基础知识点1.函数是C语言的基本构成单位.main函数是C语言程序的唯一入口.2.C语言程序开发过程. 编译过程:将以.c或.cpp结尾的源程序文件经过编译 ...

  8. request获取当前用户

    1.request.getRemoteUser();//获取当前缓存的用户,比如Spring Security做权限控制后就会将用户登录名缓存到这里 request.getRemoteAddr();/ ...

  9. ubuntn安装

    环境win7 64 ,在vmn中安装ubuntn,需要开启虚拟化操作步骤: 1.首先进入BIOS,我的是thinkphpE440,在开机联想界面出现的那刻按F1: 2.选择切换到security页面, ...

  10. Ubuntu加入opencv库的环境变量

    1.用gedit打开/etc/ld.so.conf 终端输入: sudo gedit /etc/ld.so.conf 文件末行加入:include /usr/loacal/lib .然后终端执行指令: ...