Use CH Elsewhere
posted on january 18, 2003, tag: plugin
One of the nice things about CurrentlyHearing is that you can take the information provided by the plugin and use it in unconventional ways. I've got two tutorials here for those of you that want to use CH for more than just showing information on your website.
Using PHP, you can utilize CH to create a dynamic image (GD Library required) and use it as a signature for forum systems, or use CH to display what you're hearing in your AIM profile. Read on!
The Dynamic Image
First of all, you have to have the GD Library installed and compiled for PHP. If you don't know what this is, ask your administrator. I'm not sure how common it is, but I have a feeling it's usually installed by hosts. For this tutorial, my server is running GD version 1.8.4. If you have a version higher than this, you can also use TTF fonts. For this, I'm using a GD built-in font to simplify things.
Create a file, and call it whatever you want. I'm using ch_image.php for this example. Here's what the contents should look like:
<?php
/**
* Create an image to display what
* you're CurrentlyHearing
**/function showSong($filename, $separator, $default) {
$file = $filename;
$open = @fopen($file, 'r');
$read = @fread($open, @filesize($file));
@fclose($open);
$song_info = ($read == '') ? ($song_info = $default) : ($song_info = $read);
$additional = $song_info;
if ($song_info != $default) {
$exp1 = explode('SONG:', $song_info);
$exp2 = explode(':SONG', $exp1[1]);
$song_info = $exp2[0];
if ($song_info != 'Nothing') {
$exp3 = explode(' - ', $song_info);
$artist = $exp3[0];
$title = $exp3[1];
$song_info = $artist . ' ' . $separator . ' ' . $title;
} else {
$song_info = $default;
}
}
return $song_info;
}// Be sure to change the song.txt location!
$content = 'CurrentlyHearing: ' . showSong('song.txt', '-', 'Nothing');
header("Content-type: image/png");$numchars = strlen($content);
$charHeight = ImageFontHeight(2);
$charWidth = ImageFontWidth(2);
$strWidth = $charWidth * $numchars;
$strHeight = $charHeight;$imgWidth = $strWidth + 10;
$imgHeight = $strHeight + 10;
$imgCenterX = $imgWidth / 2;
$imgCenterY = $imgHeight / 2;$im = ImageCreate($imgWidth, $imgHeight);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
$gray = ImageColorAllocate($im, 200, 200, 200);ImageFilledRectangle($im, 0, 0, $imgWidth, $imgHeight, $black);
ImageFilledRectangle($im, 1, 1, $imgWidth-2, $imgHeight-2, $white);
ImageFilledRectangle($im, 2, 2, $imgWidth-3, $imgHeight-3, $gray);$drawPosX = $imgCenterX - ($strWidth / 2) + 1;
$drawPosY = $imgCenterY - ($strHeight / 2);ImageString($im, 2, $drawPosX, $drawPosY, $content, $black);
ImagePng($im);
?>
Just make sure you change the function call to include the correct path to your song.txt file. Upload the file to your server. Then, to show the image, you can just use a plain IMG tag anywhere you want (even on other servers), just like this (replacing the URL with yours):
<img src="http://www.myserver.com/ch_image.php" border="0" alt="" />
Here's a live example:
The AIM Profile
Credit for this idea goes to Courtney.
Create a file, call it whatever you want. In this tutorial, I've called it ch_aim.php. Here's what the contents should look like:
<html>
<body bgcolor="#ffffff">
<font face="Verdana" size="1">
<?php
$ch_file = 'song.txt'; // relative or absolute path
$history = true; // do you want to list the history?
$tracks = 5; // number of tracks in the history
$ch = file($ch_file);
if (count($ch) > 1) {
echo "
" . preg_replace('/([:]?)SONG([:]?)/', '', $ch[0]) . '<br><br>';
if ($history) {
$max = 3 + $tracks;
for ($i = 3; $i < $max; $i++) {
$j = $i - 2;
echo "
<b>T</b>" . ($tracks + 1 - $j) . ': ';
$track = preg_replace('/([:]?)P' . $j . '([:]?)([
]?)/', '', $ch[$i]);
if (strlen($track) == 0) {
echo 'none';
} else {
echo $track;
}
echo '<br>';
}
}
} else {
echo 'Nothing<br>';
}
echo "<br>
";
echo '<a href="http://www.yourserver.com/ch_aim.php" target="_self">Refresh</a>? ';
echo "
" . 'Thanks in part to <a href="http://www.maniacalrage.net/plugin.php">Currently Hearing</a>';
?>
</font>
</body>
</html>
Upload this file to your server. Be sure that the refresh link in the code above is correct, and that the reference to song.txt is also correct. Once you've uploaded this file, edit your profile in AIM. To make this work, you need to link to it in your profile like this:
<a href="http://www.yourserver.com/ch_aim.php" target="_self">CurrentlyHearing</a>
Note that it's very important you have in there. It's the only way AIM will load a page in its info box.
There you have it. Two simple but fun ways to use CurrentlyHearing somewhere other than on your website. If you have any other interesting ideas, I would love to hear them!
Comments
There are 5 comments, comments are closed
Shawn on 01/19/2003:
I saw this in use in an AOL profile and was impressed (though it doesn't work in iChat, not your fault), though I was wondering if there was any way around needing to click for it to display. I suppose AIM can't automatically display a website?
Garrett on 01/19/2003:
Unfortunately, no. AIM's profile browser is very limited as to what it can process HTML wise. Meta-Refreshes don't work, and either does javascript. So you're pretty much stuck with having to click the link.
Mike on 07/23/2003:
Well, AIM just puts the profiles in the system memory when you sign in. I would like to see an automatic profile refresher.
Brian on 12/16/2003:
Actually, I had a friend test it out with iChat (and indirectly Safari). When you click on the link in the AIM profile, it loads up in their default web browser. Not really a bug; nor am I even questioning if there's a solution for it, however, it's not terribly difficult to incorporate some USER_AGENT detection to display the appropriate page. If the user's browser is Windows (obviously using the windows version of AIM) then display certain code to yield the code above. If the user's browser is Mac (or something else), have it display something different for them because when Mac users click on the link (in iChat) it loads in a standard web browser window. Just an idea...
Guss on 03/22/2004:
Need a little help because i cant get the aim profile php to work because it keeps giving me a quotation marks arent allowed to be used error