Javascript 无法显示验证码图像。获取方形缩略图而不是验证码图像(核心PHP)
我无法显示使用核心PHP生成的验证码图像。它显示一个方形缩略图,而不是验证码图像。字体文件和路径是最新的 它应该显示验证码,而不是方形缩略图(就像没有图像,静态图像路径是打开的一样) 代码:index.php(用于在前端显示)Javascript 无法显示验证码图像。获取方形缩略图而不是验证码图像(核心PHP),javascript,php,html,captcha,Javascript,Php,Html,Captcha,我无法显示使用核心PHP生成的验证码图像。它显示一个方形缩略图,而不是验证码图像。字体文件和路径是最新的 它应该显示验证码,而不是方形缩略图(就像没有图像,静态图像路径是打开的一样) 代码:index.php(用于在前端显示) 验证码演示-UandBlog 验证码演示-UandBlog 姓名: 电邮: 信息: 验证码: *请在下面填写验证码安全性 “id='captchaimg'width=“40%”> 无法读取图像?单击以刷新 提交 函数refreshCaptcha() { var img=
验证码演示-UandBlog
验证码演示-UandBlog
姓名:
电邮:
信息:
验证码:
*请在下面填写验证码安全性
“id='captchaimg'width=“40%”>
无法读取图像?单击以刷新
提交
函数refreshCaptcha()
{
var img=document.images['captchaimg'];
img.src=img.src.substring(0,img.src.lastIndexOf(“?”)+“?rand=“+Math.random()*1000;
}
代码:captcha\u Code\u file.php(用于生成验证码图像)
重新处理验证码后,我发现了问题所在,即字体路径不正确(在我的系统上肯定是这样)当使用完全限定的路径时,captcha工作正常。因此-我建议使用字体的完整路径,而不是您在这里使用的
/
语法…我修改了hexrgb
函数来输出对象,只是因为我更喜欢它
如果您在代码中注释掉输出图像的最后几行(或尝试注释),您应该(嗯,我确实)得到一条错误消息
警告:imagettfbbox()[function.imagettfbbox.html]:无效字体
文件名在
它生成的验证码示例
非常感谢。我更改了路径并成功了。但是,我仍然不确定它是如何发生的,因为字体文件在同一个文件夹中。无论如何,谢谢。在这种情况下,您可以使用
\uuu DIR\uuu.'/monofont.ttf'
<!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>
<?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);
}
?>
<?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
);
}
?>