Also calculates the function to compare the entire PHP authentication code class

Source: Internet
Author: User
Tags chr count header rand

See PHP100 Video 34 written PHP verification code class, support custom picture size, number of characters, character type, character size, picture type, Chinese character verification code, interference line, etc., note details

Font file is Microsoft Jas, and Authenticode file with directory, filename Msyh.ttf, can be found from the C:\windows\fonts directory, copied to the same directory can be

<?php

/**

* @version Id:imgcode.php 2012-11-30 k

* @package imgcode.php

* @Purview Default

* @link http://www.3oom.com

*/

$img = new Img_code ();

$img->show ();

Class img_code{

Private $img _type = ' png ';//output Picture type Png,gif,jpg

Private $line = true;//whether to increase the interference line

Private $text = ' 2 ';//Authentication code type: 0 digits, 1 letters, 2 Chinese characters

Private $text _size= 20;//font size

Private $length = 4;//string length

Private $width = 120;//picture width, pixel

Private $height = 40;//picture height, pixel

Private $font _file= ' msyh.ttf ';//font file

Public $img = ';

/*

* Create picture

*/

Private Function Img_create () {

$this->img = imagecreate ($this->width, $this->height);

Imagecolorallocate ($this->img, 255,255,255);

}

/*

* Show Pictures

*/

Public function Show () {

@session_start ();

$this->img_create ();

if ($this->line) {

$this->img_line ();

}

$this->img_text ();

$this->img_header ();

Imagedestroy ($im);

Exit ();

}

/*

* Generate string

*/

Private Function Img_text () {

$rand _string = ';

$im = & $this->img;

$fontColor [] = Imagecolorallocate ($im, 0x15, 0x15, 0x15);

$fontColor [] = Imagecolorallocate ($im, 0x95, 0x1e, 0x04);

$fontColor [] = Imagecolorallocate ($im, 0x93, 0x14, 0xa9);

$fontColor [] = Imagecolorallocate ($im, 0x12, 0x81, 0x0a);

$fontColor [] = Imagecolorallocate ($im, 0x06, 0x3a, 0xd5);

if ($this->text<2) {

Numbers, letters

For ($i =0 $i < $this->length; $i + +) {

if ($this->text) {

$c =CHR (Mt_rand (65,90));

}else{

$c =CHR (Mt_rand (48,57));

}

if ($c = = ' I ') $c = ' K ';

if ($c = = ' O ') $c = ' E ';

$rand _string[]= $c;

}

}else{

Chinese characters, we can introduce Chinese character files here

$arr =array (' big ', ' small ', ' more ', ' less ', ' man ', ' Day ', ' water ', ' earth ', ' wood ', ' Fire ', ' cloud ');

For ($i =0 $i < $this->length; $i + +) {

$l = count ($arr)-1;

File encoding for GBK requires conversion

$rand _string[] = iconv (' gb2312 ', ' utf-8 ', $arr [Mt_rand (0, $l)]);

$rand _string[] = $arr [Mt_rand (0, $l)];

}

}

$_session[' Imgcode ']= $rand _string;

$_c = count ($rand _string);

For ($i =0 $i <$_c; $i + +)

{

if ($this->text = = 1) {

$rand _string[$i] = strtoupper ($rand _string[$i]);

}

$c _fontcolor = $fontColor [Mt_rand (0,4)];

$y = $this->height-($this->height-$this->text_size)/2;

$x = ($this->width-($this->text_size+2) * $this->length)/2;

$y _pos = $i ==0? $x: $i * ($this->text_size+2) + $x;

$c = Mt_rand (0, 15);

@imagettftext ($im, $this->text_size, $c, $y _pos, $y, $c _fontcolor, $this->font_file, $rand _string[$i]);

}

}

/*

* Generate lines

*/

Private Function Img_line () {

$im = & $this->img;

$img _width = $this->width;

$img _height= $this->height;

Background horizontal

$lineColor 1 = imagecolorallocate ($im, 0xDA, 0XD9, 0xd1);

for ($j =3; $j <= $img _height-3; $j = $j +3)

{

Imageline ($im, 2, $j, $img _width-2, $j, $lineColor 1);

}

Background vertical

$lineColor 2 = imagecolorallocate ($im, 0xda,0xd9,0xd1);

for ($j =2; $j < $img _width-6; $j = $j +6)

{

Imageline ($im, $j, 0, $j +8, $img _height, $lineColor 2);

}

Draw a border

if ($use _boder && $filter _type = 0)

{

$bordercolor = Imagecolorallocate ($im, 0x9d, 0x9e, 0x96);

Imagerectangle ($im, 0, 0, $img _width-1, $img _height-1, $bordercolor);

}

}

/*

* Generate output

*/

Private Function Img_header () {

Header ("pragma:no-cache\r\n");

Header ("cache-control:no-cache\r\n");

Header ("expires:0\r\n");

if ($this->img_type = = ' jpg ') {

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

Imagejpeg ($this->img);

}else if ($this->img_type = = ' png ') {

Header (' content-type:image/png ');

Imagepng ($this->img);

}else{

Header (' content-type:image/gif ');

Imagegif ($this->img);

}

}

}

Article Source: http://www.3oom.com/blog/50.html



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.