Php method of implementing Code Applet

Source: Internet
Author: User
This paper mainly introduces the implementation method of the Code applet based on PHP implementation, and makes detailed comments, which is helpful for understanding and learning, and it is necessary for friends to see it together.

Verification code function (personal understanding):

    • Reduce the pressure on the server (such as the 12306 Verification code function);

    • 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

Range ()//Specify range output an array
A) such as: Range (1,9)
Array_merge ()//merge array
A) Array_merge (array 1, array 2 ...)
Array_rand (array, number)
A) randomly remove several subscripts from the array to return an array

    • Shuffle (array)//will again disturb the elements in the array

    • Mt_rand (Specify a range)//Generate a better random number

    • 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); Merge array $index = Array_rand ($arr, 5); 5 numbers are randomly taken in $arr, and the return value is $arr subscript Shuffle ($index);  $code = ";//define an empty string to store the generated verification code with a ' dot ' to splice the foreach ($index as $key + = $value) {//iterate over the 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) //创建一个真彩色图像

Description

    • Width: breadth of canvas (pixels)

    • Height: The width of the canvas (pixels)

    • Return value is an image resource

Attention:

True Color Image: Fill Color

imagefill(image, x, y, color) //为图像资源填充颜色

Description

    • Image//images Resource

    • 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

    • Image//images Resource

    • Red://Color (0-255) or 0x (00-FF)//hexadecimal representation (0xFF is 255)

    • green//green color (0-255)

    • 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 (+);//1.image//Image resource//2.red://Red color (0-255) or 0x (00-ff //That is 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//images resource//2.x,y, fill the coordinate point (note: Fill the closest color to this point)//3.color; What color to fill imagefill ($image, 0, 0, $color);//Output Image header (' content-type:image/jpeg '); imagejpeg ($image);// Destroy Image Resource Imagedestroy ($image);? >


Results

Output image (JPEG for example)

Output image to Browser

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

b) imagejpeg (image resources)

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); //销毁图像,释放内存资源.

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

The entire code of the CAPTCHA:


<?php//instance: Make text live in the center of the image//Create an image resource (blank canvas) $image = Imagecreatetruecolor (+); $color = Imagecolorallocate ($image, Mt_ Rand (20,200), Mt_rand (20,200), Mt_rand (20,200));//Fill the image resource with color Imagefill ($image, 0, 0, $color);//Draw Image $font = 5;//Verification Code start $ arr1 = Range (' A ', ' Z '), $arr 3 = range (' A ', ' Z '), $arr 2 = range (1,9),//array_merge-merges one or more arrays $arr = Array_merge ($arr 1, $arr 2, $arr 3); $index = Array_rand ($arr, 5); Random from the original array to find 5 subscript $string = ", foreach ($index as $value) {//$value two functions, that is, the value in the $index, is also the subscript $string in the $arr. = $arr [$value]; Get the character to connect the end of the}//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);} Join line Interference $lintecolor = Imagecolorallocate ($image, Mt_rand (20,200), Mt_rand (20,200), Mt_rand (20,200));//loop 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 a character imagefontwidth ($font)//Number of Strings: strlen (String)//character width * Number of strings//all string widths and = width of one character * number of strings//$x = ( Width of the 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;//the height of the 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 by browsing to view imagejpeg ($image, ", 100); Image resource output//Destroy Image Resource 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


Imagefontheight (font) Gets the height of the font: imagefontwidth (font) Gets the width of the font: strlen (string)//Gets the length of the string imagesx (image)//Gets the width of the canvas imagesy ( image)//Get the height of the canvas


Last Run result

Perfect again (together with HTML code)

HTML code


<! DOCTYPE html>


Understand

Final results


The above is the whole content of this article, I hope that everyone's study has helped.


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.