Support Chinese alphanumeric, custom font PHP verification Code Program

Source: Internet
Author: User
Tags chr rand set background
The code is as follows Copy Code

<?php
/*
* Captcha Class base on PHP GD Lib
* @author Design
* @version 1.0
* @copyright js8.in 2010
* @demo
* Include (' captchaclass.php ');
* $captchaDemo =new Captcha ();
* $captchaDemo->createimage ();
*/
Class captcha{
@ Define Verification Code picture height
Private $height;
@ Defines the width of the verification code picture
Private $width;
@ Defines the number of Authenticode characters
Private $textNum;
@ Define Authenticode character content
Private $textContent;
@ Define character Color
Private $fontColor;
@ defines the random text color
Private $randFontColor;
@ Define Font Size
Private $fontSize;
@ Define Font
Private $fontFamily;
@ Define Background color
Private $bgColor;
@ defines a random background color
Private $randBgColor;
@ Define Character language
Private $textLang;
@ Define number of jamming points
Private $noisePoint;
@ Define number of interference lines
Private $noiseLine;
@ definition is distorted
Private $distortion;
@ Define distorted picture source
Private $distortionImage;
@ Defines whether there is a border
Private $showBorder;
@ Define Authenticode Picture source
Private $image;

@Constructor Constructors
Public Function Captcha () {
$this->textnum=4;
$this->fontsize=16;
$this->fontfamily= ' C:\windows\fontsSIMYOU.ttf ';//set Chinese font, can be changed to Linux directory
$this->textlang= ' en ';
$this->noisepoint=30;
$this->noiseline=3;
$this->distortion=false;
$this->showborder=false;
}



@ Set Picture width
Public Function SetWidth ($w) {
$this->width= $w;
}

@ Set Picture height
Public Function SetHeight ($h) {
$this->height= $h;
}

@ Set Number of characters
Public Function Settextnumber ($textN) {
$this->textnum= $textN;
}

@ Set Character color
Public Function Setfontcolor ($FC) {
$this->fontcolor=sscanf ($FC, ' #%2x%2x%2x ');
}

@ Set Font size
Public Function Setfontsize ($n) {
$this->fontsize= $n;
}

@ Set Font
Public Function setfontfamily ($FFURL) {
$this->fontfamily= $ffUrl;
}

@ Set Character language
Public Function Settextlang ($lang) {
$this->textlang= $lang;
}

@ Set Picture background
Public Function Setbgcolor ($BC) {
$this->bgcolor=sscanf ($BC, ' #%2x%2x%2x ');
}

@ Set the number of jamming points
Public Function Setnoisepoint ($n) {
$this->noisepoint= $n;
}

@ Set the number of interference lines
Public Function Setnoiseline ($n) {
$this->noiseline= $n;
}

@ setting is distorted
Public Function Setdistortion ($b) {
$this->distortion= $b;
}

@ Sets whether borders are displayed
Public Function Setshowborder ($border) {
$this->showborder= $border;
}

@ Initialization Captcha Picture
Public Function Initimage () {
if (Empty ($this->width)) {$this->width=floor ($this->fontsize*1.3) * $this->textnum+10;}
if (Empty ($this->height)) {$this->height= $this->fontsize*2;}
$this->image=imagecreatetruecolor ($this->width, $this->height);
if (Empty ($this->bgcolor)) {
$this->randbgcolor=imagecolorallocate ($this->image,mt_rand (100,255), Mt_rand (100,255), Mt_rand (100,255));
}else{
$this->randbgcolor=imagecolorallocate ($this->image, $this->bgcolor[0], $this->bgcolor[1], $this-> BGCOLOR[2]);
}
Imagefill ($this->image,0,0, $this->randbgcolor);
}

@ produces random characters
Public Function Randtext ($type) {
$string = ';
Switch ($type) {
Case ' en ':
$str = ' ABCDEFGHJKLMNPQRSTUVWXY3456789 ';
For ($i =0 $i < $this->textnum; $i + +) {
$string = $string. ', '. $str [Mt_rand (0,29)];
}
Break
Case ' CN ':
For ($i =0 $i < $this->textnum; $i + +) {
$string = $string. ', ' Chr (rand (0XB0,0XCC)). Chr (rand (0XA1,0XBB));
}
$string =iconv (' GB2312 ', ' UTF-8 ', $string); Convert code to UTF8
Break
}
Return substr ($string, 1);
}

@ Output text to verification code
Public Function CreateText () {
$textArray =explode (', ', $this->randtext ($this->textlang));
$this->textcontent=join (", $textArray);
if (Empty ($this->fontcolor)) {
$this->randfontcolor=imagecolorallocate ($this->image,mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));
}else{
$this->randfontcolor=imagecolorallocate ($this->image, $this->fontcolor[0], $this->fontcolor[1],$ THIS-&GT;FONTCOLOR[2]);
}
For ($i =0 $i < $this->textnum; $i + +) {
$angle =mt_rand ( -1,1) *mt_rand (1,20);
Imagettftext ($this->image, $this->fontsize, $angle, 5+ $i *floor ($this->fontsize*1.3), Floor ($this-> height*0.75), $this->randfontcolor, $this->fontfamily, $textArray [$i]);
}
}

@ Generate Interference points
Public Function Createnoisepoint () {
For ($i =0 $i < $this->noisepoint; $i + +) {
$pointColor =imagecolorallocate ($this->image,mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Imagesetpixel ($this->image,mt_rand (0, $this->width), Mt_rand (0, $this->height), $pointColor);
}

}

@ Generate interference Line
Public Function Createnoiseline () {
For ($i =0 $i < $this->noiseline; $i + +) {
$lineColor =imagecolorallocate ($this->image,mt_rand (0,255), Mt_rand (0,255), 20);
Imageline ($this->image,0,mt_rand (0, $this->width), $this->width,mt_rand (0, $this->height), $lineColor) ;
}
}

@ Distort Text
Public Function Distortiontext () {
$this->distortionimage=imagecreatetruecolor ($this->width, $this->height);
Imagefill ($this->distortionimage,0,0, $this->randbgcolor);
For ($x =0 $x < $this->width; $x + +) {
For ($y =0 $y < $this->height; $y + +) {
$rgbColor =imagecolorat ($this->image, $x, $y);
Imagesetpixel ($this->distortionimage, (int) ($x +sin ($y/$this->height*2*m_pi-m_pi*0.5) *3), $y, $rgbColor);
}
}
$this->image= $this->distortionimage;
}

@ Generate Verification Code picture
Public Function CreateImage () {
$this->initimage (); Create a basic picture
$this->createtext (); Output Verification Code character
if ($this->distortion) {$this->distortiontext ();}//Warp text
$this->createnoisepoint (); Generate Interference points
$this->createnoiseline (); Generate interference lines
if ($this->showborder) {imagerectangle ($this->image,0,0, $this->width-1, $this->height-1, $this-> Randfontcolor);} Add border
Imagepng ($this->image);
Imagedestroy ($this->image);
if ($this->distortion) {Imagedestroy ($this-> $distortionImage);}
return $this->textcontent;
}

}
?> Use Method:

<?php
Session_Start ();
Header ("Content-type:image/png");
Include (' captcha5_class.php ');
$captcha 5=new Captcha ();

@ Set Verification Code width
$captcha 5->setwidth (200);

@ Set Authentication Code height
$captcha 5->setheight (50);

@ Set Number of characters
$captcha 5->settextnumber (5);

@ Set Character color
$captcha 5->setfontcolor (' #ff9900 ');

@ Set Font size
$captcha 5->setfontsize (25);

@ Set Font
$captcha 5->setfontfamily (' C:\windows\fonts\STXINGKA. TTF ');

@ Set Language
$captcha 5->settextlang (' cn ');

@ Set Background color
$captcha 5->setbgcolor (' #000000 ');

@ Set the number of jamming points
$captcha 5->setnoisepoint (600);

@ Set the number of interference lines
$captcha 5->setnoiseline (10);

@ setting is distorted
$captcha 5->setdistortion (TRUE);

@ Sets whether borders are displayed
$captcha 5->setshowborder (TRUE);

Output Verification Code
$code = $captcha 5->createimage ();
$_session[' Captchacode ' [' content ']= $code;
$_session[' Captchacode ' [' Time ']]=microtime ();
?>

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.