Let's first check whether our php has the gd library turned on.
!$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)];
}
// Draw an image
$im = imagecreatetruecolor($width, $height);
// Define the color to be used
$back_color = imagecolorallocate ($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0 , 120), mt_rand(0, 120));
// Draw the background
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// Draw the 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(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color) ;
}
// Draw interference points
for($i = 0;$i < 50;$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:WINDOWSFontssimsun.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
An example of generating verification codes developed using PHP, combined with session and GD library extensions (recommended by w3c), which can be easily used in projects. And the style is beautiful
Copy the code
The code is as follows:
//First start the session
session_start();
//Define the length & width of the verification code displayed in the front desk
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example ; 864";
//Define the string to generate the 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 colors 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 ; 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 be shown in browser widow
imagejpeg ($image);//showing the image
imagedestroy($image);//destroying the image instance
//Set session and do verification
$_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 one generates the verification code program code. Students can try to compare it for reference. The last one is generated by W3C standard and also uses the php gd library.
http://www.bkjia.com/PHPjc/328174.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/328174.html
TechArticleLet’s first check whether our php has the gd library turned on. Copy the code as follows: ?php if(extension_loaded('gd')) { echo 'You can use gdbr'; foreach(gd_info() as $cate=$v...