ETN.fm trance track as an image
I noticed that ETN.fm have now got forums! Huzzah! Obviously I registered straight away, 94th member 😉
Well anyway, a few people have requested a signature strip that contains the current track being played. I don’t know how amb_ on the forum has done it but here’s my attempt. Basically you create yourself a template image (468×60) in JPEG or PNG format (I did do one in GIF BUT when the text was rendered onto it, it was kinda 50% transparent so that was useless, using PNG or JPEG overcomes this problem) which looks a little like… this:
You can choose the text colour using HEX format, the script goes away and retrieves the current track by getting the ShoutCast webpage on ukserv.etn.fm, parses it up and spits out the trackname to another function. That function writes it onto the bottom left of the image and then outputs it as a PNG image. GIF format can GIF off. It’s noteworthy that I used a font that I downloaded from http://04.jp.org/ so many thanks go to Yuji Oshimoto. I haven’t provided the font I used in the zip package for obvious reasons in that it belongs to him, just go get it from his site instead. 🙂
The PHP script below gives you this…
Well actually it gives you a reference to a PHP file which will probably be ignored by most forums as a signature image. Most will give you [img]http://www.domain.com/etnplay.php[/img] instead of the actual image. So, for the purposes, I’ve had to use IIS_Rewrite since (I’m using IIS) as opposed to mod_rewrite for Apache. Further details are in the comment I’ve added.
I’ve no doubt gone a long way round to retrieve the trackname but still.. it’s functional.
Get the source for this script + images from here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
function fetchURL( $url ) { $url_parsed = parse_url($url); $host = $url_parsed["host"]; $port = $url_parsed["port"]; if ($port==0) $port = 80; $path = $url_parsed["path"]; //if url is http://example.com without final "/" //I was getting a 400 error if (empty($path)) $path="/"; //redirection if url is in wrong format if (empty($host)): $host="www.somesite.com"; $path="/404.shtml"; endif; if ($url_parsed["query"] != "") $path .= "?".$url_parsed["query"]; $out = "GET $path HTTP/1.0\r\nHost: $host\r\nUser-Agent: Mozilla 4.0\r\n\r\n"; @$fp = fsockopen($host, $port, $errno, $errstr, 2); if (!$fp) { return 'Error connecting...'; } fwrite($fp, $out); $body = false; while (!feof($fp)) { $body .= fgets($fp, 128); } fclose($fp); return $body; } function getShoutcastTrack($url) { $content = fetchURL($url); if ($content == 'Error connecting...') { return 'ETN.fm (track unavailable)'; } $content = explode('Current Song:', strip_tags($content)); $song = explode('Written by Stephen', $content[1]); $track = trim($song[0]); return $track; } function &hex2rgb($hex, $asString = true) { // strip off any leading # if (0 === strpos($hex, '#')) { $hex = substr($hex, 1); } else if (0 === strpos($hex, '&H')) { $hex = substr($hex, 2); } // break into hex 3-tuple $cutpoint = ceil(strlen($hex) / 2)-1; $rgb = explode(':', wordwrap($hex, $cutpoint, ':', $cutpoint), 3); // convert each tuple to decimal $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0); $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0); $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0); /*return ($asString ? "{$rgb[0]}, {$rgb[1]}, {$rgb[2]}" : $rgb);*/ return $rgb; } function trackSig($trancetrack, $progtrack, $text_col="000000") { //putenv('GDFONTPATH=C:\WINNT\Fonts'); // Fontpath $font='04b2.ttf'; $ang='0'; $font_size='6'; $tC = hex2rgb($text_col); if ($trancetrack == '') { $trancetrack = "ETN.fm (track unavailable)"; } if ($progtrack == '') { $progtrack = "ETN.fm (track unavailable)"; } if (strlen($trancetrack) > 75) { $trancetrack = substr($trancetrack, 0, 75)."..."; } if (strlen($progtrack) > 75) { $progtrack = substr($progtrack, 0, 75)."..."; } $trancetrack = strtoupper($trancetrack); $progtrack = strtoupper($progtrack); // Create the image from the template $img = imagecreatefrompng('etn.png'); $width = imagesx($img); $height = imagesy($img); // Colours to be used for text $text_col = imagecolorallocate($img, $tC[0], $tC[1], $tC[2]); // Write the text onto the image imagettftext($img, $font_size, $ang, 61, $height-10, $text_col, $font, $trancetrack); imagettftext($img, $font_size, $ang, 61, $height-3, -$text_col, $font, $progtrack); // Output the image header("Content-Type: image/png"); imagepng($img); imagedestroy($img); } $trancetrack = getShoutcastTrack('http://ukserv.etn.fm:8000'); $progtrack = getShoutcastTrack('http://toronto.etn.fm:8210/'); trackSig($trancetrack, $progtrack, "FFFFFF"); |
Well it proved a bit of a pain in the butt to actually get it working as a “signature” on etn.fm’s forums but I finally managed it. With etn’s forum, if it doesn’t see an image request as any one of png, jpg, jpeg, gif etc then it just wraps the link you give it and forgets about it.
In order to overcome this I had to install IIS_Rewrite which I downloaded from http://www.helicontech.com/
With the addition of a simple enough rule to the httpd.ini:
RewriteRule /etnplay\.png /etnplay.php
I was able to trick etn’s forum into believing it was an actual PNG image and display it. Funny what you can do these days innit?
Heya dude, wonderful lookin script you got going here at the moment, im really interested in trying to use this on a site rather than a forum, just wondering how i might go about doing that?
If you could email me at all or add me to msn (email is my msn) so we could talk about it that would be great!
Hope to hear from you soon.
Rich. aka Tricks