我尝试通过PHP GD制作Captcha。但不幸的是我遇到了一个问题! PHP告诉我:
The image “http://127.0.0.1/par.php” cannot be displayed because it contains errors.
我的代码就是这个
<?php
header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
$red = imagecolorallocate($im, 255, 0, 0);
for ($i=0;i<=120;$i=$i+1){
for ($j=0;$j<=20;$j=$j+1){
imagesetpixel($im, $i,$j,$red);
}
}
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>
答案 0 :(得分:4)
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
您首先隐藏真实错误并尝试显示某些内容......
您无法显示,因为您无法查找,
并且无论是否真的生成了图像都会曝光。
然后你继续进行stackoverflow,希望有人能猜到你可能只是使用@
运算符来抑制错误。
确保<?php
之前没有任何内容,如果有,请在结尾删除?>
。
要确保安装了GD,请在新的php文件中尝试:
<?php
if (extension_loaded('gd') && function_exists('gd_info')) {
echo "PHP GD library is installed on your web server";
}
else {
echo "PHP GD library is NOT installed on your web server";
}
答案 1 :(得分:3)
问题在于
将其更改为:
for ($i=0; $i < 120 ; $i++) {
for ($j=0; $j < 20 ; $j++) {
imagesetpixel($im, $i,$j,$red);
}
}
修改
这是我测试的代码:
header ('Content-Type: image/png');
$im = imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 0, 14, 91);
$red = imagecolorallocate($im, 255, 0, 0);
for ($i=0; $i < 120 ; $i++) {
for ($j=0; $j < 20 ; $j++) {
imagesetpixel ($im ,$i,$j ,$red);
}
}
imagestring($im, 1, 5, 5,'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
结果:
答案 2 :(得分:1)
仅在FireFox中出现此问题,而不是Chrome。如果不引起其他问题,你应该继续。即使错误显示消失,也不应删除标题()。
答案 3 :(得分:-2)
很可能你的php输出了一些警告或通知,搞乱你的图像数据。 尝试在 imagepng()函数调用之前中断代码以查看是否生成了任何错误或警告,或尝试停止php.ini或ini_set中的所有警告和通知。