我无法显示使用核心PHP生成的验证码图像。它显示方形缩略图而不是验证码图像。字体文件和路径是最新的。
代替正方形缩略图(就像没有图像并且静止图像路径打开一样),它应该显示验证码
代码:index.php(用于在前端显示)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Captcha Demo - UandBlog</title>
</head>
<body style="background-color:#D8D8D8;">
<div style="width:500px; margin:0 auto; margin-top:100px; background:#F0F0F0; padding:10px;">
<h2>Captcha Demo - UandBlog</h2>
<form action="submitPage.php" method="post">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr style="color:#F00;">
<td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:15px;">
<?php if(isset($_REQUEST['msg1'])){?>
<?php echo $_REQUEST['msg1'];?>
<?php } ?>
</td>
</tr>
<tr>
<td>Name:</td>
<td><input name="name" type="text" required></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" type="email"></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="msg" cols="" rows=""></textarea></td>
</tr>
<tr>
<td style=" text-transform:uppercase;" valign="top">captcha:</td>
<td>*Please fill in the captcha security below</td>
</tr>
<tr>
<td valign="top"> </td>
<td ><input id="6_letters_code_1" name="6_letters_code_1" required type="text" style="border:1px solid #e2d5ca !important;"></td>
</tr>
<tr>
<td class="blk" valign="top"> </td>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align:left;"> <img src="captcha_code_file.php?rand=<?php echo rand();?>" id='captchaimg' width="40%"></td>
<td style="text-align:right;"></td>
</tr>
<tr>
<td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:11px;"> Can't read the image? click <a href='javascript: refreshCaptcha();' style="color:#F00;">here</a> to refresh</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
<td><button class="submit_btn" name="Submit1" type="submit">Submit</button></td>
</tr>
</table>
</form>
</div>
<script type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</body>
</html>
代码:captcha_code_file.php(用于生成验证码图像)
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 200;
$image_height = 60;
$characters_on_image = 5;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 140;
$random_lines = 5;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$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 be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code_1'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
答案 0 :(得分:0)
重新处理了验证码代码后,我发现了这个问题-即字体路径不正确(在我的系统上肯定是),并且当使用完全限定的路径时,验证码可以正常工作。所以-我建议使用完整的字体路径,而不是这里使用的./
语法...我修改了hexrgb
函数以仅因为喜欢它而输出对象。 / p>
如果用您的代码注释掉了输出图像的最后几行(或尝试),您应该得到(好吧,我确实)了一条错误消息
警告:imagettfbbox()[function.imagettfbbox.html]:无效的字体 文件名输入.....
<?php
session_start();
$width = 200;
$height = 60;
$characters = 5;
$font = 'c:/wwwroot/webfonts/arial.ttf';
$chars = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 140;
$random_lines = 5;
$captcha_text_color="0x142864";
$captcha_noise_color = "0x142864";
$code = '';
$i = 0;
while( $i < $characters ) {
$code .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
$i++;
}
$font_size = $height * 0.75;
$image = @imagecreate( $width, $height );
$background_color = imagecolorallocate($image, 255, 255, 255);
$rgb = hexrgb( $captcha_text_color );
$text_color = imagecolorallocate($image,
$rgb->r,
$rgb->g,
$rgb->b
);
$rgb = hexrgb( $captcha_noise_color );
$noise_color = imagecolorallocate($image,
$rgb->r,
$rgb->g,
$rgb->b
);
/* generating the dots randomly in background */
for( $i=0; $i < $random_dots; $i++ ) {
imagefilledellipse( $image, mt_rand( 0, $width ), mt_rand( 0, $height ), 2, 3, $noise_color );
}
/* generating lines randomly in background of image */
for( $i=0; $i < $random_lines; $i++ ) {
imageline( $image, mt_rand( 0, $width ), mt_rand( 0, $height ), mt_rand( 0, $width ), mt_rand( 0, $height ), $noise_color );
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox( $font_size, 0, $font, $code );
$x = ( $width - $textbox[4] )/2;
$y = ( $height - $textbox[5] )/2;
imagettftext( $image, $font_size, 0, $x, $y, $text_color, $font , $code );
header('Content-Type: image/jpeg');
imagejpeg( $image );
imagedestroy( $image );
$_SESSION['6_letters_code_1'] = $code;
function hexrgb( $hexstr ){
$int = hexdec( $hexstr );
return (object)array(
'r' => 0xFF & ($int >> 0x10),
'g' => 0xFF & ($int >> 0x8),
'b' => 0xFF & $int
);
}
?>
它产生的验证码示例