Three PHP Chinese verification code generation and invocation Method _php Tutorial

Source: Internet
Author: User
Tags set background utf 8
Three PHP Chinese code generation and invocation method in PHP to generate a Chinese verification code must be done with the generated verification code is not the same operation, because the GD function only accepts UTF8 format encoded text, so in PHP to generate Chinese verification code before the face to the first encoding conversion, PHP Iconv can be manipulated as an example.

Three PHP tutorial Chinese verification Code generation and invocation method
In PHP to generate a Chinese verification code must be done with the generation of verification code is not the same operation, because the GD function only accepts UTF8 format encoded text, so in PHP to generate Chinese verification code before the face to the first to encode the conversion, the operation of PHP iconv can be an example.
*/

$ch _str= "You want to generate Chinese character verification Code";
$str =array ();
for ($i =0; $i <>
{
$str []= $ch _str[$i]. $ch _str[$i +1]. $ch _str[$i +2];
}
The length and height of the picture
$image _x=200;
$image _y=100;
$im = Imagecreate ($image _x, $image _y);
Here to take the image of the white background
$bkg = Imagecolorallocate ($im, 255,255,255);
Display the font style, this will put the file in the corresponding directory, if you do not have files, go to the window font file to find a bar.
$FNT = "Simfang.ttf";
Assign some colors to an image
$white =imagecolorallocate ($im, 234,185,95);
To draw an elliptical arc on a picture, specify the next sitting punctuation
Imagearc ($im, 8, A, A, $white);
Imagearc ($im, 7,50, 175, $white);
Draw a line segment on the picture to specify the next sitting punctuation
Imageline ($im, 20,20,180,30, $white);
Imageline ($im, 20,18,170,50, $white);
Imageline ($im, 25,50,80,50, $white);
The number of chaotic points
$noise _num=3000;
$line _num=80;
The colors of various chaotic characters
$rectangle _color=imagecolorallocate ($im, 0XAA,0XAA,0XAA);
$noise _color=imagecolorallocate ($im, 0x00,0x00,0x00);
$font _color=imagecolorallocate ($im, 0x00,0x00,0x00);
for ($i =0; $i < $noise _num; $i + +)
{
Draw a single pixel at a coordinate point, which is defined above and is black.
Imagesetpixel ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), $noise _color);
}

for ($i =0; $i < $line _num; $i + +)
{
$line _color=imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Draw a line between two coordinate points, the color defined above
Imageline ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), Mt_rand (0, $image _x), Mt_rand (0, $image _y), $line _color);
}
$randnum =rand (0,count ($STR)-4);
Keep an even number
if ($randnum%2)
{
$randnum +=1;
}
$str 1= $str [$randnum]. $str [$randnum +1];
for ($i =0; $i <2; $i + +)
{
Imagettftext ($im, Rand (28,32), Rand (0,70), Rand (($image _X/4) * $i + $image _X/10, ($image _x/4) * $i + $image _X/8), rand ($ image_y/2+ $image _Y/10, $image _y/2+ $image _y/5), $font _color, $fnt, $str [$randnum + $i]);
}
Imagepng ($im);
Imagedestroy ($im);

Generate Chinese Verification Code II
$str = "Chinese characters";
$image _x=110;
$image _y=110;
$im = Imagecreate ($image _x, $image _y);
$bkg = Imagecolorallocate ($im, 255,255,255);
$FNT = "Hb.ttf"; The font style that is displayed
$white =imagecolorallocate ($im, 234,185,95);
Imagearc ($im, 8, A, A, $white);
Imagearc ($im, 7,50, 175, $white);
Imageline ($im, 20,20,180,30, $white);
Imageline ($im, 20,18,170,50, $white);
Imageline ($im, 25,50,80,50, $white);
$noise _num=3000;
$line _num=80;
Imagecolorallocate ($im, 0xff,0xff,0xff);
$rectangle _color=imagecolorallocate ($im, 0XAA,0XAA,0XAA);
$noise _color=imagecolorallocate ($im, 0x00,0x00,0x00);
$font _color=imagecolorallocate ($im, 0x00,0x00,0x00);
$line _color=imagecolorallocate ($im, 0x00,0x00,0x00);
for ($i =0; $i < $noise _num; $i + +)
Imagesetpixel ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), $noise _color);
for ($i =0; $i < $line _num; $i + +)
Imageline ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), Mt_rand (0, $image _x), Mt_rand (0, $image _y), $line _color);
$randnum =rand (0,strlen ($STR)-4);
if ($randnum%2) $randnum +=1;
$str 1=substr ($str, $randnum, 4);
$str 2 = iconv ("gb2312", "Utf-8", $str 1);//verify Chinese characters in $STR1

