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

发布于 2024-10-07 20:30:11 字数 2476 浏览 10 评论 0原文

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

为了将此字符串转换为图像,我正在使用 PHP: GD Library

特别是我正在使用的 imagestring() 函数一个非常小的修改:

<?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);
?>

这按预期工作:它将 $output 转换为图像。

所以,我的问题是(或者至少是我最好的猜测):

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

这之后不允许我输出任何 html。

所以我读了这个问题,其中 询问您是否可以使用两个标头,但您不能,但是接受的答案建议,如下所示:

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

那么,我将如何处理这个问题?

提前致谢!!


*我想这会解决我的问题,因为我可以在外部文件上执行此操作,并且只需调用图像(但可能不是,因为我必须执行包含操作,这也将包含标头),正如您所见有点困惑。抱歉:)


更新:

所以我发现我的问题很混乱,所以我将添加更多代码来看看这是否能进一步澄清我的问题:

<?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 脚本,但由于标头的原因,我无法将其放入同一个文件中。

I have a variable ($output) that is set to a string.

To mekt this string an image I'm using the PHP: GD Library.

In particular the imagestring() function, which I'm using with a very slight modification:

<?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);
?>

This is working as expected: It turns $output into an image.

So, my problem is (or my best guess at least):

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

Which, doesn't allow me to output any html after that.

So I read this question, which asks if you can use two headers, which you can't, but the accepted answer recommends, something like this: <img src="my_img.php" />

This of course would be fine, except I don't know how would this solve my issues since, since even if I knew the source to this image (which I don't - so, that would be my first question, what's the path to the generated image*) I't would change the fact that the header is there and is not letting me output text.

So, I how would you approach this issue?

Thanks in advance!!


*I guess that would solve my issues since I could do this on an external file, and the just call the image (but maybe not since I would have to do an include and this would include the header too) as you see I'm a bit confused. Sorry :)


UPDATE:

So I'm seeing my question was confusing so I will add some more code to see if this clarifies my problem a little bit more:

<?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>

I want $output to be a image so this got me trying to use the PHP:GD script above, but I can't make put in the same file because of the header.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

君勿笑 2024-10-14 20:30:11

您需要创建一个单独的 PHP 脚本来提供图像,然后创建一个指向该脚本的 标记。

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

You need to make a separate PHP script which serves the image, then make an <img> tag that points to this script.

You can send information to the script using the querystring in the image URL.

拥醉 2024-10-14 20:30:11

莫尔博说:“风车不是这样工作的!晚安!”

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

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

有很多方法可以使用它。一旦您意识到图像就像 php 页面一样,这就会打开新的路线。 PHP可以输出任何东西。 Word 文档、Excel、pdf、css,甚至 JS。所有这些都可以用来做很酷的事情。

只需将图像视为一个单独的页面即可。你会明白的。它就会在你的脑海中卡入到位。那些重大的“啊哈”时刻之一。

Morbo says: "Windmills do not work that way! Goodnight!"

Which means that you need to bone up on how this all works. This issue points to you having a basic misunderstanding of your tools. HTML can't contain images, it contains links to images. So your script needs to be included from another page via an image tag. (or CSS)

However, all this is a good thing. It means that this script can have dynamic elements that produce a new image each time it is called. Or that it could password-protect your images so only logged-in users can see it.

There are a host of ways to use this. And once you realize the image is like a page to php, this opens new routes. Php can output anything. Word docs, excel, pdf, css, even JS. all of which can be used to do cool things.

Just think of the image as a separate page. You'll get it. It'll just click into place in your mind. One of those big 'aha' moments.

心病无药医 2024-10-14 20:30:11

首先,路径。

来自手册

imagepng (资源 $image [, string $filename [, int $quality [, int $filters ]]] )

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

第二:

在你的页面(即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个分隔文件。

First, the path.

From the manual :

imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )

It means that if you don't give a second argument (it is the case here), you don't have the path to a file but the data of the file / image ressource. The php file will be understand as a png file by the browser thanks to the header you give.

Second :

In your page (ie index.php), you could add this like that

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

and in your php script myimg.php you have it like this :

<?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 separates files.

小嗲 2024-10-14 20:30:11

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

You can send the image inline with echo '<img src="data:image/png;base64,'. base64_encode(imagepng($im)) .'" /> instead of <img src="my_img.php">.
Do not set a content-type header! Your output is text/html what you don't have to announce as it's the default in most server setups.

何处潇湘 2024-10-14 20:30:11

HTTP 不是这样工作的。当您提供图像/png 数据时,就好像您正在从网站提供 png 文件,而不是包含图像的 html 文件。你想实现什么目标?

HTTP doesn't work that way. When your are serving image/png data, it as if you are serving a png file from the site, not an html file with an image in it. What are you trying to accomplish?

网名女生简单气质 2024-10-14 20:30:11

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

my_img.php is not source to your image. It is a source to the script generating that image, or reading it from file and outputting directly into browser. Obviously the approach with img src would be the best, because you'll hide all "boring" implementation of displaying image string behind browser's mechanisms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文