驗證碼類

驗證碼類 captcha.class.php

<?php
class captcha{
    private $width;
    private $height;
    private $fontfile="mtcorsva.ttf";
    private $codeNum;
    private $code;
    private $im;

    function __construct($width=80, $height=20, $codeNum=4)
    {
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
    }

    public function showImg()
    {
        //創(chuàng)建圖片
        $this->createImg();
        //設置干擾元素
        $this->setDisturb();
        //設置驗證碼
        $this->setCaptcha();
        //輸出圖片
        $this->outputImg();
    }

    public function getCaptcha()
    {
        return $this->code;
    }

    private function createImg()
    {
        $this->im = imagecreatetruecolor($this->width, $this->height);
        $bgColor = imagecolorallocate($this->im, 208, 208, 208);
        imagefill($this->im, 0, 0, $bgColor);
    }

    private function setDisturb()
    {
        $area = ($this->width * $this->height) / 20;
        $disturbNum = ($area > 250) ? 250 : $area;
        //加入點干擾
        // for ($i = 0; $i < $disturbNum; $i++) {
        //     $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
        //     imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
        // }
        //加入弧線
        for ($i = 0; $i <= 5; $i++) {
            $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
            imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
        }
    }

    private function createCode()
    {
        $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";

        for ($i = 0; $i < $this->codeNum; $i++) {
            $this->code .= $str{rand(0, strlen($str) - 1)};
        }
    }

    private function setCaptcha()
    {
        $this->createCode();

        for ($i = 0; $i < $this->codeNum; $i++) {
            $color = imagecolorallocate($this->im, rand(0, 50), rand(50, 250), rand(0, 250));
            $size = rand(floor($this->height / 2), floor($this->height / 1.5));
            $x = floor($this->width / $this->codeNum) * $i + 5;
            $y = rand(floor($this->height / 2), floor($this->height / 1.2));
            //imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
            imagettftext($this->im, $size, 0, $x, $y, $color, $this->fontfile ,$this->code{$i});
        }
    }

    private function outputImg()
    {
        if (imagetypes() & IMG_JPG) {
            header('Content-type:image/jpeg');
            imagejpeg($this->im);
        } elseif (imagetypes() & IMG_GIF) {
            header('Content-type: image/gif');
            imagegif($this->im);
        } elseif (imagetype() & IMG_PNG) {
            header('Content-type: image/png');
            imagepng($this->im);
        } else {
            die("Don't support image type!");
        }
    }
    function __destruct(){
        imagedestroy($this->image);
    }
}

生成驗證碼圖片,驗證碼存入session imagecode.php

<?php
session_start();
$act  = isset($_GET['act']) ? trim($_GET['act']) : "";
if($act=="verifycode")
{
    $width   = isset($_GET['width']) ? intval(trim($_GET['width'])) : "120";
    $height  = isset($_GET['height']) ? intval(trim($_GET['height'])) : "40";
    require '../../captcha.class.php';
    $code = new captcha($width, $height, 4);
    $code->showImg();
    $_SESSION["verifycode"]= strtolower($code->getCaptcha());
}

頁面調(diào)用驗證碼

<img id="checkCodeImg" onclick="refreshCc()" src="../imagecode.php?act=verifycode&width=100&height=36" />
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容