PHP captcha code sharing

Source: Internet
Author: User

<?php
Session_Start (); Open session at the very beginning of the file
Put both English and Chinese into two arrays
$english _arr=array (' Q ', ' W ', ' e ', ' r ', ' t ', ' y ', ' u ', ' i ', ' o ', ' P ', ' a ', ' s ', ' d ', ' f ', ' g ', ' H ', ' J ',
' K ', ' l ', ' Z ', ' x ', ' C ', ' V ', ' b ', ' n ', ' m ');
$chinese _arr=array (' One ', ' two ', ' three ', ' four ', ' five ', ' VI ', ' VII ', ' VIII ', ' IX ', ' Ten ', ' 0 ', ' wood ', ' and ', ' I ',
' People ', ' there ', ' ', ' right ', ' Lord ', ' production ', ' for ', ' this ', ' work ', ' on ', ' Down ', ' left ';
/**
* Access to Chinese and English
*/

/**
* Get a random number, random letter, Random Chinese
*/
for ($i =0; $i <5; $i + +)
{
$index =rand (0,2);
if ($index = = ' 0 ')
{
$num. =rand (0,9);
echo "<font color=red> $num </font><br>";
}
else if ($index = = ' 1 ')
{
$num. = $english _arr[rand (0,25)];
echo "<font color=blue> $num </font><br>";
}
else if ($index = = ' 2 ')
{
Echo $num;
$num. = $chinese _arr[rand (0,25)]; If you are adding Chinese, you want to convert the encoding
echo "<font color=green> $num </font><br>";
}

}

$_session[check_num]= $num; Write this verification code into session.
Echo $_session[check_num];
Imagecreatetruecolor (wide, high)
$image =imagecreatetruecolor (150,50); Create a picture of a 100,30

Imagecolorallocate (image name, red, blue, green) three primary colors
Imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));//The first time is to set the picture background color, three primary colors randomly

$color =imagecolorallocate ($image, 255,255,255);//later in order to create a color

Imagestring (image name, font type 1-6 six kinds, x coordinate, y coordinate, content, color)
Imagestring ($image, Rand (3,6), 10,3, $num, $color); Write the contents to the picture cannot output Chinese

Imagettftext (image name, font size, offset angle, x-coordinate, y-coordinate, color, font file address, input) this content must be encoded at the time of input
Imagettftext ($image, Rand (15,18), 0,rand (2,50), Rand (30,39), $color, ' Simkai.ttf ', iconv (' GBK ', ' UTF-8 ', $num)); Write the contents in the picture, can have Chinese



/**
* Draw lines and dots to do interference
*/
for ($i =0; $i <10; $i + +)
{
$linecolor =imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
Imageline ($image, Rand (0,150), Rand (0,50), Rand (0,150), Rand (0,50), $linecolor);

}
for ($i =0; $i <200; $i + +)
{
$pixelcolor =imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($image, Rand (0,150), Rand (0,50), $pixelcolor);

}

/**
* Output Image
*/
Echo Dechex (rand (0,15));

Header ("Contenttype:image/jpeg"); Set this file to what the situation is output here is as a picture

Imagejpeg ($image); Print images in JPEG format



?>

PHP captcha code sharing

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.