PHP生成Gif图片验证码(2)

// 添加干扰线
            for ($k = 0; $k < $MixCnt; $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 <= 1) {
                //文字           
                $foreColor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
                for ($j = 0; $j < $L; $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 '<pre>', Var_Dump($Imdata), '</pre>';
            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 < 3) ? $GIF_dis : 3) : 2;
        self::$COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
        for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $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 << (ord(self::$BUF[$i]{10}) & 0x07))), $k = TRUE; $k; $j++) {
                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 < $count_buf; $i++) {
            self::AddFrames($i, $GIF_dly);
        }
        self::$Img .= ';';
        return (self::$Img);
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/7bf30abdf9c3d6152834b297762c3169.html