我有一個變量($output
),它被設置爲一個字符串。PHP標頭,內容類型:圖像不允許文字
要使用此字符串圖片我正在使用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" />
當然,這將是罰款,但我不知道怎麼會變成這樣解決我的因爲即使我知道這個圖像的來源(,我不知道 - 所以,這將是我的第一個問題,生成的圖像的路徑是什麼*)我不會改變這個事實,即標題在那裏,不讓我輸出文字。
那麼,我該如何解決這個問題?
在此先感謝!
*我想這將解決我的問題,因爲我能做到這一點對一個外部文件,而只是調用圖像(但也許不是,因爲我會做一個包括這將包括頭太),因爲你看到我有點困惑。對不起:)
UPDATE:
所以,我看到我的問題混淆了,所以我會添加一些更多的代碼,看是否能澄清我的問題有點多:
<?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腳本,但由於標題,我無法放入同一個文件。
好吧,我想我明白你要去哪裏。 – Trufa 2010-12-13 16:00:14
+1 - 你想放置圖像的位置需要''。因爲您正在使用'image/png'內容類型,所以它會將從腳本返回的任何內容視爲圖像。 – 2010-12-13 16:02:35
@Ben:的確如此。 – SLaks 2010-12-13 16:03:45