PHP image operation-generating image verification code and php image Verification Code
A simple verification code is actually a few characters output in the image, which can be achieved through the imagestring function.
However, in terms of processing, in order to make the verification code more secure and Prevent Automatic Identification by other programs, it is often necessary to interfere with the verification code. Generally, noise and line segments are drawn, skew and distortion of output characters.
You can use imagesetpixel to draw a point to Implement noise interference, but it is not helpful to draw only one point. Therefore, here we often use loops to draw randomly.
Example:
<? Php
$ Img = imagecreatetruecolor (100, 40 );
$ Black = imagecolorallocate ($ img, 0x00, 0x00, 0x00 );
$ Green = imagecolorallocate ($ img, 0x00, 0xFF, 0x00 );
$ White = imagecolorallocate ($ img, 0xFF, 0xFF, 0xFF );
Imagefill ($ img, 0, 0, $ white );
// Generate a random Verification Code
$ Code = '';
For ($ I = 0; $ I <4; $ I ++) {// 4-digit Verification Code
$ Code. = rand (0, 9 );
}
Imagestring ($ img, 5, 10, 10, $ code, $ black );
// Add noise interference
For ($ I = 0; $ I <50; $ I ++ ){
Imagesetpixel ($ img, rand (0,100), rand (0,100), $ black); // imagesetpixel-draw a single pixel. Syntax: bool imagesetpixel (resource $ image, int $ x, int $ y, int $ color)
Imagesetpixel ($ img, rand (0,100), rand (0,100), $ green );
}
// Output the verification code
Header ("content-type: image/png ");
Imagepng ($ img); // output the image to a browser or file in PNG format
Imagedestroy ($ img); // after image processing is complete, use the imagedestroy () command to destroy image resources to release memory. Although this function is not necessary, it is a good habit to use it.
?>