Develop your own Authentication Code Class

On the network, especially on websites that need to log in, authentication codes are simply visible everywhere and come in a variety of forms: static pictures, dynamic pictures, voice, sliders.
Now I'm going to make my own verification code class for generating pictures, of course, in the language php that I eat.
The gd library is required and the corresponding gd2 extension in php.ini must be opened
Start by creating a class, such as I named gcudCaptcha, and then declare several basic properties

<?php


namespace gcud;


class gcudCaptcha
{
//Units appear to be pixels
    private $Weight=150,$Height=50;
}


I've used the gd library before, so I'm just familiar with it
First you need to create a canvas with a specified width and height, then write a method

private function CreateImage(){
//The gd library's method for creating canvas
    imagecreate($this->Weight,$this->Height);
}


Also add the $Image property to receive the created image

private $Image;
    private function CreateImage(){
        $this->Image=imagecreate($this->Weight,$this->Height);
    }


Because the created image needs to be output directly to the browser, then write an output method

private function Output(){
    //Let browsers recognize pictures
    header('Content-Type:image/jpeg');
    //Set output type to jpeg image
    imagejpeg($this->Image);
    //Remember to release resources
    imagedestroy($this->Image);
}


You also need to expose a generated method

    public function Generate(){
        $this->Output();
    }


And create image is called at initialization

    public function __construct()
    {
        $this->CreateImage();
    }


So now the whole code becomes

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;

    public function __construct()
    {
        $this->CreateImage();
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
    }

    private function Output()
    {
        header('Content-Type:image/jpeg');
        imagejpeg($this->Image);
        imagedestroy($this->Image);
    }

    public function Generate()
    {
        $this->Output();
    }
}


Call freely in one php

<?php
//My class is here
require_once 'Vendor\gcud\gcudCaptcha.php';
$gcudCaptcha=new \gcud\gcudCaptcha();
$gcudCaptcha->Generate();


You can see that the browser output a black picture, then set a background color, set the background color RGB property, and write it in the Create Image function

private $BackgroundColorRGB=[127,127,127];
    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

Run the code again to see the gray picture

Next, you'll draw the validation code, add a validation code property and a validation code color property, and initialize it in the constructor

    private $Captcha,$CaptchaRGB=[255,100,37];
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
    }

Also specify a font and draw a verification code

private $Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
private function DrawCaptcha(){
//Parameters: Picture, Font Size, Angle (seems to be 0-360),x-coordinate, y-coordinate, Verification Code Color, Font, Verification Code
        imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha);
    }

Called in the Generate Authentication Code method and passed in the Authentication Code when instantiated

    public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
//Authentication code class regardless of the generation of the Authentication code string, validation
$gcudCaptcha=new \gcud\gcudCaptcha('123456');
$gcudCaptcha->Generate();

Run it again to see the orange 123456. Looking closely, you can also see the colorful distortion. If you don't want to see this, you can change the imagejpeg to the imagepng function. You can also change the header type, but in fact, it can be resolved as well as the suffix name browser that downloads the pictures.

This is actually a verification code, but remember the original purpose of the verification code, it must be more difficult to prevent it from being cracked quickly [which is also harmony]

Take a look at all the current code

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;
    private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37];
    private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

    private function Output()
    {
        header('Content-Type:image/png');
        imagepng($this->Image);
        imagedestroy($this->Image);
    }
    private function DrawCaptcha(){
        imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha);
    }

    public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
}

So the first method is to rotate each character around randomly and change the drawing method as follows

    private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,20,mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
            $X+=20;
        }
    }

Run it again and you'll see the verification codes going east and west, but that's not enough. You need to use a few other parameters to try random font sizes, different fonts, different colors, using Chinese as verification codes, and so on.

Write another common line drawing, add an attribute $LineRGB=[37,32,98] and initialize it in the constructor, then put the line below the drawing verification code there

  private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
//Parameter Picture, Start x, Start y, End x, End y, Color
            imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB);
            $X+=20;
        }
    }

No more, interested in your own research, and finally complete code

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;
    private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37],$LineRGB=[37,32,98];
    private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
        $this->LineRGB=imagecolorallocate($this->Image,$this->LineRGB[0],$this->LineRGB[1],$this->LineRGB[2]);
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

    private function Output()
    {
        header('Content-Type:image/png');
        imagepng($this->Image);
        imagedestroy($this->Image);
    }
    private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
            imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB);
            $X+=20;
        }
    }

    public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
}

Tags: Programming PHP Windows network Attribute

Posted on Thu, 07 Nov 2019 07:52:42 -0500 by Worqy