php.20-Verification Code Generation

Source: Internet
Author: User

Generate Verification Code

idea : First define the CAPTCHA function GetCode ()

Draw a verification code
$num = 4;//character length
GetCode ($num, 2);

1. Create canvas, assign color Imagecreatetruecolor ()
$height
$width = $num *20; Assume that each word has a size of 18
$im = Imagecreatetruecolor ($width, $height);//Create a true Color canvas
$BG = Imagecolorallocate ($im, Rand (200,250), Rand (250,255), rand (150, 255));//define Background color image
$color [] = Imagecolorallocate ($im, 240,240,240);//define Font Color "can be stored as an array, define some dark font"
    

2. Start Painting (everything is done on the canvas $im.)
Imagefill ($im, 0,0, $BG); Area fill (fills the background to the canvas)
Imagerectangle ($im, 0,0, $width-1, $height-1, $color [rand (0,3)]);//Define a border

Draw Verification Code: verbatim output
for ()
Imagettftext ($im, Rand (16,18), Rand ( -40,40), 8+ (18* $i), $color [Rand (0,3)], "MSYH.TTC", $str [$i]);
Random addition of interference points (points custom)
for () {
$c = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//Interference point color
Imagesetpixel ($im, rand (0, $width), rand (0, $height), $c);
Random addition of interference lines (number of lines self-defined)
for () {
$c = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//Interference Line Color
Imageline ($im, rand (0, $width), rand (0, $height), rand (0, $width), rand (0, $height), $c);

3. Output image
Header ("Content-type:image/png");//Set the response header (cannot have output before
Imagepng ($im);


4. Destroying pictures
Imagedestroy ($im);

Custom function, get verification code
function GetCode ($m =4, $type =1)
{
$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$t = Array (9, strlen ($STR)-1);//Type Division
$c = "";
for ($i =0; $i < $m; $i + +)
$c. = $str [rand (0, $t [$type])];

Call verification code, onclick can achieve click image Refresh

php.20-Verification Code Generation

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.