If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!
The service last.fm offers is pretty cool, you register, and it automatically records data about your music, and puts that data on their website. From there, you can find others who have the same taste as you or find artists similar to the ones you like. Audioscrobbler, which powers last.fm, offers an API, which makes it easy to get the latest tracks, artists, albums, and more. This data is offered in a variety of formats, text, XML, and XSPF (rarely). We’re going to be using plain text, as it’s easiest to manipulate. For a listing of all the API services, see here. We’re going to be using the API to display our last listened to track.
We will be using the API for latest tracks in combination with the GD functions to create a dynamic image with our latest song. The API is simple, we have a url which will look like: http://ws.audioscrobbler.com/1.0/user/USERNAME/recenttracks.txt and that contains the latest tracks for that user. The first part of our script will handle the API part:
<?php
header(“Content-type: image/png”);
$user = “RJ”;
$url = “http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.txt”;
$data = file($url);
$song = explode(“,”, $data[0]);
$song = explode(“-”, $song[1]);
The first part tells the browser that we will be sending out an image (png in our case), the next few lines make the request to the API, and then gets the latest song’s artist and title in an array ($song). The next part of the script will deal with creating, writing, and outputting our image. This is a little more confusing, just becuase the image functions often are.
$img = imagecreate(225, 30);
$bg = imagecolorallocate($img, 255, 255, 255);
$text = imagecolorallocate($img, 0, 0, 0);
imagestring($img, 2, 5, 5, trim($song[1]), $text);
imagepng($img);
imagedestroy($img);
?>
This creates a new image with the dimensions 225 by 30, makes white the background color, black the text color, writes our song and artist, then outputs it to the browser. The 2, 5, 5 are the font size, x position, and y position.
The final image is really simple, but it’s still cool. It’s easy to change the background color to fit the page it will be on, just get the RGB values and put them in for the $bg variable.
Enjoy!
Print This Post
Email This Post
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| By N2H | |||||
Comments RSS
TrackBack Identifier URI
You must be logged in to post a comment.