PHP generates a verification code with interference lines, interference points, character skew

Source: Internet
Author: User

PHP generates verification code class code, this verification code class supports the generation of interference points, interference lines and other interfering pixels, also can make the character tilt. In a class you can define parameters such as the width, height, length, tilt angle of the captcha, and then use the following:

<?PHPclassclass_authcode{ Public  $authcode= ";//Verification Code    Private $width= ";//Verification Code Picture width    Private $height= ";//Verification Code Picture High    Private $len= ";//Verification Code Length    Private $tilt=Array( -30,30);//Verification Code Tilt Angle    Private $font= ' Altehaasgroteskbold.ttf ';//Font Files    Private $str= ";//Verification code Base    Private $im= ";//Create a handle//constructor for a picture    function__construct ($width=100,$heigh=30,$len=4) {        $this->width =$width; $this->height =$heigh; $this->len =$len; $this->str = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; $str _len=strlen($this-&GT;STR)-1;  for($i= 0;$i<$len;$i++) {            $this->authcode. =$this->str[Rand(0,$str _len)]; }    }    //generate a Captcha picture    Private functionimagecreate () {$this->im = Imagecreatetruecolor ($this->width,$this-height); }    //Interference Color    Private functionExt_color () {returnImagecolorallocate ($this->im,Rand(50, 180),Rand(50, 180),Rand(50, 180)); }    //Generating Interference points    Private functionExt_point () { for($i= 0;$i<$this->width*2;$i++) {Imagesetpixel ($this->im,Rand(1,$this-&GT;WIDTH-1),Rand(1,$this-&GT;HEIGHT-1),$this-Ext_color ()); }    }    //Generate interference Lines    Private functionExt_line () { for($i= 0;$i<$this->len;$i++) {            $x 1=Rand(1,$this->width-1); $y 1=Rand(1,$this->height-1); $x 2=Rand(1,$this->width-1); $y 2=Rand(1,$this->height-1); Imageline ($this->im,$x 1,$y 1,$x 2,$y 2,$this-Ext_color ()); }    }    //write the verification code to the picture (cannot be used in conjunction with $this->imgstrfloat ())    Private functionImgstr () {$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); }    }    //tilt the verification code to the picture (note that this cannot be used in conjunction with the $THIS->IMGSTR () method)    Private functionimgstrfloat () {$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 Verification Code picture    functionoutput () {$this-imagecreate (); $this-Imgstr (); //$this->imgstrfloat ();        $this-Ext_point (); $this-Ext_line (); Header(' Content-type:image/png '); Imagepng ($this-im); Imagedestroy ($this-im); }}?>

Description of the Code usage:

$obj new Class_authcode (); // instantiate the object and set the width, height, and length of the verification code for the CAPTCHA image $obj-$authcode// Get Verification code $obj// output captcha picture

PHP generates a verification code with interference lines, interference points, character skew

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.