0

I'm trying to write the following output of a (shoutcast status) script to an image using GD but it's not working. What am I doing wrong?

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

Using the above just displays the path to the file not it's content.

output:

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);
?>
Fat Finger
  • 59
  • 1
  • 2
  • 7

1 Answers1

1

The shoutcast Status is saved in test.txt? Then you have to write the content of the file into your 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;
}
take
  • 2,202
  • 2
  • 19
  • 36
  • Only one caveat: it is not guaranteed that `\n` will be the line separator. It might just be `\r\n`. So you might want to split it using a regexp, or issue a `$line = trim($line)` as first thing inside the `foreach`. – LSerni Feb 20 '13 at 23:41
  • My programing skills are somewhat limited, could you show me what the the whole script should look like? – Fat Finger Feb 21 '13 at 00:22
  • Anyway I genuinely appreciate your time on this so thanks but I think I might be better off trying to display the data as text so I'm going to look into that instead... – Fat Finger Feb 21 '13 at 01:32