当前位置:网站首页>A solution for PHP to implement image login verification code

A solution for PHP to implement image login verification code

2022-06-12 05:25:00 Missed engraving sometimes

PHP Solution to realize image login verification code

HTML Code

    <div class="login">
        <p><input type="text" id="username" name="username" placeholder=" user name " autocomplete="off"></p>
        <p><input type="password" id="password" name="username" placeholder=" password " autocomplete="off"></p>
        <p><input type="text" id="captcha" name="captcha" placeholder=" Verification Code " maxlength="4" autocomplete="off"></p>
        <p><img src="?m=Login&a=loginAccess&act=getCode" id="getCode" alt="" title=" Click refresh verification code "></p>
        <p><button id="loginBtn"> Sign in </button></p>
    </div>

Refresh the picture

        $("#getCode").click(function () {
    
            $(this).attr("src", '?m=Login&a=loginAccess&act=getCode&' + Math.random());
        });

Generate verification code and picture

    case "getCode";
        require_once "libs/vcode.class.php";
        $obj = new vcode();// Instantiation ;
        SetCookie("authcode", $obj->authcode, time() + 30, "/");
        die($obj->output());
        break;

Verification code class library

class vcode
{
    
    public $authcode = '';                            // Verification Code 
    private $width = '';                            // The verification code picture is wide 
    private $height = '';                            // The verification code picture is high 
    private $len = '';                            // Length of verification code 
    private $tilt = array(-30, 30);                // Verification code tilt angle 
    private $font = 'AlteHaasGroteskBold.ttf';    // The font file 
    private $str = '';                            // Verification code base 
    private $im = '';                            // Handle to generate picture 

    // Constructors , Generate verification code .
    function __construct($width = 100, $heigh = 40, $len = 4)
    {
    
        $this->width = $width;
        $this->height = $heigh;
        $this->len = $len;
        //$this->str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        $this->str = '0123456789';

        $str_len = strlen($this->str) - 1;
        for ($i = 0; $i < $len; $i++) {
    
            $this->authcode .= $this->str[rand(0, $str_len)];
        }
    }

    // Create a picture 
    private function imagecreate()
    {
    
        $this->im = imagecreatetruecolor($this->width, $this->height);
    }

    // Interference color 
    private function ext_color()
    {
    
        return imagecolorallocate($this->im, rand(50, 180), rand(50, 180), rand(50, 180));
    }

    // Create interference points 
    private function ext_point()
    {
    
        for ($i = 0; $i < $this->width * 2; $i++) {
    
            imagesetpixel($this->im, rand(1, $this->width - 1), rand(1, $this->height - 1), $this->ext_color());
        }
    }

    // Create interference lines 
    private function ext_line()
    {
    
        for ($i = 0; $i < $this->len; $i++) {
    
            $x1 = rand(1, $this->width - 1);
            $y1 = rand(1, $this->height - 1);
            $x2 = rand(1, $this->width - 1);
            $y2 = rand(1, $this->height - 1);
            imageline($this->im, $x1, $y1, $x2, $y2, $this->ext_color());
        }
    }

    // Write the verification code into the picture ( Unable to join $this->imgstrfloat() Use at the same time )
    private function imgstr()
    {
    
        $old_x = 1;
        for ($i = 0; $i < $this->len; $i++) {
    
            $fontsize = rand(2, 5);        // font size 
            $tmp_1 = $fontsize * 2.5;
            $tmp_2 = $i > 0 ? $tmp_1 : 0;
            $y = rand(1, $this->height / 2);
            $x = rand($old_x + $tmp_2, ($i + 1) * ($this->width) / $this->len - $tmp_1);
            $old_x = $x;
            $color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
            imagestring($this->im, $fontsize, $x, $y, $this->authcode[$i], $color);
        }
    }

    // Write the verification code obliquely into the picture ( Unable to join $this->imgstr() Use at the same time )
    private function imgstrfloat()
    {
    
        $old_x = 1;
        for ($i = 0; $i < $this->len; $i++) {
    
            $fontfloat = rand($this->tilt[0], $this->tilt[1]);
            $fontsize = rand(10, 15);        // font size 
            $tmp_1 = $i > 0 ? $fontsize : 0;
            $y = rand($fontsize + 2, $this->height - 2);
            $x = rand($old_x + $tmp_1 + 2, ($i + 1) * ($this->width) / $this->len - $fontsize - 2);
            $old_x = $x;
            $color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
            imagettftext($this->im, $fontsize, $fontfloat, $x, $y, $color, $this->font, $this->authcode[$i]);
        }
    }

    // Output pictures 
    public function output()
    {
    
        $this->imagecreate();
        $this->imgstr();
        //$this->imgstrfloat();
        $this->ext_point();
        $this->ext_line();
        header('content-type:image/png');
        imagepng($this->im);
        imagedestroy($this->im);
    }
}

@lockdata.cn

原网站

版权声明
本文为[Missed engraving sometimes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120524030056.html