Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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

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标题,内容类型:图像不允许文本_Php_Image_Header_Gd_Content Type - Fatal编程技术网

PHP标题,内容类型:图像不允许文本

PHP标题,内容类型:图像不允许文本,php,image,header,gd,content-type,Php,Image,Header,Gd,Content Type,我有一个设置为字符串的变量($output) 我正在使用一个图像来表示这个字符串 特别是我正在使用的函数,只做了很小的修改: <?php // Create a 100*30 image $im = imagecreate(100, 30); // White background and blue text $bg = imagecolorallocate($im, 255, 255, 255); $textcolor = imagecolorallocate($im, 0, 0, 2

我有一个设置为字符串的变量(
$output

我正在使用一个图像来表示这个字符串

特别是我正在使用的函数,只做了很小的修改:

<?php
// Create a 100*30 image
$im = imagecreate(100, 30);

// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write the string at the top left
imagestring($im, 5, 0, 0, $output, $textcolor); #here is my string $output

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>
在那之后,不允许我输出任何html

所以我读了这个问题,你不能,但建议这样:

这当然没问题,但我不知道这将如何解决我的问题,因为即使我知道这个图像的来源(,我不知道-所以,这将是我的第一个问题,生成图像的路径是什么*),我也不会改变这样的事实,即标题在那里,不允许我输出文本

那么,我想你会如何处理这个问题

提前谢谢


*我想这会解决我的问题,因为我可以在一个外部文件上这样做,只需调用图像(但可能不会,因为我必须执行include,这也会包括头部),正如您看到的,我有点困惑。对不起:)


更新:

因此,我发现我的问题令人困惑,因此我将添加更多代码,看看这是否能进一步澄清我的问题:

<?php

$x = mt_rand(1,5);
$y = mt_rand(1,5);

function add($x, $y) { return $x + $y; }
function subtract($x, $y) { return $x - $y; }
function multiply($x, $y) { return $x * $y; }

$operators = array(
    'add',
    'subtract', 
    'multiply'
    );

$rdno = $operators[array_rand($operators)];

$result = call_user_func_array($rdno, array($x, $y));
session_start();
$_SESSION['res'] = $result;

if ($rdno == "add") {
    $whato = "+";
}elseif ($rdno == "subtract") {
    $whato = "-";
} else {
    $whato = "*";
}
$output = $x . $whato . $y . " = ";
?>
<form name="input" action="check.php" method="post">
<input type="text" name="result" />
<input type="submit" value="Check" />
</form>


我希望
$output
成为一个图像
,因此我尝试使用上面的PHP:GD脚本,但由于标题的原因,我无法将put放入同一个文件。

您需要制作一个单独的PHP脚本来为图像服务,然后制作一个指向该脚本的
标记


您可以使用图像URL中的查询字符串向脚本发送信息。

HTTP不能以这种方式工作。当您提供图像/png数据时,就好像您提供的是来自站点的png文件,而不是包含图像的html文件。您想实现什么?

my_img.php不是您图像的源代码。它是生成该图像的脚本的源,或者从文件中读取图像并直接输出到浏览器中。显然,使用img src的方法是最好的,因为您将把显示图像字符串的所有“枯燥”实现隐藏在浏览器的机制后面。

首先是路径

:

imagepng(资源$image[,字符串$filename[,int$quality[,int$filters]]))

这意味着,如果不提供第二个参数(这里就是这种情况),则没有文件的路径,只有文件/图像资源的数据。感谢您提供的标题,php文件将被浏览器理解为png文件

第二:

在您的页面(ie index.php)中,您可以这样添加

<img src="myimg.php?output=[...]" />

在php脚本myimg.php中,您的脚本如下所示:

<?php
$output = $_GET['output'] // getting your text

// Create a 100*30 image
$im = imagecreate(100, 30);

// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write the string at the top left
imagestring($im, 5, 0, 0, $output, $textcolor); #here is my string $output

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>


2分离文件。

您可以使用
echo'
而不是
内联发送图像
不要设置内容类型标题!您的输出是text/html,您无需宣布,因为这是大多数服务器设置中的默认设置。

Morbo说:“风车不是那样工作的!晚安!”

这意味着你需要专注于这一切是如何运作的。这个问题表明你对你的工具有一个基本的误解。HTML不能包含图像,它包含指向图像的链接。因此,您的脚本需要通过图像标记从另一个页面包含。(或CSS)

然而,这一切都是一件好事。这意味着该脚本可以具有动态元素,这些元素在每次调用时生成新图像。或者它可以通过密码保护您的图像,这样只有登录的用户才能看到它

有很多方法可以使用它。一旦你意识到图像就像是php的一个页面,这就打开了新的途径。Php可以输出任何东西。Word文档、excel、pdf、css甚至JS。所有这些都可以用来做很酷的事情


只需将图像看作一个单独的页面。你会明白的。它只会在你的脑海中一闪而过。其中一个重要的“啊哈”时刻。

好的,我想我知道你要去哪里了。+1-你想把图像放在哪里需要
。因为您提供的是
image/png
内容类型,所以它会将脚本返回的任何内容都视为图像。@SLaks问题是我正在动态生成此
$output
,因此我不知道如何将其放在另一个文件m上,因为该过程将生成变量,我想通过一个会话将其发送到执行PHP:GD的文件,然后获取源代码。这有点让人困惑,我在解释我自己吗?谢谢@Akinator:您需要将代码移动到一个单独的文件中。你可以在querystring中传递它需要的任何信息。我并不是说我可能不会对一些基本概念感到困惑,但在这种情况下,我认为表达自己的问题比这更大。我知道html不能包含图像:)所以在你的问题中,它是一个带有my_img.php的标记。该文件包含图像脚本。a标记所在的文件是页面本身。同意,但我不知道如何链接到它,因为当我必须调用它时,我会“尚未创建”,或者,请看一下我的更新。您调用的PHP脚本将动态创建图像。不把它保存在任何地方。
<?php
$output = $_GET['output'] // getting your text

// Create a 100*30 image
$im = imagecreate(100, 30);

// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write the string at the top left
imagestring($im, 5, 0, 0, $output, $textcolor); #here is my string $output

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>