Php-based verification code Applet and php Verification Code Applet

Source: Internet
Author: User
Tags imagejpeg

Php-based verification code Applet and php Verification Code Applet

Verification Code function (personal understanding ):

  • Reduce the pressure on the server (for example, the verification code 12306 function );
  • Prevents violent Registration

Personal thinking: In a-z, A-Z, 1-9 generate n random numbers to form a new verification code.

Several small functions for generating verification Codes

Range () // specify the range to output an array
A) For example: range)
Array_merge () // merge Arrays
A) array_merge (array 1, array 2 ....)
Array_rand (array, quantity)
A) randomly retrieve several subscripts from the array and return an array.

  • Shuffle (array) // will disrupt the elements in the array again
  • Mt_rand (specify a range) // generate a better Random Number
  • For example, mt_rand () // generates an arbitrary number between 1 and 5.

Generate Verification Code

<? Php $ arr1 = range ('A', 'z'); // specify the range to output an array $ arr2 = range ('A', 'z '); $ arr3 = range (1, 9); $ arr = array_merge ($ arr1, $ arr2, $ arr3); // merge arrays $ index = array_rand ($ arr, 5 ); // In $ arr, the number of 5 is random. The returned value is $ arr subscript Shuffle ($ index); $ code = ''; // define an empty string to store the generated verification code. Use 'point' to splice foreach ($ index as $ key => $ value) {// traverse the array $ code. = $ arr [$ value]; // obtain the value in the array according to the subscript} var_dump ($ code);?>

Running result

Perfect: You need to add the verification code to the image to make it realistic.

Before making improvements, let's take a look at the general steps for creating images.

Create an image

Method 1: create a true color image (empty canvas)

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

Note:

  • Width: the width (in pixels) of the canvas)
  • Height: the height of the canvas in pixels)
  • The returned value is image resource.

Note:

True Color Image: fill color

Imagefill (image, x, y, color) // fill the color of image resources

Note:

  • Image // image resources
  • X, y, fill coordinate point (Note: Fill the closest color to this point)
  • Color; // The color used for filling.

True Color Image: Color allocation

imagecolorallocate(image, red, green, blue)

Note:

  • Image // image resources
  • Red: // the red color (0-255) or 0x (00-ff) // indicates the hexadecimal notation (0xff is 255)
  • Green // green color (0-255)
  • Blue // blue color (0-255)

Code demonstration of imagefill and imagecolorallocate

Effect when the canvas is not colored

Effects and code when adding colors to the canvas

<? Php // create image resources (blank canvas) Black by default $ image = imagecreatetruecolor (300,400); // 1. image // image resource // 2.red: // red color (0-255) or 0x (00-ff) // that is, hexadecimal representation (0xff is 255) // 3. green // green color (0-255) // 4. blue // blue color (0-255) $ color = imagecolorallocate ($ image, 255, 0, 0); // 1. image // image resource // 2.x, y, fill coordinate point (Note: Fill the closest color to this point) // 3. color; // The color used to fill the imagefill ($ image, 0, 0, $ color); // output image header ('content-type: image/jpeg '); imagejpeg ($ image); // destroy the image resource imagedestr Oy ($ image);?>

Result;

Output image (using jpeg as an example)

Output the image to the browser

A) header ('content-type: image/jpeg '); // you can view the image through browsing.

B) imagejpeg (image resources)

Output images by file

A) imagejpeg (image resource, 'image path', image quality) // the image quality value ranges from 0 to 100.

B) Note:

Note: Only the jpeg format can have the quality parameter.

Destroy Images

Imagedestroy ($ image); // destroy the image to release memory resources.

Note: several image resources are generated and destroyed.

The entire code of the Verification Code:

<? Php // example: Place text 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 the image resource color imagefill ($ image, 0, 0, $ color ); // draw the image $ font = 5; // start of the Verification Code $ arr1 = range ('A', 'z'); $ arr3 = range ('A ', 'Z'); $ arr2 = range (); // array_merge-merges one or more arrays $ arr = array_merge ($ arr1, $ arr2, $ arr3 ); $ index = array_rand ($ arr, 5); // randomly locate 5 subscripts from the original array $ string = ''; foreach ($ index as $ value) {// $ value is the value in $ index and the subscript $ string in $ arr. = $ arr [$ value]; // connect the obtained characters} // end of the Verification Code // mt_rand-generate a better random number // echo mt_rand (); die; // Add point interference $ pointcolor = imagecolorallocate ($ image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200 )); // create 1000 interference points in a loop for ($ I = 0; $ I <1000; $ I ++) {imagesetpixel ($ image, mt_rand (0, imagesx ($ image), mt_rand (0, imagesy ($ image), $ pointcolor);} // line interference $ lintecolor = imagecolorallocate ($ image, mt_rand (20,200), mt_rand (20,200), mt_rand (20,200); // create 50 line interference 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) ;}// the width of one character imagefontwidth ($ font) // The number of strings: strlen (string) // width of one character * Number of strings // width of all strings and = width of one character * Number of strings // $ x = (width of the canvas-width of all strings and) /2 $ x = (imagesx ($ image)-imagefontwidth ($ font) * strlen ($ string)/2; // $ y = (canvas height-character height)/2; // character height: 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 to view images through browsing imagejpeg ($ image, '', 100); // output image resources // destroy image resources imagedestroy ($ image ); // destroy the image

Understand some functions in the code

Interference point added

imagesetpixel(image, x, y, color)

Note: coordinates of a vertex x and y

Add interference line

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

Note: x1, y1 is an endpoint coordinate of the line; x2, y2 is the coordinate of another port of the line; draw a line from two points

Place the verification code in the middle of the image

Imagefontheight (font) to get the font Height: imagefontwidth (font) to get the font width: strlen (string) // to get the length of the string imagesx (image) // obtain the canvas width imagesy (image) // obtain the canvas height

Final running result

Further improvement (combined with html code)

Html code

<! DOCTYPE html> 

Understanding;

Final Result

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.