Imagettftext ($im, Rand (28,32), Rand (0,70), Rand (25,27), Rand (70,100), $font _color, $fnt, $str 2);
Imagepng ($im);
Imagedestroy ($im);

Put the kanji in the array
/*
The GD function only accepts text encoded in UTF8 format, so the first thing to do is to encode the text before writing it. PHP's own iconv and mbstring libraries can do the job.
*/

$randcode =array (' spoil ');
$codetable =array ();
$FP =fopen ("Gb2312.txt", "R");
while ($line =fgets ($FP))
$codetable [Hexdec (substr ($line, 0,6))]=substr ($line, 7,6);
Fclose ($FP);

gb2312 Turn UTF8
function Gb2utf8 ($GBSTR)
{
Global $codetable;
if (Trim ($gbstr) = = "")
return $gbstr;
$ret = "";
$utf 8 = "";
while ($GBSTR)
{
if (Ord (substr ($GBSTR, 0,1)) >127)
{
$THISW =substr ($gbstr, 0,2);
$gbstr =substr ($gbstr, 2,strlen ($GBSTR));
$utf 8 = "";
@ $utf 8=u2utf8 (Hexdec ($codetable [Hexdec (Bin2Hex ($THISW)) -0x8080]));

if ($utf 8!= "")
for ($i =0; $i<>
$ret. =CHR (substr ($utf 8, $i, 3));
}
Else
{
$ret. =substr ($gbstr, 0, 1);
$gbstr =substr ($gbstr, 1,strlen ($GBSTR));
}
}
return $ret;
}

Unicode goto UTF8
function U2utf8 ($c)
{
$str = "";
if ($c <0x80)
$str. = $c;
ElseIf ($c <0x800)
{
$str. = (0xc0| $c >>6);
$str. = (0x80| $c &0x3f);
}
ElseIf ($c <0x10000)
{
$str. = (0xe0| $c >>12);
$str. = (0x80| $c >>6&0x3f);
$str. = (0x80| $c &0x3f);
}
ElseIf ($c <0x200000)
{
$str. = (0xf0| $c >>18);
$str. = (0x80| $c >>12&0x3f);

$str. = (0x80| $c >>6&0x3f);
$str. = (0x80| $c &0x3f);
}
return $str;
}

Generate additional Code
function Create_excode ($length)
{
Global $randcode;
Header ("Content-type:image/png");
$image _x= $length *30; Picture width
$image _y=40; Picture height
$noise _num=80* $length; Number of miscellaneous points
$line _num= $length-2; Number of interference lines
$image =imagecreate ($image _x, $image _y);
Imagecolorallocate ($image, 0xff,0xff,0xff); Set Background color
$rectangle _color=imagecolorallocate ($image, 0XAA,0XAA,0XAA); Border color
$noise _color=imagecolorallocate ($image, 0x00,0x00,0x00); Color of miscellaneous dots
$font _color=imagecolorallocate ($image, 0x00,0x00,0x00); Font Color
$line _color=imagecolorallocate ($image, 0x33,0x33,0x33); Interference Line Color

Join the miscellaneous points
for ($i =0; $i < $noise _num; $i + +)
Imagesetpixel ($image, Mt_rand (0, $image _x), Mt_rand (0, $image _y), $noise _color);

$font _face= "Simkai.ttf"; Font
$x = 2;
$session _code= ';
for ($i =0; $i < $length; $i + +)
{
$code = $randcode [Mt_rand (0,count ($randcode)-1)];
Imagettftext ($image, 18,mt_rand ( -6,6), $x, $font _color, $font _face,gb2utf8 ($code));
$x +=30;
$session _code.= $code;
}
@session_start ();
$_session[' Excode ']= $session _code; Place the value of the additional code in the session


Adding interference lines
for ($i =0; $i < $line _num; $i + +)
Imageline ($image, Mt_rand (0, $image _x), Mt_rand (0, $image _y),
Mt_rand (0, $image _x), Mt_rand (0, $image _y), $line _color);
Imagerectangle ($image, 0,0, $image _x-1, $image _y-1, $rectangle _color); Add a border
Imagepng ($image);
Imagedestroy ($image);
}
Create_excode (6);

The use of the time directly with the HTML syntax: Call on the server to do the validation when the session stored validation characters compared with the user-submitted characters, the same is verified

http://www.bkjia.com/PHPjc/633011.html www.bkjia.com true http://www.bkjia.com/PHPjc/633011.html techarticle Three PHP Chinese verification code generation and invocation method in PHP to generate a Chinese verification code must be done with the generated verification code is not the same operation, because the GD function only accepts UTF8 format encoded text ...

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