[Tutorial] How to create a audio signature

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi all,

To create your audio signature, follow these instructions:


Requirements:



1. Create a file called index.php and open it in notepad.



2. Paste the following code into that file and save it. Make sure you change the Last.fm username to yours!! - See yellow text in the code box below

Code:
<?php

$textfile = "tracks.txt";

if(filemtime($textfile) + (3 * 60) < time())
{

$fh1 = fopen($textfile, 'w+') or die("can't open file");
fwrite($fh1, "");
fclose($fh1);

class RSSParser {

	var $insideitem = false;
	var $tag = "";
	var $title = "";

	function startElement($parser, $tagName, $attrs) {
		if ($this->insideitem) {
			$this->tag = $tagName;
		} elseif ($tagName == "ITEM") {
			$this->insideitem = true;
		}
	}

	function endElement($parser, $tagName) {
		if ($tagName == "ITEM") {
			$textfile = "tracks.txt";
			$fh = fopen($textfile, 'a+') or die("can't open file");
			$tracks = sprintf("%s\n",(trim($this->title)));
			fwrite($fh, $tracks);
			fclose($fh);
			$this->title = "";
			$this->insideitem = false;
		}
	}

	function characterData($parser, $data) {
		if ($this->insideitem) {
		switch ($this->tag) {
			case "TITLE":
			$this->title .= $data;
			break;
			case "DESCRIPTION":
			$this->description .= $data;
			break;
			case "LINK":
			$this->link .= $data;
			break;
		}
		}
	}
}


$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://ws.audioscrobbler.com/1.0/user/[color=yellow][b]Last.fm Account Username Here[/b][/color]/recenttracks.rss","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

}

if ($slogans = @file("tracks.txt"))
{
$slogan = $slogans[0];
} else {
$slogan = "No Recent Music";
}

function imagestrokedtext($im, $size, $angle, $x, $y, $tcolour, $scolour, $font, $text) {

imagettftext($im, $size, $angle, $x + 1, $y, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x + 1, $y + 1, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x + 1, $y - 1, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x, $y, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x, $y + 1, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x, $y - 1, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x - 1, $y, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x - 1, $y + 1, $scolour, $font, $text);
imagettftext($im, $size, $angle, $x - 1, $y - 1, $scolour, $font, $text);

imagettftext($im, $size, $angle, $x, $y, $tcolour, $font, $text);

}

$im = imagecreatefromjpeg('background.jpg');

$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$font = "font.TTF";

imagestrokedtext($im, 7, 0, 57, 48, $white, $black, $font, strtoupper($slogan));

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: image/jpeg");
imagejpeg($im,"",99);
imagedestroy($im);

?>



3. Create a file called tracks.txt and open it in notepad.



4. Paste the following code into that file and save it.

Code:
Audio Signature



5. Upload the background image you downloaded earlier (background.jpg), the font (font.TTF), the index.php file and the tracks.txt file into the same directory on you PHP enabled webhost/webserver. You should see something like the following when viewing the directory:





6. Now that they are uploaded have the direct url to the index.php file handy and ready to type, including the BB IMG tags (see below). For instance the url could be, including the IMG BB tags:

direct url to index.php said:
58208cf92d_test5435.png

7. Go to edit your signature and type the direct url to index.php code that you got in the previous section. You should see something similar to the following:





8. Click on preview to check it works, and you would see something similar to below:





9. If you see something similar then you have successfully added a audio signature and you can click save. If not you must have done something wrong whilst following instructions. If you have followed the instructions correctly, you will end up with a signature similar to the following:

index.php




Please Note: My PHP skills are pretty bad so bare that in mind. Also make sure you change the account details in the PHP file.


Hope this helps
 
Last edited:
I didn't know about that, thanks... This should make the script a whole lot quicker if I use that. :)

EDIT: I've done it now, and it works great. A lot faster and less than 50 lines of PHP. :)
 
Last edited:
Kudos to the OP, Conrad11 for posting his excellent code :)
MikeHunt79 said:
I hope you don't mind, but I modded this a bit so it has the last 7 songs played. :)
Ditto, I also modded it slightly so it uses the .txt file rather than the rss feed, also you can custom specify the username and number of tracks listed:

http://www.mydomain.co.uk/music.php?user=riddlermarc&num=8

music.php


I also stuck in some basic error checking to make sure a valid .txt file was returned..

Code:
<?php
$username = $_GET["user"];
$numlines = $_GET["num"];
$lines = file('http://ws.audioscrobbler.com/1.0/user/'.$username.'/recenttracks.txt');
$fontsize = 6;
$cnt = 1;
$x = 30;
$y = $fontsize + 3;
$im = imagecreatefromjpeg('background.jpg');
$white = imagecolorallocate($im, 255, 255, 255);
$font = "font.TTF";
if ($lines) {
  foreach ($lines as $line_num => $line) {
    if($cnt <= $numlines)
    {
      $pieces = explode(",", $line);
      imagettftext($im, $fontsize, 0 , $x+1, $y+1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x+1, $y, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x+1, $y-1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x, $y+1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x, $y, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x, $y-1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x-1, $y+1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x-1, $y, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x-1, $y-1, black, $font, strtoupper(rtrim($pieces[1])));
      imagettftext($im, $fontsize, 0 , $x, $y, $white, $font, strtoupper(rtrim($pieces[1])));
      $y=$y + $fontsize + 2;
    }
    $cnt++;
  }
}
else {
  imagettftext($im, $fontsize, 0 , $x, $y, $white, $font, "no data for $username..");
}  
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: image/jpeg");
imagejpeg($im,"",100);
imagedestroy($im);
?>
 
Last edited:
Yeah edit to your hearts content, it was complied with other free (i hope) scripts so do as you wish.

I didnt realise they automatically create txt files so it would probably be best to read of those as said.
 
Although not very beneficial to the script if used as written, I have a text border function I wrote for my sig system that may be useful if people wanted to play around with the thickness of the text border so I thought I may aswell post it in here for what it's worth:

Code:
//	Border function - creates a text border on $canvas
function border($canvas, $size, $angle, $x, $y, $colour, $font, $string, $width){
	for($i1 = $x - $width; $i1 <= $x + $width; $i1++)	{
		for($i2 = $y - $width; $i2 <= $y + $width; $i2++)	{
			imagettftext($canvas, $size, $angle, $i1, $i2, $colour, $font, $string);
		}
	}
}
null :)
 
Back
Top Bottom