Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';无法获取要显示的GD图像_Php_Image_Gd_Captcha - Fatal编程技术网

Php Can';无法获取要显示的GD图像

Php Can';无法获取要显示的GD图像,php,image,gd,captcha,Php,Image,Gd,Captcha,我的代码: 将此文件另存为captcha.php <?php ob_start(); session_start(); if(!isset($_POST['submit'])){ echo "<form method=\"post\" action=\"captcha.php\">\n"; echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "

我的代码: 将此文件另存为captcha.php

    <?php
ob_start();
session_start();
if(!isset($_POST['submit'])){
    echo "<form method=\"post\" action=\"captcha.php\">\n";
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<tr><td>Type The Letters You See Below Into the Box</td></tr>\n";
    echo "<tr><td align=\"center\"><img src=\"image.php\"></td></tr>\n";
    echo "<tr><td align=\"center\"><input type=\"text\" name=\"image\"></td></tr>\n";
    echo "<tr><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Check CAPTCHA\"></td></tr>\n";
    echo "</table></form>\n";
}else {
    $image = $_POST['image'];
    if ($image == $_SESSION['string'])
    {
        echo "<b>Great success!</b>\n";
    }
    else 
    {
        echo "<em>Failure!</em>\n";
    }
}
ob_end_flush();
?> 

将此文件另存为image.php

<?php

session_start();
$img = imagecreatetruecolor(118,80);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$red = imagecolorallocate($img, 255, 0, 0);
$grey = imagecolorallocate($img,150, 150, 150);
$pink = imagecolorallocate($img, 200, 0, 150);
$yellow = imagecolorallocate($img, 255, 246, 0);

function randomString($length){
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $str = "";
    $i = 0;

        while($i <= $length){
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $str = $str . $tmp;
            $i++;
        }
    return $str;
}

for($i=1;$i<=rand(1,5);$i++){
    $color = (rand(1,2) == 1) ? $pink : $red;
    imageline($img,rand(6,90),rand(6,40), rand(6,90)+6,rand(6,40)+6, $color);
}

imagefill($img, 0, 0, $yellow);

$string = randomString(rand(4,6));
$_SESSION['string'] = $string;

imagettftext($img, 20, 10, 10, 52, $black, "calibri.ttf", $string);
imagettftext($img, 20, 10, 12, 53, $grey, "calibri.ttf", $string);

header("Content-type: image/png");
imagepng($img);
imagedestroy($img);

?>

转到这个地址,下载字体并将其保存在与我们刚才创建的to.php文件相同的根文件夹中


注意!!这将在LOCALHOST上运行,但我对image.php文件的显示方式有一个问题->����JFIF��>创建者:gd jpeg v1.0(使用IJG jpeg v90),默认质量��C$。“,#(7),01444'9=82我想你发布的代码不是整个文件:尝试删除
标记前后的所有内容。你甚至可以(这是许多人采用的样式约定,因为这个问题)

这会影响您正在执行的所有其他文件:PHP标记之外或PHP回显的所有空格、换行符或其他内容都会添加到图像内容中,并最终导致图像损坏。此外,PHP通知和警告也是一个问题

为清楚起见,执行的文件格式必须为:

<?php // nothing before this line
// no other PHP tags till the end
... // PHP code with no echo calls
// no closing tag
或者将脚本用作HTML页面,删除

header( "Content-type: image/png" );

此代码运行良好。我已经在chrome和firefox上对其进行了本地测试。

我也遇到了同样的问题。删除所有html标记,它将100%正常工作

另存为php文件..index.php

<?php
/**
 * PHP GD
 * create a simple image with GD library
 * 
 */
//setting the image header in order to proper display the image
header("Content-Type: image/png");
//try to create an image
$im = @imagecreate(800, 600)
    or die("Cannot Initialize new GD image stream");
//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
//set the color for the text
$text_color = imagecolorallocate($im, 133, 14, 91);
//adf the string to the image
imagestring($im, 5, 300, 300,  "I'm a pretty picture:))", $text_color);
//outputs the image as png
imagepng($im);
//frees any memory associated with the image 
imagedestroy($im);
?>

将标题移动到代码的第一行,看看它是如何运行的。看起来您的浏览器没有接收到标题。还可以尝试使用输出缓冲捕获
imagejpeg()
的数据,然后在脚本末尾回显它。我已经在本地测试过,它工作正常。然后出现了一些不同的情况!甚至是““在图像内容前面可能会使浏览器无法读取,而且很可能他在那里得到了类似的东西。你可以在我做的这个上测试一下,它工作正常吗?”?你想在哪个程序中运行它?是的,我不能发布我们在cms中使用的代码,但我的问题不是本地的,如果我运行验证码,图像会很好地显示出来,一切正常,但一旦我将其作为插件添加到我们的cms中,它会告诉我无法加载图像的给定URL。此外,我还删除了所有的白色间距。我可以给你文件代码检查吗?所以问题是在CMS文件执行时,你调用图像文件脚本。您必须确保前面调用的图像生成脚本(库等)文件符合我在答案中编写的格式。这是你必须做的检查,你不能把你所有的文件都发给我们。这可能是一个长期的工作感谢很多人非常感谢我没有让它工作,但我会继续挖掘,如果我来的权利,我会张贴给你们的种子在你们的CMS代码,重新格式化它可能是相当困难的;您可以尝试从图像脚本中删除不必要的
include
s,并查看图像是否已损坏,以便发现坏文件。工作正常V已将完整代码添加到链接ID is链接ID:btf-i1a。如果你可以看一下,请检查我在phpfiddle.org上的代码链接id:btf-i1a是链接id,看看当我把你的代码放在编辑器中并点击运行按钮时,你是否能找出我哪里出错了,它给出了一个图像作为输出,上面写着“一个简单的文本字符串”。再试一次我正忙着修改代码,但如果你使用“imagestring($img,20,10,10,$String,$black);”图像PNG($img);图像处理(img);在第39行和注释other之后,它给出图像作为输出。
<img src="png_generator_script.php"/>
header( "Content-type: image/png" );
<?php
/**
 * PHP GD
 * create a simple image with GD library
 * 
 */
//setting the image header in order to proper display the image
header("Content-Type: image/png");
//try to create an image
$im = @imagecreate(800, 600)
    or die("Cannot Initialize new GD image stream");
//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
//set the color for the text
$text_color = imagecolorallocate($im, 133, 14, 91);
//adf the string to the image
imagestring($im, 5, 300, 300,  "I'm a pretty picture:))", $text_color);
//outputs the image as png
imagepng($im);
//frees any memory associated with the image 
imagedestroy($im);
?>
<html>
<head>
</head>
<body>

<?php
/**
 * PHP GD
 * create a simple image with GD library
 * 
 */
//setting the image header in order to proper display the image
header("Content-Type: image/png");
//try to create an image
$im = @imagecreate(800, 600)
    or die("Cannot Initialize new GD image stream");
//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
//set the color for the text
$text_color = imagecolorallocate($im, 133, 14, 91);
//adf the string to the image
imagestring($im, 5, 300, 300,  "I'm a pretty picture:))", $text_color);
//outputs the image as png
imagepng($im);
//frees any memory associated with the image 
imagedestroy($im);
?>
</body>
</html>