<?php
require_once 'string.func.php';
//通过GD库做验证码
/**
 *添加验证文字
 * @param int $type
 * @param int $length
*/
function buildRandomString($type=1,$length=4){
    $row='';
    if($type==1){
        $row=join('',range(0, 9));
    }else if($type==2){
        $row=join('', array_merge(range('a','z'),range('A', 'Z')));
    }else if($type==3){
        $row=join('', array_merge(range('a','z'),range('A', 'Z'),range(0, 9)));
    };
    $row=str_shuffle($row);

    $row=substr($row,0,$length);
    return $row;
}
/**
 * 生成缩略图
 * @param int $type //包含数字或者英文
 * @param int $length 多少个字符
 * @param int $pixel 干扰小点的密度
 * @param int $dst_h 干扰线的密度
 * @param string //验证码在$_SESSION中的名字
 */

function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){
    //session_start();
    //创建画布
    $width = 100;
    $height = 40;
    $image = imagecreatetruecolor ( $width, $height );
    $white = imagecolorallocate ( $image, 255, 255, 255 );
    $black = imagecolorallocate ( $image, 0, 0, 0 );
    //用填充矩形填充画布
    imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );
    $chars = buildRandomString ( $type, $length );
    $_SESSION [$sess_name] = $chars;
    //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" );
    $fontfiles = array ("SIMKAI.TTF" );
    //由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体
    for($i = 0; $i < $length; $i ++) {
        $size = mt_rand ( 14, 18 );
        $angle = mt_rand ( - 15, 15 );
        $x = 5 + $i * $size;
        $y = mt_rand ( 20, 26 );
        $fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
        $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
        $text = substr ( $chars, $i, 1 );
        imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
    }
    if ($pixel) {
        for($i = 0; $i < 50; $i ++) {
            imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black );
        }
    }
    if ($line) {
        for($i = 1; $i < $line; $i ++) {
            $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
            imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color );
        }
    }
    header ( "content-type:image/gif" );
    imagegif ( $image );
    imagedestroy ( $image );
}

主要要点:1、如果前面没有申明session_start();则需要申明;2、字体可以在cmf输入fonts下载到自己定义的fonts文件夹;3、$_SESSION [$sess_name]可以通过$_POST获得用户输入的验证码进行比较。

php完整验证码代码的更多相关文章

  1. 兄弟,你爬虫基础这么好,需要研究js逆向了,一起吧(有完整JS代码)

    这几天的确有空了,看更新多快,专门研究了一下几个网站登录中密码加密方法,比起滑块验证码来说都相对简单,适合新手js逆向入门,大家可以自己试一下,试不出来了再参考我的js代码.篇幅有限,完整的js代码在 ...

  2. 谷歌开源项目Google Preview Image Extractor(PIEX) (附上完整demo代码)

    前天偶然看到谷歌开源项目中有一个近乎无人问津的项目Google Preview Image Extractor(PIEX) . 项目地址: https://github.com/google/piex ...

  3. openssl对数组加密解密的完整实现代码

    本例是用C实现的对一个数组进行加密,加密到第二个数组,然后解密到另一个数组的完整实现代码. #include <stdio.h> #include <string.h> #in ...

  4. 快速双边滤波 附完整C代码

    很早之前写过<双边滤波算法的简易实现bilateralFilter>. 当时学习参考的代码来自cuda的样例. 相关代码可以参阅: https://github.com/johng12/c ...

  5. 基于RNN的音频降噪算法 (附完整C代码)

    前几天无意间看到一个项目rnnoise. 项目地址: https://github.com/xiph/rnnoise 基于RNN的音频降噪算法. 采用的是 GRU/LSTM 模型. 阅读下训练代码,可 ...

  6. 音频压缩编码 opus 附完整C++代码示例

    绝大数人都知道mp3格式编码,以及aac,amr等压缩格式编码. 而在语音通信界有一个强悍的音频格式编码opus. 经过实测,压缩比最高可以达到1:10. 100KB 压缩后 10KB 虽然是有损压缩 ...

  7. 音频降噪算法 附完整C代码

    降噪是音频图像算法中的必不可少的. 目的肯定是让图片或语音 更加自然平滑,简而言之,美化. 图像算法和音频算法 都有其共通点. 图像是偏向 空间 处理,例如图片中的某个区域. 图像很多时候是以二维数据 ...

  8. mser 最大稳定极值区域(文字区域定位)算法 附完整C代码

    mser 的全称:Maximally Stable Extremal Regions 第一次听说这个算法时,是来自当时部门的一个同事, 提及到他的项目用它来做文字区域的定位,对这个算法做了一些优化. ...

  9. 音频自动增益 与 静音检测 算法 附完整C代码

    前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用于评估一定长度音频的音量强度, 而分析之后,很多类似的需求,肯定是做音频增益,提高音量诸如此类做法. ...

随机推荐

  1. CocoaPods不更新spec仓库进行install/update

    最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...

  2. oracle Imp和exp以及导入常见的错误

    一 1) 数据库对象已经存在 一般情况, 导入数据前应该彻底删除目标数据下的表, 序列, 函数/过程,触发器等; 数据库对象已经存在, 按缺省的imp参数, 则会导入失败 如果用了参数ignore=y ...

  3. Codeigniter数据库操作

    //查询: $query = $this->db_query("SELECT * FROM table"); ================================ ...

  4. Linux防火墙iptables学习笔记(三)iptables命令详解和举例[转载]

     Linux防火墙iptables学习笔记(三)iptables命令详解和举例 2008-10-16 23:45:46 转载 网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能 ...

  5. sublimeText3安装package control和禁止弹出更新下载弹窗

    1.sublimeText3安装package control import urllib.request,os; pf = 'Package Control.sublime-package'; ip ...

  6. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  7. Properties文件,Data,Calendar类的使用

    package cn.hncu.day9; import java.io.FileInputStream;import java.io.FileNotFoundException;import jav ...

  8. Chapter 5. The Gradle Wrapper 关于gradle wrapper

    Most tools require installation on your computer before you can use them. If the installation is eas ...

  9. RedHat7安装Sublime Text 3

    下载Sublime Text 3 # wget http://c758482.r82.cf2.rackcdn.com/sublime_text_3_build_3083_x64.tar.bz2 解压S ...

  10. Windows Server 2008 R2中关闭“IE增强的安全配置”

    当在Windows Sever 2008 R2中运动IE8的时候会发现默认情况下IE启用了增强的安全配置,为了方便而且是在内网的情况下我们可以关闭IE8的增强安全配置,操作很简单如下步骤. 一,以本机 ...