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

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

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

  1. 1classgd.class.php
  1. <?php
  2. Class Captcha{
  3. private $_fontfile='';
  4. private $_size=36;
  5. private $_width=200;
  6. private $_height=100;
  7. private $_length=4;
  8. private $_image=null;
  9. private $_snow=0;
  10. private $_pixel=0;
  11. private $_line=0;
  12. public function __construct($config=array()){
  13. if(is_array($config)&&count($config)>0){
  14. if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
  15. $this->_fontfile=$config['fontfile'];
  16. }else{
  17. return false;
  18. }
  19. if(isset($config['size'])&&$config['size']>0){
  20. $this->_size=(int)$config['size'];
  21. }
  22. if(isset($config['width'])&&$config['width']>0){
  23. $this->_width=(int)$config['width'];
  24. }
  25. if(isset($config['height'])&&$config['height']>0){
  26. $this->_height=(int)$config['height'];
  27. }
  28. if(isset($config['length'])&&$config['length']>0){
  29. $this->_length=(int)$config['length'];
  30. }
  31. if(isset($config['snow'])&&$config['snow']>0){
  32. $this->_snow=(int)$config['snow'];
  33. }
  34. if(isset($config['pixel'])&&$config['pixel']>0){
  35. $this->_pixel=(int)$config['pixel'];
  36. }
  37. if(isset($config['line'])&&$config['line']>0){
  38. $this->_line=(int)$config['line'];
  39. }
  40. $this->_image=imagecreatetruecolor($this->_width,$this->_height);
  41. return $this->_image;
  42.  
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. public function getCaptcha(){
  49. $white=imagecolorallocate($this->_image,255,255,255);
  50. imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
  51. $str=$this->_generateStr($this->_length);
  52. if(false===$str){
  53. return false;
  54. }
  55. $fontfile=$this->_fontfile;
  56. for($i=0;$i<$this->_length;$i++){
  57. $size=$this->_size;
  58. $angle=mt_rand(-30,30);
  59. $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
  60. $y=ceil($this->_height/1.5);
  61. $color=$this->_getRandColor();
  62. //针对中文字符截取
  63. //$text=mb_substr($str,$i,1,'utf-8');
  64. $text=$str{$i};
  65. imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
  66. }
  67. if($this->_snow){
  68. $this->_getSnow();
  69.  
  70. }else{
  71. if($this->_pixel){
  72. $this->_getPixel();
  73.  
  74. }
  75. if($this->_line){
  76. $this->_getLine();
  77. }
  78. }
  79. header('content-type:image/png');
  80. imagepng($this->_image);
  81. imagedestroy($this->_image);
  82. return strtolower($str);
  83. }
  84. private function _getSnow(){
  85. for($i=1;$i<=$this->_snow;$i++){
  86. imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
  87. }
  88. }
  89. private function _getPixel(){
  90. for($i=1;$i<=$this->_pixel;$i++){
  91. imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
  92. }
  93. }
  94. private function _getLine(){
  95. for($i=1;$i<=$this->_line;$i++){
  96. 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());
  97.  
  98. }
  99. }
  100. private function _generateStr($length=4){
  101. if($length<1 || $length>30){
  102. return false;
  103. }
  104. $chars=array(
  105. 'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
  106. 'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
  107. 1,2,3,4,5,6,7,8,9
  108. );
  109. $str=join('',array_rand(array_flip($chars),$length));
  110. return $str;
  111. }
  112. private function _getRandColor(){
  113. return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  114. }
  115. }
  116. ?>

2、testCaptcha.php

  1. <?php
  2. require_once 'classgd.class.php';
  3. $config=array(
  4. 'fontfile'=>'fonts/simfang.ttf', //引入字体文件
  5. //'snow'=>50,
  6. 'pixel'=>100,
  7. 'line'=>10
  8. );
  9. $captcha=new Captcha($config);
  10. $captcha->getCaptcha();
  11. ?>

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. Express开发性能优化

    1.使用浏览器缓存 在app.js里添加 var CACHETIME = 60 * 1000 * 60 * 24 * 30; app.use(express.static(path.join(__di ...

  2. Firebird shadow

    火鸟数据库的shadow,即实时镜像. 主库发生变化,shadow也跟随变化,防止任何意外造成主库损坏无法使用,当然shadow可以有多个. 1.创建shadow的准备:修改Firebird.conf ...

  3. Python__函数和代码复用

    主要内容 函数的定义和使用 实例:七段数码管的绘制 代码复用与函数递归 PyInstall库的使用 实例:科赫雪花小包裹 函数的定义与使用 函数的理解与定义 函数的使用及调用过程 函数的参数传递 函数 ...

  4. 悟空模式-java-单例模式

    [那座山,正当顶上,有一块仙石.其石有三丈六尺五寸高,有二丈四尺围圆.三丈六尺五寸高,按周天三百六十五度:二丈四尺围圆,按政历二十四气.上有九窍八孔,按九宫八卦.四面更无树木遮阴,左右倒有芝兰相衬.盖 ...

  5. Linux安装jdk,编写helloworld程序

    今天学习了Linux安装jdk,做个笔记记录一下. 第一步,确定Linux是32位的还是64位的,然后到oracle官网上下载对应版本的jdk,一般下载.tar.gz文件.查看Linux的版本的命令是 ...

  6. ARM体系结构和汇编指令

    第一节 可编程器件的编程原理 1. 可编程器件的特点 1 . CPU在固定频率的时钟控制下节奏运行 2 . CPU可以通过总线读取外部存储设备中的二进制指令集,然后解码执行 3 . 这些可以被CPU解 ...

  7. Mysql-安装指南

    1.设置用户名密码 首次登录后修改密码如下: 如果密码设置太过简单会报以下错误 mysql修改密码Your password does not satisfy the current policy r ...

  8. Getting Started with Erlang

    Getting Started with Erlang Erlang is a great language that lets you build highly concurrent applica ...

  9. AE中IHookHelper的用法 来自http://blog.sina.com.cn/s/blog_6faf711d0100xs1x.html

    IHookHelper 主要在用在自定义类型于AE带的的ICommand或ITool等, 1.实例化IHookHelper 对象: IHookHelper m_hookHelper = new Hoo ...

  10. jQuery实现大图轮播

    css样式: *{    margin: 0;    padding: 0;}ul{    list-style:none;}.slideShow{    width: 620px;    heigh ...