验证码头没有加载?

时间:2012-12-07 23:04:18

标签: php

如何加载?现在,它根本没有显示任何图像......我在制作验证码时并不是那么出色,因为我通常都不会这样做。

captcha.php:

<?php
$string = '';
for ($i = 0; $i < 5; $i++) {
    // this numbers refer to numbers of the ascii table (lower case)  
    $string .= chr(rand(97, 122));
}
$image = imagecreatetruecolor(170, 60);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, ASSETPATH."arial.ttf", $string);
header("Content-type: image/png");
imagepng($image);
?>

register.php:

<?php echo '<img src="'.ASSETPATH.'img/captcha.php" />'; ?>

我的资产路径是正确的,因为我在其他地方使用它,它加载完全正常。这个项目的MVC格式是否会以某种方式弄乱它?

2 个答案:

答案 0 :(得分:0)

如何使用recaptcha之类的验证码服务?

答案 1 :(得分:0)

根据您对imagettftext中的图像URI以及 fontfile 路径使用此常量ASSETPATH判断我只能假设您已将字体文件存储在和你的形象一样的路径?

如果不是这种情况,或者无法在那里打开文件,PHP将发出警告imagettftext(): Invalid font filename。如果在php.ini中将display_errors设置为 On (您可以检查phpinfo以验证这一点),这意味着错误消息将与您一起发送到输出流图像数据(即破坏图像数据并导致浏览器不显示图像)。这也可以防止标题被修改,因为在调用标题之前会发生错误。

但是,如果未打开display_errors并且PHP无法找到您提供的字体文件或因任何原因无法打开(例如权限),则结果将是一个空白的170x60 PNG图像。

如果我在本地测试你的代码,它证明可以按预期工作,只要我为PHP提供了我的truetype字体文件的正确绝对路径。

例如,我在/usr/share/fonts/truetype/存储的系统上有一些truetype字体,这绝对不在我的webroot中,因为我通常不会保留我的字体文件。另请注意,我的PHP用户有足够的权限来读取此路径。

现在,如果我向PHP提供了我想要使用的truetype字体文件的正确绝对路径,我将使用您的代码获取以下图像...

$string = '';
for ($i = 0; $i < 5; $i++) {
    // this numbers refer to numbers of the ascii table (lower case)
    $string .= chr(rand(97, 122));
}
$image = imagecreatetruecolor(170, 60);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-ExtraLight.ttf', $string);
header("Content-type: image/png");
imagepng($image);

Image output from the above code

为了进一步调试,您应该尝试做的是运行自己生成图像的PHP脚本,并打开display_errors并将error_reporting设置为-1,如果确实是如果您的字体文件出现问题,您将在error_log中或在测试带有display_errors的脚本期间显示的错误输出中看到此问题。