您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP生成Gif图片验证码_PHP教程

2026/2/13 9:05:30发布12次查看
先看效果图
字体及字体文件的路径需要在类中$fontfilepath及$fontfilename中设置。如:
复制代码 代码如下:
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); //
完整代码如下:
复制代码 代码如下:
0) {
                for ($m = 0; $m                     imageline($img, $m * $linegap, 0, $m * $linegap, $h, $noisycolor);
                }
                for ($n = 0; $n                     imageline($img, 0, $n * $linegap, $w, $n * $linegap, $noisycolor);
                }
            }
            unset($noisycolor);
            // 添加干扰线
            for ($k = 0; $k                 $wr = mt_rand(0, $w);
                $hr = mt_rand(0, $w);
                $linecolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
                imagearc($img, $w - floor($wr / 2), floor($hr / 2), $wr, $hr, rand(90, 180), rand(180, 270), $linecolor);
                unset($linecolor);
                unset($wr, $hr);
            }
            //第一帧忽略文字
            if ($i != 0 || $f                 //文字           
                $forecolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
                for ($j = 0; $j                     $fontfile = self::$fontfilepath . self::$fontfilename[rand(0, count(self::$fontfilename) - 1)];
                    if (!file_exists($fontfile))
                        imagestring($img, 4, self::$textmargin + $j * self::$textgap, ($h - rand($h / 2, $h)), self::$code[$j], $forecolor);
                    else
                        imagettftext($img, rand(15, 18), rand(-15, 15), self::$textmargin + $j * self::$textgap, ($h - rand(7, 10)), $forecolor, $fontfile, self::$code[$j]);
                }
                unset($forecolor);
            }
            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         self::$col = ($gif_red > -1 && $gif_grn > -1 && $gif_blu > -1) ? ($gif_red | ($gif_grn         for ($i = 0, $src_count = count($gif_src); $i             if (strtolower($gif_mod) == 'url') {
                self::$buf[] = fread(fopen($gif_src[$i], 'rb'), filesize($gif_src[$i]));
            } elseif (strtolower($gif_mod) == 'bin') {
                self::$buf[] = $gif_src[$i];
            } else {
                throw new exception('error:' . __line__ . ',unintelligible flag (' . $gif_mod . ')!');
                die();
            }
            if (!(substr(self::$buf[$i], 0, 6) == 'gif87a' or substr(self::$buf[$i], 0, 6) == 'gif89a')) {
                throw new exception('error:' . __line__ . ',source ' . $i . ' is not a gif image!');
                die();
            }
            for ($j = (13 + 3 * (2                 switch (self::$buf[$i]{$j}) {
                    case '!':
                        if ((substr(self::$buf[$i], ($j + 3), 8)) == 'netscape') {
                            throw new exception('error:' . __line__ . ',could not make animation from animated gif source (' . ($i + 1) . ')!');
                            die();
                        }
                        break;
                    case ';':
                        $k = false;
                        break;
                }
            }
        }
        self::addheader();
        for ($i = 0, $count_buf = count(self::$buf); $i             self::addframes($i, $gif_dly);
        }
        self::$img .= ';';
        return (self::$img);
    }
    private static function addheader() {
        $i = 0;
        if (ord(self::$buf[0]{10}) & 0x80) {
            $i = 3 * (2             self::$img .= substr(self::$buf[0], 6, 7);
            self::$img .= substr(self::$buf[0], 13, $i);
            self::$img .= !\377\13netscape2.0\3\1 . chr(self::$lop & 0xff) . chr((self::$lop >> 8) & 0xff) . \0;
        }
        unset($i);
    }
    private static function addframes($i, $d) {
        $l_str = 13 + 3 * (2         $l_end = strlen(self::$buf[$i]) - $l_str - 1;
        $l_tmp = substr(self::$buf[$i], $l_str, $l_end);
        $g_len = 2         $l_len = 2         $g_rgb = substr(self::$buf[0], 13, 3 * (2         $l_rgb = substr(self::$buf[$i], 13, 3 * (2         $l_ext = !\xf9\x04 . chr((self::$dis > 0) & 0xff) . chr(($d >> 8) & 0xff) . \x0\x0;
        if (self::$col > -1 && ord(self::$buf[$i]{10}) & 0x80) {
            for ($j = 0; $j                 if (ord($l_rgb{3 * $j + 0}) == (self::$col >> 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_img = substr($l_tmp, 8, 10);
                $l_tmp = substr($l_tmp, 18, strlen($l_tmp) - 18);
                break;
            case ',':
                $l_img = substr($l_tmp, 0, 10);
                $l_tmp = substr($l_tmp, 10, strlen($l_tmp) - 10);
                break;
        }
        if (ord(self::$buf[$i]{10}) & 0x80 && self::$img > -1) {
            if ($g_len == $l_len) {
                if (self::compare($g_rgb, $l_rgb, $g_len)) {
                    self::$img .= ($l_ext . $l_img . $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_img . $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_img . $l_rgb . $l_tmp);
            }
        } else {
            self::$img .= ($l_ext . $l_img . $l_tmp);
        }
        self::$img = 1;
    }
    private static function compare($g_block, $l_block, $len) {
        for ($i = 0; $i             if ($g_block{3 * $i + 0} != $l_block{3 * $i + 0} || $g_block{3 * $i + 1} != $l_block{3 * $i + 1} || $g_block{3 * $i + 2} != $l_block{3 * $i + 2}) {
                return (0);
            }
        }
        return (1);
    }
}
用法在类开头的注释里。
http://www.bkjia.com/phpjc/824928.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/824928.htmltecharticle先看效果图 字体及字体文件的路径需要在类中$fontfilepath及$fontfilename中设置。如: 复制代码 代码如下: private static $fontfilepath = static/font/...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product