PHP Verification code class based on GD library and supports Chinese and English fonts, backgrounds, interference points, distortions, etc.

Source: Internet
Author: User
PHP Verification code class based on GD library (support Chinese and English font, background, interference dot line, twist ...) )

<?php/** Captcha Class Base on php GD lib* @author design* @version 1.0* @copyright js8.in 2010* @demo * include (' Captch Aclass.php '); * $captchaDemo =new Captcha (); * $captchaDemo->createimage (); */class captcha{//@ define CAPTCHA picture height private $ height;//@ definition captcha picture width private $width;//@ defines the number of captcha characters private $textNum;//@ defines the content of the captcha character as private $textContent;//@ defines the character color private $ fontcolor;//@ defines the randomly-out text color private $randFontColor;//@ defines the font size private $fontSize;//@ defines the font private $fontFamily;//@ Defines the background color private $bgColor;//@ defines the random background color of the private $randBgColor;//@ defines the character language private $textLang;//@ defines the number of interference points private $noisePoint //@ defines the number of interference lines private $noiseLine;//@ defines whether to distort private $distortion;//@ definition distorts the image source private $distortionImage;//@ defines if there is a border private $ showborder;//@ definition captcha picture source private $image;//@Constructor constructor Public Function Captcha () {$this->textnum = 4; $this FontSize = $this->fontfamily = ' C:\windows\fontsSIMYOU.ttf '; Set the Chinese font, can be changed to the directory of Linux $this->textlang = ' en '; $this->noisepoint =; $this->noiseline = 3; $this distortion = false; $this-&GT;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->fontcolor = sscanf ($FC, ' #%2x%2x%2x ');} @ Set the 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 number of interference points public function Setnoisepoint ($n) {$this->noisepoint = $n;} @ Set number of interference lines public function Setnoiseline ($n) {$this->noiseline = $n;} @ Sets whether to distort public function setdistortion ($b) {$this->distortion = $b;} @ set whether to show border public function Setshowborder ($border) {$this->showborder = $border;} @ Initialize Authenticode 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 (255), Mt_rand (255), Mt_rand (100, 255));} else {$this->randbgcolor = imagecolorallocate ($this->image, $this->bgcolor[0], $this->bgcolor[1], $this- &GT;BGCOLOR[2]);} Imagefill ($this->image, 0, 0, $this->randbgcolor);} @ Generate random character 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 Utf8break;} 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,), Mt_ Rand (0, +), Mt_rand (0, 100));} else {$this->randfontcolor = imagecolorallocate ($this->image, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);} for ($i = 0; $i < $this->textnum; $i + +) {$angle = Mt_rand ( -1, 1) * Mt_rand (1); Imagettftext ($this->image, $thi  S->fontsize, $angle, 5 + $i * FLOOR ($this->fontsize * 1.3), Floor ($this->height * 0.75), $this->randfontcolor, $this->fontfamily, $textArray [$i]);}} @ Generate noise point public function Createnoisepoint () {for ($i = 0; $i < $this->noisepoint; $i + +) {$pointColor = Imagecoloralloc Ate ($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-&G T;image, Mt_rand (0, 255), Mt_rand (0, 255), Imageline ($this->image, 0, Mt_rand (0, $this->width), $this- width, mt_rand (0, $this->height), $lineColor);}} @ Distorted 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-&gt ;d istortionimage, (int) ($x + sin ($y/$this->height * 2 * M_PI-M_PI * 0.5) * 3), $y, $rgbColor);}} $this->image = $this->distortionimage;} @ Generate Captcha Picture public Function CreateImage () {$this->initimage ();//Create basic Picture $this->createtext ();//Output CAPTCHA character if ($this- >distortion) {$this->distortiontext ();}//Distorted text $this->createnoisepoint (); Generate interference Point $this->createnoiseline (); Generate interference line 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;}}? >

How to use:

<?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 captcha height//$captcha 5->setheight (50),//@ set number of characters $captcha5-> Settextnumber (5);//@ set character color//$captcha 5->setfontcolor (' #ff9900 ');//@ set font size//$captcha 5->setfontsize (25);//@ Set the font $captcha5->setfontfamily (' C:\windows\fonts\STXINGKA. TTF ');//@ set Language $captcha5->settextlang (' cn ');//@ set background color//$captcha 5->setbgcolor (' #000000 ');//@ set number of interference points//$ Captcha5->setnoisepoint (600);//@ set the number of interference lines//$captcha 5->setnoiseline (10);//@ setting is distorted//$captcha 5-> Setdistortion (TRUE);//@ sets whether to display border $captcha5->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.