History
Initial Script by: radicand (nick»radicand)
Updated by: scappy (last.fm)
PHP Version
This code uses Version 5 of PHP, not a lot of webservers run this. I just thought I'd add this comment as I took this code and hacked it and tried to slap it on my website - Scappy - (site / last.fm)
Description
I wanted a way of incorporating my latest tracks into another site, and an image was going to be far easier than SSI'ing the page itself. So, here I present the php code I wrote to generate an image.
Note that this requires PHP with GD and SimpleXML compiled in. I tried screwing around with transparency, but I didn't want to spend the time to really get it working. Perhaps someone else can improve on this.
Additionally, this is currently designed to use the Verdana TTF font. I just copied it out of my Windows font directory to the webserver. As an alternative, use the commented-out string function instead.
Call the PHP script
<?php include("http://example.com/script.php?user=myUserName&display=4"); ?>
The PHP script
<?php
function MDashedLine($image, $x0, $y0, $x1, $y1, $fg, $bg)
{
$st = array($fg, $fg, $fg, $fg, $bg, $bg, $bg, $bg);
ImageSetStyle($image, $st);
ImageLine($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
}
$user = $_GET['user'];
$display = $_GET['display'];
$buffer = 10;
$spacing = 15;
// I'd move this line down, just in case $display
// is less than the number of tracks
// there is info for. -- Joe
// $h = ($display * $spacing) + $buffer;
$w = 300;
$xml = file_get_contents("http://ws.audioscrobbler.com/1.0/user/" . $user ."/recenttracks.xml");
$sxml = simplexml_load_string($xml);
$i=0;
foreach ($sxml as $track) {
$text[$i] .= $i+1 .". " . (string)$track->{"artist"} . " - " . (string)$track->{"name"};
if (++$i == $display) break;
}
// Something like this:
$h = ( count($text)* $spacing) + $buffer;
$im = @imagecreatetruecolor($w, $h) or die("Cannot Initialize new GD image stream");
$pink = imagecolorallocate($im, 233, 14, 91);
$grey = imagecolorallocate($im, 255, 127, 36);
$white = imageColorAllocate ($im, 255, 255, 255);
$black = imageColorAllocate ($im, 0,0,0);
$font = 2;
$fs = 8.5;
$th = 10.5;
$tc = $black;
$trclr = $grey;
$trans = imagecolortransparent($im,$trclr);
imagefill($im,0,0,$trans);
for ($i=0; $i<count($text); $i++) {
// imagestring($im, $font, 5, (($spacing * $i+1)+($buffer/2)), $text[$i], $tc);
ImageTTFText ($im, $fs, 0, ($buffer/2), (($spacing * $i+1)+($buffer/2))+$th, $tc, "verdana.ttf", $text[$i]);
}
MDashedLine($im,0,0,$w,0,$tc,$trans);
MDashedLine($im,$w-1,0,$w-1,$h,$tc,$trans);
MDashedLine($im,0,0,0,$h,$tc,$trans);
MDashedLine($im,0,$h-1,$w,$h-1,$tc,$trans);
if (function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($im);
} elseif (function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($im, "", 0.5);
} elseif (function_exists("imagepng")) {
header("Content-type: image/png");
imagepng($im);
} elseif (function_exists("imagewbmp")) {
header("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
} else {
die("No image support in this PHP server");
}
imagedestroy($im);
?>

