Php 验证码图像未加载?

Php 验证码图像未加载?,php,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 = imagecreatetru

我怎样才能把这个装进去?现在,它根本没有显示任何图像。。。我并不擅长创建验证码,因为我通常从不这样做

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格式是不是搞砸了??

使用像recaptcha这样的captcha服务怎么样?

从您对图像URI和中的fontfile路径使用的常量
ASSETPATH
判断,我只能假设您的fontfile存储在与您的文件相同的路径中形象

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

但是,如果未打开“显示错误”,并且PHP无法找到您提供的fontfile,或者由于任何原因(例如权限)无法打开fontfile,则结果将是一个空白的170x60 PNG图像

如果我在本地测试您的代码,只要我为PHP提供正确的truetype字体文件的绝对路径,它就可以像预期的那样工作

例如,我的系统中有一些truetype字体存储在
/usr/share/fonts/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);
$string='';
对于($i=0;$i<5;$i++){
//此数字指ascii表格的数字(小写)
$string.=chr(兰特(97122));
}
$image=imagecreatetruecolor(170,60);
$color=imagecolorallocate($image,20010090);//红色
$white=imagecolorallocate($image,255,255);
imagefilledrectangle($image,0,0399,99,$white);
imagettftext($image,30,0,10,40,$color,'/usr/share/font/truetype/ttf dejavu/DejaVuSans ExtraLight.ttf',$string);
标题(“内容类型:图像/png”);
imagepng($image);


为了进一步调试,您应该尝试运行PHP脚本,该脚本自行生成图像,打开显示错误并将其设置为
-1
,如果字体文件确实存在问题,您将在错误日志或在显示错误的情况下测试脚本时显示的错误输出中看到这一点。

如果您必须使用验证码,请利用诸如reCAPTCHA之类的服务—由您的用户生成的验证码自定义脚本(白色背景上的纯文本)很容易破解。