Let's first check if our PHP is open to the GD library.
Copy CodeThe code is as follows:
if (extension_loaded (' GD ')) {
Echo ' You can use GD
';
foreach (Gd_info () as $cate = = $value)
echo "$cate: $value
";
}else
Echo ' You did not install GD extension ';
?>
If you have a return message, you can use it correctly.
Example 1
Copy CodeThe code is as follows:
/**
* Vcode (M,N,X,Y) m digital display size n edge width x Edge height y
* Rewrite the recording session yourself $code
*/
Session_Start ();
Vcode (4, 15); 4 digits with a display size of 15
function Vcode ($num = 4, $size = A, $width = 0, $height = 0) {
$width && $width = $num * $size * 4/5 + 5;
! $height && $height = $size + 10;
Removed 0 1 O l etc.
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = ";
for ($i = 0; $i < $num; $i + +) {
$code. = $str [Mt_rand (0, strlen ($str)-1)];
}
Drawing images
$im = Imagecreatetruecolor ($width, $height);
Define the colors you want to use
$back _color = Imagecolorallocate ($im, 235, 236, 237);
$boer _color = Imagecolorallocate ($im, 118, 151, 199);
$text _color = imagecolorallocate ($im, Mt_rand (0, 120), Mt_rand (0, +), Mt_rand (0,));
Painting background
Imagefilledrectangle ($im, 0, 0, $width, $height, $back _color);
Draw Border
Imagerectangle ($im, 0, 0, $width-1, $height-1, $boer _color);
Draw interference Lines
for ($i = 0; $i < 5; $i + +) {
$font _color = imagecolorallocate ($im, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
Imagearc ($im, Mt_rand (-$width, $width), Mt_rand (-$height, $height), Mt_rand ($width * 2), Mt_rand ($height * 2), M T_rand (0, Mt_rand), (0), $font _color);
}
Draw interference points
for ($i = 0; $i < $i + +) {
$font _color = imagecolorallocate ($im, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255));
Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $font _color);
}
Draw Verification Code
@imagefttext ($im, $size, 0, 5, $size + 3, $text _color, ' C:\WINDOWS\FONTS\SIMSUN.TTC ', $code);
$_session["Verifycode"]= $code;
Header ("cache-control:max-age=1, s-maxage=1, No-cache, must-revalidate");
Header ("content-type:image/png;charset=gb2312");
Imagepng ($im);
Imagedestroy ($im);
}
?>
Example 2
Using PHP, combined with the session and GD Library extension developed an example of generating verification code (recommended), can be easily used in the project. and beautiful style.
Copy CodeThe code is as follows:
First Open session
Session_Start ();
Define the foreground display verification code length & width
$image _width = 120;
$image _height = 40;
$characters _on_image = 6;
$font = './monofont.ttf ';
The characters that can is used in the CAPTCHA code.
Avoid confusing characters (l 1 and I for example)
$possible _letters = ' 23456789bcdfghjkmnpqrstvwxyz ';
$random _dots = 10;
$random _lines = 30;
$captcha _text_color= "0x142864";
$captcha _noice_color = "0x142864";
Define a string to generate a verification code
$code = ";
$i = 0;
while ($i < $characters _on_image) {
$code. = substr ($possible _letters, Mt_rand (0, strlen ($possible _letters)-1), 1);
$i + +;
}
$font _size = $image _height * 0.75;
$image = @imagecreate ($image _width, $image _height);
/* Setting the background, text and noise colours here */
$background _color = imagecolorallocate ($image, 255, 255, 255);
$arr _text_color = Hexrgb ($captcha _text_color);
$text _color = imagecolorallocate ($image, $arr _text_color[' red '),
$arr _text_color[' green '], $arr _text_color[' Blue ');
$arr _noice_color = Hexrgb ($captcha _noice_color);
$image _noise_color = imagecolorallocate ($image, $arr _noice_color[' red '),
$arr _noice_color[' green '], $arr _noice_color[' Blue ');
/* Generating the dots randomly in background */
for ($i =0; $i < $random _dots; $i + +) {
Imagefilledellipse ($image, Mt_rand (0, $image _width),
Mt_rand (0, $image _height), 2, 3, $image _noise_color);
}
/* generating lines randomly in background of image */
for ($i =0; $i < $random _lines; $i + +) {
Imageline ($image, Mt_rand (0, $image _width), Mt_rand (0, $image _height),
Mt_rand (0, $image _width), Mt_rand (0, $image _height), $image _noise_color);
}
/* Create a text box and add 6 letters code in IT */
$textbox = Imagettfbbox ($font _size, 0, $font, $code);
$x = ($image _width-$textbox [4])/2;
$y = ($image _height-$textbox [5])/2;
Imagettftext ($image, $font _size, 0, $x, $y, $text _color, $font, $code);
/* Show CAPTCHA image in the page HTML page */
Header (' content-type:image/jpeg ');//Defining the image Type to being shown in browser widow
Imagejpeg ($image);//showing the image
Imagedestroy ($image);//destroying the image instance
Set session, do validation
$_session[' 6_letters_code '] = $code;
function Hexrgb ($HEXSTR)
{
$int = Hexdec ($HEXSTR);
Return Array ("Red" = 0xFF & ($int >> 0x10),
"Green" 0xFF & ($int >> 0x8),
"Blue" 0xFF & $int);
}
Personal recommendation the second code to generate verification code, you can try the reference comparison Oh, the last one is the standard generation of the same is the use of PHP GD library.
http://www.bkjia.com/PHPjc/328174.html www.bkjia.com true http://www.bkjia.com/PHPjc/328174.html techarticle Let's first check if our PHP is open to the GD library. Copy the code as follows: PHP if (extension_loaded (' GD ')) {echo ' You can use GDBR '; foreach (Gd_info () as $cate = $v ...