找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 848|回复: 0
打印 上一主题 下一主题

PHP生成Gif图片验证码

[复制链接]

2600

主题

2600

帖子

7736

积分

论坛元老

Rank: 8Rank: 8

积分
7736
跳转到指定楼层
楼主
发表于 2018-2-14 08:11:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            先看效果图



字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置。如:
[U]复制代码[/U] 代码如下:
private static $FontFilePath = "static/font/"; //相对地本代码文件的位置
private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf"); //
完整代码如下:
[U]复制代码[/U] 代码如下:
/**
  说明: 验证码生成类,支持生成Gif图片验证码(带噪点,干扰线,网格,随机色背景,随机自定义字体,倾斜,Gif动画)
  服务端:
  $mod = strtolower(isset($_REQUEST["mod"]) ? $_REQUEST["mod"] : "");
  if($mod == "code"){
  echo SecurityCode:raw(4, 1, 120, 30, 5, 10, 100, "secode");
  die();
  }
  调用:

  验证:
  $reqCode = strtolower(isset($_REQUEST["secode"]) ? $_REQUEST["secode"] : "");  //请求的验证码
  $sessionCode = strtolower(isset($_SESSION["secode"]) ? $_SESSION["secode"] : ""); //会话生成的验证码
  if($reqCode != $sessionCode){
  echo "安全验证码错误!";
  die();
  }
*/
$mod = strtolower(isset($_REQUEST["mod"]) ? $_REQUEST["mod"] : "");
if ($mod == "code") {
    echo SecurityCode:raw(4, 15, 100, 27, 10, 2, 100, "secode");
    die();
}
//安全验证码类
class SecurityCode {
    private static $Debug = 0;
    private static $Code = '';
    private static $Chars = 'bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY34568';
    //private static $Chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
    private static $TextGap = 20;
    private static $TextMargin = 5;
    private static $FontFilePath = "static/font/"; //相对地本代码文件的位置
    private static $FontFileName =array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf"); //
    private static $Img = 'GIF89a'; //GIF header 6 bytes   
    private static $BUF = Array();
    private static $LOP = 0;
    private static $DIS = 2;
    private static $COL = -1;
    private static $-1;
    /**
      生成GIF图片验证
      @param int $L 验证码长度
      @param int $F 生成Gif图的帧数
      @param int $W 宽度
      @param int $H 高度
      @param int $MixCnt 干扰线数
      @param int $lineGap 网格线间隔
      @param int $noisyCnt 澡点数
      @param int $sessionName 验证码Session名称
     */
    public static function Draw($L = 4, $F = 1, $W = 150, $H = 30, $MixCnt = 2, $lineGap = 0, $noisyCnt = 10, $sessionName = "Code") {
        ob_start();
        ob_clean();
        for ($i = 0; $i
        if (!isset($_SESSION))
            session_start();
        $_SESSION[$sessionName] = strtolower(self:Code);
        $bgRGB = array(rand(0, 255), rand(0, 255), rand(0, 255));
        //生成一个多帧的GIF动画
        for ($i = 0; $i
            //背景色
            $bgColor = imagecolorallocate($img, $bgRGB[0], $bgRGB[1], $bgRGB[2]);
            ImageColorTransparent($img, $bgColor);
            unset($bgColor);
            //添加噪点
            $maxNoisy = rand(0, $noisyCnt);
            $noisyColor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
            for ($k = 0; $k
            //添加网格
            if ($lineGap > 0) {
                for ($m = 0; $m
            // 添加干扰线
            for ($k = 0; $k
            //第一帧忽略文字
            if ($i != 0 || $F
            ImageGif($img);
            Imagedestroy($img);
            $Imdata[] = ob_get_contents();
            OB_clean();
        }
        unset($W, $H, $B);
        if (self:Debug) {
            echo $_SESSION['code'];
            echo '', Var_Dump($Imdata), '';
            die();
        }
        header('Content-type:image/gif');
        return self::CreateGif($Imdata, 20);
        unset($Imdata);
    }
    private static function CreateGif($GIF_src, $GIF_dly = 10, $GIF_lop = 0, $GIF_dis = 0, $GIF_red = 0, $GIF_grn = 0, $GIF_blu = 0, $GIF_mod = 'bin') {
        if (!is_array($GIF_src) && !is_array($GIF_tim)) {
            throw New Exception('Error:' . __LINE__ . ',Does not supported function for only one image!!');
            die();
        }
        self:LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
        self:DIS = ($GIF_dis > -1) ? (($GIF_dis  -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn
    private static function AddHeader() {
        $i = 0;
        if (ord(self:BUF[0]{10}) & 0x80) {
            $i = 3 * (2 > 8) & 0xFF) . "\0";
        }
        unset($i);
    }
    private static function AddFrames($i, $d) {
        $L_str = 13 + 3 * (2 > 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
        if (self:COL > -1 && ord(self:BUF[$i]{10}) & 0x80) {
            for ($j = 0; $j > 0) & 0xFF && ord($L_rgb{3 * $j + 1}) == (self:COL >> 8) & 0xFF && ord($L_rgb{3 * $j + 2}) == (self:COL >> 16) & 0xFF) {
                    $L_ext = "!\xF9\x04" . chr((self:DIS > 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
                    break;
                }
            }
        }
        switch ($L_tmp{0}) {
            case '!':
                $L_($L_tmp, 8, 10);
                $L_tmp = substr($L_tmp, 18, strlen($L_tmp) - 18);
                break;
            case ',':
                $L_($L_tmp, 0, 10);
                $L_tmp = substr($L_tmp, 10, strlen($L_tmp) - 10);
                break;
        }
        if (ord(self::$BUF[$i]{10}) & 0x80 && self::$> -1) {
            if ($G_len == $L_len) {
                if (self::Compare($G_rgb, $L_rgb, $G_len)) {
                    self::$Img .= ($L_ext . $L_$L_tmp);
                } else {
                    $byte = ord($L_img{9});
                    $byte |= 0x80;
                    $byte &= 0xF8;
                    $byte |= (ord(self::$BUF[0]{10}) & 0x07);
                    $L_img{9} = chr($byte);
                    self::$Img .= ($L_ext . $L_$L_rgb . $L_tmp);
                }
            } else {
                $byte = ord($L_img{9});
                $byte |= 0x80;
                $byte &= 0xF8;
                $byte |= (ord(self::$BUF[$i]{10}) & 0x07);
                $L_img{9} = chr($byte);
                self::$Img .= ($L_ext . $L_$L_rgb . $L_tmp);
            }
        } else {
            self::$Img .= ($L_ext . $L_$L_tmp);
        }
        self::$;
    }
    private static function Compare($G_Block, $L_Block, $Len) {
        for ($i = 0; $i
}
用法在类开头的注释里。
            
            
您可能感兴趣的文章:
  • php图片验证码代码
  • php5 图片验证码实现代码
  • PHP图片验证码制作实现分享(全)
  • 一漂亮的PHP图片验证码实例
  • PHP生成图片验证码、点击切换实例
  • php生成图片验证码-附五种验证码
  • PHP编写的图片验证码类文件分享
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端