search
HomeBackend DevelopmentPHP TutorialPHP generates Gif image verification code_PHP tutorial

Look at the renderings first
PHP generates Gif image verification code_PHP tutorial
PHP generates Gif image verification code_PHP tutorial

The paths of fonts and font files need to be set in $FontFilePath and $FontFileName in the class. For example:

Copy code The code is as follows:

private static $FontFilePath = "static/font/"; //relatively The location of this code file
private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf"); //

The complete code is as follows:
Copy code The code is as follows:

/**
  说明: 验证码生成类,支持生成Gif图片验证码(带噪点,干扰线,网格,随机色背景,随机自定义字体,倾斜,Gif动画)

  服务端:
  $mod = strtolower(isset($_REQUEST["mod"]) ? $_REQUEST["mod"] : "");
  if($mod == "code"){
  echo SecurityCode::Draw(4, 1, 120, 30, 5, 10, 100, "secode");
  die();
  }
  调用: PHP generates Gif image verification code_PHP tutorial
  验证:
  $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::Draw(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 $IMG = -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             self::$Code .= SubStr(self::$Chars, mt_rand(0, strlen(self::$Chars) - 1), 1);
        }

        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             $img = ImageCreate($W, $H);

            //背景色
            $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                 imagesetpixel($img, rand(0, $W), rand(0, $H), $noisyColor);
            }

            //添加网格
            if ($lineGap > 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 = "!xF9x04" . chr((self::$DIS > 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "x0x0";
        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 = "!xF9x04" . 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);
    }

}


用法在类开头的注释里。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/824928.htmlTechArticle先看效果图 字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置。如: 复制代码 代码如下: private static $FontFilePath = "static/font/...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools