12306 Verification Code Support Chinese alphanumeric, custom font PHP CAPTCHA code

Source: Internet
Author: User
Tags chr
Copy CodeThe code is as follows:


/*
* Captcha Class base on PHP GD Lib
* @author Design
* @version 1.0
* @demo
* Include (' captchaclass.php ');
* $captchaDemo =new Captcha ();
* $captchaDemo->createimage ();
*/
Class captcha{
@ Define CAPTCHA Picture height
Private $height;
@ Define CAPTCHA Picture width
Private $width;
@ Defines the number of Captcha characters
Private $textNum;
@ Define CAPTCHA character content
Private $textContent;
@ Define character Color
Private $fontColor;
@ Defines the randomly-out text color
Private $randFontColor;
@ Define Font Size
Private $fontSize;
@ Define Font
Private $fontFamily;
@ Define Background color
Private $bgColor;
@ Define a randomly-out background color
Private $randBgColor;
@ Define Character language
Private $textLang;
@ Define number of interference points
Private $noisePoint;
@ Define number of interference lines
Private $noiseLine;
@ defines whether to distort
Private $distortion;
@ Define distort picture source
Private $distortionImage;
@ Defines if there is a border
Private $showBorder;
@ Define CAPTCHA Picture Source
Private $image;
@Constructor Constructors
Public Function Captcha () {
$this->textnum=4;
$this->f
$this->f//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 the number of characters
Public Function Settextnumber ($textN) {
$this->textnum= $textN;
}
@ Set Character color
Public Function Setfontcolor ($FC) {
$this->f
}
@ Set Font size
Public Function Setfontsize ($n) {
$this->f
}
@ Set Font
Public Function setfontfamily ($FFURL) {
$this->f
}
@ 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 number of interference points
Public Function Setnoisepoint ($n) {
$this->noisepoint= $n;
}
@ Set number of interference lines
Public Function Setnoiseline ($n) {
$this->noiseline= $n;
}
@ setting is distorted
Public Function Setdistortion ($b) {
$this->distortion= $b;
}
@ Set whether borders are displayed
Public Function Setshowborder ($border) {
$this->showborder= $border;
}
@ Initialize Verification code 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);
}
@ Generate 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 Encoding to UTF8
Break
}
Return substr ($string, 1);
}
@ Output text to verification code
Public Function CreateText () {
$textArray =explode (', ', $this->randtext ($this->textlang));
$this-&GT;TEXTC
if (Empty ($this->fontcolor)) {
$this-&GT;RANDF
}else{
$this-&GT;RANDF
}
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) ;
}
}
@ Warp Text
Public Function Distortiontext () {
$this->distorti
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), $y, $rgbColor);
}
}
$this->image= $this->distortionimage;
}
@ Generate Verification Code picture
Public Function CreateImage () {
$this->initimage (); Create a basic picture
$this->createtext (); Output Verification Code characters
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 a border
Imagepng ($this->image);
Imagedestroy ($this->image);
if ($this->distortion) {Imagedestroy ($this, $distortionImage);}
return $this->textcontent;
}
}
?> How to use:
Session_Start ();
Header ("Content-type:image/png");
Include (' captcha5_class.php ');
$captcha 5=new Captcha ();
@ Set Verification Code width
$captcha 5->setwidth (200);
@ Set Verification Code height
$captcha 5->setheight (50);
@ Set the 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 number of interference points
$captcha 5->setnoisepoint (600);
@ Set number of interference lines
$captcha 5->setnoiseline (10);
@ setting is distorted
$captcha 5->setdistortion (TRUE);
@ Set whether borders are displayed
$captcha 5->setshowborder (TRUE);
Output Verification Code
$code = $captcha 5->createimage ();
$_session[' Captchacode ' [' content ']= $code;
$_session[' Captchacode ' [' Time ']=microtime ();
?>

The above describes the 12306 verification code support Chinese alphanumeric, custom font PHP code, including the 12306 verification code aspects, I hope to be interested in PHP tutorial friends helpful.

  • 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.