The latest and most full PHP generation verification Code details (recommended), detailed verification Code _php tutorial

Source: Internet
Author: User

The latest and most full PHP generation verification Code details (recommended), detailed verification code


1 First look at the code first

OK, now combine the above code, to analyze and analyze the above used several functions:

①imagecreatetruecolor ();

imagecreatetruecolor-Create a new true color image (feel wow, so long, actually carefully look at a very good remember image/create/true/color, what is true color image? Look down)

Resource Imagecreatetruecolor (int $width, int $height)

Imagecreatetruecolor () and imagecreate () two functions can create a canvas

Imagecreatetruecolor () creates a black image of size x and Y (the default is black [even if it's a true color image]), and if you want to change the background color, you need to

To use the Fill color function Imagefill ($img, 0,0, $color);

Imagecreate Create a new blank image resource, add a background color with imagecolorallocate ()

The above two functions are nothing more than a function of two ways

②imagecolorallocate ();

imagecolorallocate-assigning colors to an image

int Imagecolorallocate (resource $image, int $red, int $green, int $blue)

The colors are made up of red, green, and blue three-color combinations, which are integers from 0 to 255 or hex 0x00 to 0xFF.

③mt_rand ();

mt_rand-generate a better random number

int Mt_rand (int $min, int $max)

$min optional, returned minimum value (default: 0) $max optional, maximum value returned (default: Mt_getrandmax ())
This is used to let him randomly generate background color, 0-255 random value. So the page does not refresh once the canvas background color is different. :

2.0 begin to make a disturbance line, which disturbs the point. Prevent verification images from being recognized by seconds

Function Analysis:

①imageline ();

imageline-Drawing a line segment

Imageline () Draws a line segment from coordinates x1,y1 to X2,y2 (0, 0 in the upper left corner of the image) with a color color.

Imageline ($img, Mt_rand (0,150), Mt_rand (0,50), Mt_rand (0,150), Mt_rand (0,50), $linecolor); This means that the canvas $img from coordinates x1,y1 to x2, Y2 random

②imagesetpixel ();

imagesetpixel-draw a single pixel

BOOL Imagesetpixel (resource $image, int $x, int $y, int $color)

Imagesetpixel () Draws a point on the image with a color in x, y coordinates (0,0 in the upper-left corner of the image).



3.0 Adding a verification letter number

<?phpheader ("content-type:text/html; charset=utf-");//Set the encoding style header (" Content-type:image/jpeg ") of the page,//notify the browser output is JPEG format image $img = Imagecreatetruecolor (,); /create canvas and set size x-axis y-Axis $bgcolor = imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,));//Assign background color//Add interference line, and cycle times, Background color random for ($i =; $i <; $i + +) {$linecolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)); Imageline ($img , Mt_rand (,), Mt_rand (,), Mt_rand (,), Mt_rand (,), $linecolor);} Add interference points, and loop the times, background color random for ($i =; $i <; $i + +) {$dotcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)); Imagesetpixel ($img, Mt_rand (,), Mt_rand (,), $dotcolor);} Add letters or numbers that need to be verified $rand_str = "QWERTYUIOPASDFGHJKLZXCVBNM";//need to use some letters and numbers to verify $str_arr = Array (); Name an array for ($i =; $i <; $i + +) {//round robin, there are four random letters or numbers $pos = Mt_rand (, strlen ($rand _str)-); $str _arr[] = $rand _str[$pos] ;//Temporary Swap} $x _start=/;//single character x-axis position foreach ($str _arr as $key) {$fontcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)); Imagettftext ($img,, Mt_rand (-), $x _start,/, $FONTCOlor, "C:/windows/fonts/verdana.ttf", $key); $x _start +=;//traverse a single character along the x-axis +}imagefill ($img,,, $bgcolor); Fill the background with the image imagejpeg ($img); Output Image Imagedestroy ($img);  Destroy Image?>

Function:

Imagettftext ();

imagettftext-writing text to an image with TrueType fonts

Analyze the following code:

$img-----------Canvas

-----------the size of the font.

Mt_rand ( -15,15) angle is----------angle, and 0 degrees is the text that reads from left to right. A higher value indicates a counterclockwise rotation. For example, 90 degrees is the text that reads from the bottom up. (That's the problem with the font angle)

$x _start----------Easy to understand is the x-axis position of the character

50/2 height of----------characters

$fontcolor----------Character Color

Font style path for "C:/windows/fonts/verdana.ttf"----------characters

$key-----------The characters after the traversal

Effect:


The above content is this article to introduce you to the latest and most full PHP production verification Code details (recommended) of the full narrative, I hope to help you!

http://www.bkjia.com/PHPjc/1136666.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136666.html techarticle the latest and most full PHP production verification Code details (recommended), Verification code 1 first look at the code phpheader ("content-type:text/html; charset=utf-");//Set the encoding style of the page Hea ...

  • 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.