PHP Verification Code Applet

Source: Internet
Author: User
Tags imagejpeg shuffle

Verification code function (personal understanding):

    1. Reduce the pressure on the server (such as the 12306 verification code function);
    2. Prevention of violent registration

Personal idea: generate N-bit random numbers in a-z,a-z,1-9 to form a new verification code.

A few small functions of generating verification code

    1. Range ()//specify range output an array

A) such as: range (1,9)

    1. Array_merge ()//merge Array

A) array_merge (array 1, Array 2 ...)

    1. Array_rand (array, Number)

A) randomly remove several subscripts from the array to return an array

    1. Shuffle (array)//will again disturb the elements in the array
    2. Mt_rand (specify a range)//generate a better random number
    3. For Example: Mt_rand (1,5)//generate an arbitrary number between 1-5

Generate code codes for verification codes

<?php

$arr 1=range (' a ', ' z ');//specify a range to output an array

$arr 2=range (' A ', ' Z ');

$arr 3=range (1,9);

$arr =array_merge ($arr 1, $arr 2, $arr 3); Merging arrays

$index = Array_rand ($arr, 5); Randomly take 5 numbers in $arr, The return value is the subscript of $arr

Shuffle ($index);

$code = ';//define An empty string to store the generated verification code with ' dot ' for stitching

foreach ($index as $key + $value) {//traversal Array

$code. = $arr [$value];//the values in the array according to the subscript

}

Var_dump ($code);

?>

Run results

Perfect: to add the verification code to the image of such a verification code to be realistic

Describe the approximate steps for image creation before you complete

Create an image

Method One: Create a true color image (empty Canvas)

Imagecreatetruecolor (width, Height)//create a true color image

Description

    1. Width: breadth of canvas (pixels)
    2. Height: the width of the canvas (pixels)
    3. Return value is an image resource

Attention:

True Color image: Fill Color

Imagefill (image, x, y, color)//fill color for image resource

Description

    1. Image//images Resource
    2. x, y, The coordinate point of the fill (note: the color closest to this point is filled)

Color What color to fill with

For true color images: assigning colors

Imagecolorallocate (image, red, green, Blue)

Description

    1. Image//images Resource
    2. Red://color (0-255) or 0x (00-ff)//hexadecimal representation (0xff is 255)
    3. Green//green Color (0-255)
    4. Blue//blue Color (0-255)

Code demos for Imagefill and Imagecolorallocate

Effects when the canvas is not filled with color

Effects and code to fill the canvas with color

<?php

Creating an Image resource (blank Canvas) is displayed by default as Black

$image = Imagecreatetruecolor (300, 400);

1.image//image Resources

2.red://red color (0-255) or 0x (00-ff)//hexadecimal to indicate (0xff is 255)

3.green//green Color (0-255)

4.blue//blue Color (0-255)

$color = imagecolorallocate ($image, 255, 0, 0);

1.image//image Resources

2.x,y, filled coordinate point (note: the color closest to this point is filled)

3.color; What color to fill with

Imagefill ($image, 0, 0, $color);

Output image

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

Imagejpeg ($image);

Destroying image Resources

Imagedestroy ($image);

?>

Results

Output Image (jpeg for Example)

    1. Output image to Browser

A) Header (' Content-type:image/jpeg '); Set the image to view by browsing

B) imagejpeg (image resources)

    1. Output images by file

A) imagejpeg (image resource, ' image path ', image quality)//quality value 0-100

B) note:

Note: only the JPEG format has the quality of this parameter.

Destroying images

Imagedestroy ($image); Destroys the image and frees up memory Resources.

Note: Several image resources are currently generated and several are Destroyed.

The entire code of the Captcha:

<?php

Example: making text live in the middle of an image

Create an image resource (blank Canvas)

$image = Imagecreatetruecolor (100, 50);

$color = imagecolorallocate ($image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200));

Fill a color for an image resource

Imagefill ($image, 0, 0, $color);

Drawing an image

$font = 5;

The start of the verification code

$arr 1 = range (' a ', ' z ');

$arr 3 = range (' A ', ' Z ');

$arr 2 = Range (1,9);

Array_merge-merging one or more arrays

$arr = Array_merge ($arr 1, $arr 2, $arr 3);

$index = Array_rand ($arr, 5); Randomly identify 5 subscripts from the original array

$string = ";

foreach ($index as $value) {//$value two functions, which is the value in the $index and the subscript in the $arr

$string. = $arr [$value]; Will get the character to connect

}

End of verification code

Mt_rand-generate a better random number

echo Mt_rand (1,5);d ie;

Join Point Interference

$pointcolor = imagecolorallocate ($image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200));

Loop create 1000 interference points

For ($i =0; $i <1000; $i + +) {

Imagesetpixel ($image, mt_rand (0,imagesx ($image)), mt_rand (0,imagesy ($image)), $pointcolor);

}

Interference in line join

$lintecolor = imagecolorallocate ($image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200));

Loop Create 50 Line disturbances

For ($i =0; $i <50; $i + +) {

Imageline ($image, mt_rand (0,imagesx ($image)), mt_rand (0,imagesy ($image)), mt_rand (0,imagesx ($image)), mt_rand (0, Imagesy ($image)), $lintecolor);

}

Width of one character imagefontwidth ($font)

Number of strings: strlen (string)

The width of a character * number of strings

All string widths and = width of one character * number of strings

$x = (width of Canvas-all string widths AND)/2

$x = (imagesx ($image)-imagefontwidth ($font) *strlen ($string))/2;

$y = (height of the canvas-the height of the character)/2;

Height of character: imagefontheight ($font)

$y = (imagesy ($image)-imagefontheight ($font))/2;

$stringcolor = imagecolorallocate ($image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200));

Imagestring ($image, $font, $x, $y, $string, $stringcolor);

Output image

Header (' Content-type:image/jpeg '); Set the image to view by browsing

Imagejpeg ($image, ", 100); Exporting an image resource

Destroying image Resources

Imagedestroy ($image); Destroying images

Understand some of the functions in the code

Join the point of interference

Imagesetpixel (image, x, y, color)

Description: x, y coordinates of a point

Join the line of interference

Imageline (image, x1, y1, x2, y2, color)

Description: X1,y1 is an endpoint coordinate of the line; X2,y2 is the coordinate of the other port of the line; Draw a line from two points

Let the captcha reside in the center of the image

    1. Imagefontheight (font) Gets the height of the font:
    2. Imagefontwidth (font) Gets the width of the font:
    3. Strlen (string)//gets The length of the string
    4. Imagesx (image)//get the width of the canvas
    5. Imagesy (image)//get The height of the canvas

Last Run result

Perfect again (together with HTML Code)

HTML code

<! DOCTYPE html>

<meta charset= "UTF-8" >

<title>Document</title>

<body>

<form name= ' frm ' method= ' post ' action= ' >

<table width= "30%" border= "2" align= "center" rules= "all" cellpadding= "ten" >

<tr>

<th colspan= "2" > Please enter information </th>

</tr>

<tr>

<th> name:</th>

<th><input type= "text" name= "username" ></input></th>

</tr>

<tr>

<th> password:</th>

<th><input type= "password" name= "userpwd" ></input></th>

</tr>

<tr> 555556

<th> Verification Code </th>

<th><input type = ' text ' name = ' Checkcode ' ></input></th>

</tr>

<tr>

<th colspan= "2" ><input type= "submit" name= "submit" value= "submission" ></input></th>

</tr>

</table>

</form>

</body>

Understand

Final results

PHP Verification Code Applet

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.