2013-02-20 64 views
0

我試圖使用GD將(shoutcast status)腳本的以下輸出寫入圖像,但它不起作用。我究竟做錯了什麼?將(shoutcast狀態)腳本的輸出寫入圖像

$string = "/home/test.txt"; 

使用上面只顯示文件的路徑,而不是它的內容。

輸出:

psytrance.value 37 
breaks.value 8 
dubstep.value 6 
reggae.value 130 
oldskool.value 5 
ambient.value 81 
test.value <- this should be ignored! 
complete.value 267 

PHP:

<?php 
header ("Content-type: image/png"); 
$string = "/home/test.txt"; 
// try changing this as well 
$font = 4; 
$width = imagefontwidth($font) * strlen($string) ; 
$height = imagefontheight($font) ; 
$im = imagecreatefrompng("/home/banner2.png"); 
$x = imagesx($im) - $width ; 
$y = imagesy($im) - $height; 
$backgroundColor = imagecolorallocate ($im, 255, 255, 255); 
$textColor = imagecolorallocate ($im, 0, 0,0); 
imagestring ($im, $font, $x, $y, $string, $textColor); 
imagepng($im); 
?> 
+1

看起來你已經知道了答案 - *是*,前提是你可以獲得shoutcast狀態。 – LSerni 2013-02-20 21:44:06

+0

我已經更新了我的問題以上... – 2013-02-20 22:51:31

+0

啊,我明白了。對不起,我應該想通了......好吧,你可以接受@ take的答案;-) – LSerni 2013-02-20 23:38:51

回答

1

的Shoutcast的狀態保存在test.txt?然後你必須將文件的內容寫入你的PNG。

$content = file_get_contents ($string); 
[...] 
$lines = explode("\n", $content); 
foreach ($lines as $line) { 
    if (strstr("test.value", $line) !== false) continue; 
    imagestring ($im, $font, $x, $y, $string, $textColor); 
    $y += 20; 
} 
+0

只有一個警告:不能保證'\ n'將成爲行分隔符。它可能只是'\ r \ n'。所以你可能想使用正則表達式來分割它,或者在'foreach'中發出'$ line = trim($ line)'作爲第一件事。 – LSerni 2013-02-20 23:41:03

+0

我的編程能力有限,你能告訴我整個腳本應該是什麼樣子嗎? – 2013-02-21 00:22:08

+0

無論如何,我真的很感謝你在這個謝謝你的時間,但我認爲我可能會更好地嘗試顯示數據作爲文本,所以我要去看看,而不是... – 2013-02-21 01:32:17