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

时间:2010-12-13 15:56:34

标签: php image header gd content-type

我有一个设置为字符串的变量($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。

所以我读了这个问题,asks if you can use two headers,你不能,但是the accepted answer建议,这样的话:<img src="my_img.php" />

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

那么,我将如何解决这个问题呢?

提前致谢!!


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


更新

所以我看到我的问题令人困惑,所以我会添加更多代码,看看这是否能更多地澄清我的问题:

<?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脚本,但由于标题我无法放入同一个文件

6 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:3)

Morbo说:“风车不会这样工作!晚安!”

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

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

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

只需将图像视为单独的页面即可。你会明白的。它只会在你的脑海中点击。其中一个重大的“啊哈”时刻。

答案 2 :(得分:1)

首先,路径。

From the manual

  

imagepng(resource $ 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分隔文件。

答案 3 :(得分:1)

您可以使用echo '<img src="data:image/png;base64,'. base64_encode(imagepng($im)) .'" />而非<img src="my_img.php">内联发送图片 不要设置内容类型标题!您的输出是text / html,您不必宣布,因为它是大多数服务器设置中的默认设置。

答案 4 :(得分:0)

HTTP无法正常工作。当您正在提供image / png数据时,就好像您正在从站点提供png文件,而不是带有图像的html文件。你想要完成什么?

答案 5 :(得分:0)

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

相关问题