PHP Graphics Verification Code class

Source: Internet
Author: User
Tags border color explode imagejpeg rand

PHP Graphics Verification Code class


Copy code font file see attachment!

<?php

Session_Start ();

$vi = new Vcodeimage;

$vi->setimage (1,4,65,20,80,1);

Class vcodeimage{

/*******************************************************

**filename:verify_image.php

**copyright:none! (Keep this information, though)

**author:vsfree.com

**date:2007-08-08

********************************************************/

var $mode; 1: Digital mode, 2: Alphabetic mode, 3: Alphanumeric mode, others: Digital letter Optimization Mode

var $v _num; Number of verification codes

var $img _w; Verify Code image width

var $img _h; Authentication Code Image Height

var $int _pixel_num; Number of disturbing pixels

var $int _line_num; Number of interference lines

var $font _dir; Font file relative path

var $border; Image border

var $borderColor; Image border color

function SetImage ($made, $v _num, $img _w, $img _h, $int _pixel_num, $int _line_num, $font _dir= '. /font ', $border =true, $borderColor = ' 255,200,85 ') {

if (!isset ($_session[' Vcode ')) {

Session_register (' Vcode ');

}

$_session[' Vcode ']= ' ";

$this->mode = $made;

$this->v_num = $v _num;

$this->img_w = $img _w;

$this->img_h = $img _h;

$this->int_pixel_num = $int _pixel_num;

$this->int_line_num = $int _line_num;

$this->font_dir = $font _dir;

$this->border = $border;

$this->bordercolor = $borderColor;

$this->generateimage ();

}

function GetChar ($mode) {

if ($mode = = "1") {

$ychar = "0,1,2,3,4,5,6,7,8,9";

}

else if ($mode = = "2") {

$ychar = "A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";

}

else if ($mode = = "3") {

$ychar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";

}

Else

$ychar = "3,4,5,6,7,8,9,a,b,c,d,h,k,p,r,s,t,w,x,y";

return $ychar;

}

function Randcolor ($rs, $re, $gs, $ge, $bs, $be) {

$r = Mt_rand ($rs, $re);

$g = Mt_rand ($gs, $ge);

$b = Mt_rand ($bs, $be);

Return Array ($r, $g, $b);

}

function Generateimage () {

$im = Imagecreate ($this->img_w, $this->img_h);

$black = Imagecolorallocate ($im, 0,0,0);

$white = Imagecolorallocate ($im, 255,255,255);

$bgcolor = Imagecolorallocate ($im, 250,250,250);

Imagefill ($im, 0,0, $bgcolor);

$fonts = Scandir ($this->font_dir);

$fmax = count ($fonts)-2;

$ychar = $this->getchar ($this->mode);

$list = Explode (",", $ychar);

$x = Mt_rand (2, $this->img_w/($this->v_num+2));

$cmax = count ($list)-1;

$v _code = ';

For ($i =0 $i < $this->v_num; $i + +)//Verification Code

{

$randnum = Mt_rand (0, $cmax);

$this _char = $list [$randnum];

$v _code. = $this _char;

$size = Mt_rand (intval ($this->img_w/5), Intval ($this->IMG_W/4));

$angle = Mt_rand ( -20,20);

$y = Mt_rand (($size +2), ($this->img_h-2));

if ($this->border)

$y = Mt_rand (($size +3), ($this->img_h-3));

$rand _color = $this->randcolor (0,200,0,100,0,250);

$randcolor = Imagecolorallocate ($im, $rand _color[0], $rand _color[1], $rand _color[2]);

$fontrand = Mt_rand (2, $fmax);

$font = "$this->font_dir/". $fonts [$fontrand];

Imagettftext ($im, $size, $angle, $x, $y, $randcolor, $font, $this _char);

$x = $x + intval ($this->img_w/($this->v_num+1));

}

For ($i =0 $i < $this->int_pixel_num; $i + +) {//interference pixels

$rand _color = $this->randcolor (50,250,0,250,50,250);

$rand _color_pixel = imagecolorallocate ($im, $rand _color[0], $rand _color[1], $rand _color[2]);

Imagesetpixel ($im, Mt_rand ()% $this->img_w, Mt_rand ()% $this->img_h, $rand _color_pixel);

}

For ($i =0 $i < $this->int_line_num; $i + +) {//Interference line

$rand _color = $this->randcolor (0,250,0,250,0,250);

$rand _color_line = imagecolorallocate ($im, $rand _color[0], $rand _color[1], $rand _color[2]);

Imageline ($im, Mt_rand (0,intval ($this->img_w/3)), Mt_rand (0, $this->img_h), Mt_rand (Intval ($this->img_w-( $this->IMG_W/3)), $this->img_w), Mt_rand (0, $this->img_h), $rand _color_line);

}

if ($this->border)//Draw a border

{

if (Preg_match ("/^d{1,3},d{1,3},d{1,3}$/", $this->bordercolor)) {

$borderColor = Explode (', ', $this->bordercolor);

}

$border _color_line = imagecolorallocate ($im, $borderColor [0], $borderColor [1], $borderColor [2]);

Imageline ($im, 0, 0, $this->img_w, 0, $border _color_line); Upper Horizontal

Imageline ($im, 0, 0, 0, $this->img_h, $border _color_line); Left vertical

Imageline ($im, 0, $this->img_h-1, $this->img_w, $this->img_h-1, $border _color_line); Lower Horizontal

Imageline ($im, $this->img_w-1, 0, $this->img_w-1, $this->img_h, $border _color_line); Right Vertical

}

Imageantialias ($im, true); Anti-aliasing

$time = time ();

$_session[' vcode ' = $v _code. "|". $time; Negative validation code and generation time to $_session[vcode]

Generate images to browsers

if (function_exists ("Imagegif")) {

Header ("Content-type:image/gif");

Imagegif ($im);

}

ElseIf (function_exists ("Imagepng")) {

Header ("Content-type:image/png");

Imagepng ($im);

}

ElseIf (function_exists ("imagejpeg")) {

Header ("Content-type:image/jpeg");

Imagejpeg ($im, "" ", 80);

}

ElseIf (function_exists ("Imagewbmp")) {

Header ("Content-type:image/vnd.wap.wbmp");

Imagewbmp ($im);

}

Else

Die ("No Image Support on this Server!");

Imagedestroy ($im);

}

}

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.