【发布时间】:2011-05-24 18:15:37
【问题描述】:
我有一个设置为字符串的变量 ($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 脚本,但由于标题的原因,我无法放入同一个文件中。
【问题讨论】:
标签: php image header gd content-type