Helping ordinary people create extraordinary websites!
$1 CPM Advertising For A Limited Time Only
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

PHP Dynamic Signature: Now Playing (Winamp)

By Joe S
2005-08-16


PHP Dynamic Signature: Now Playing (Winamp)

In this tutorial I will teach you how to make a dynamic image using PHP.

The image will have text saying the last two songs you listened to (if you are currently listening) or the last song you listened to (if you aren't currently listening).

In order to get this info, you will have to be using Winamp, and this plugin.

Once you've installed the plugin you have to set up a template that will upload every time you change the song.
For this tutorial the template you should use it the following:
[np:IsPlaying]Now[/np:IsPlaying][np:NotPlaying]Last[/np:NotPlaying]
A1:[np:Artist1]:A1
T1:[np:Title1]:T1
A2:[np:Artist2]:A2
T2:[np:Title2]:T2

Just save that as a .html file in your /Winamp/np_templates folder.

Then go to the plugin preferences (In Winamp, Ctrl + P > Plug-ins > General Purpose > Now Playing plug-in vN.N > Configure > HTML Settings tab) select your template name from the dropdown box and check 'Use template.'

Then, go to the FTP tab and fill in your FTP info.
Set the filename to 'data.txt' (without quotes)

Now, whenever your song changes, a file will be uploaded to your server (to the path specified) with the following format:
'Now' or 'Last' - Depending on whether or not you are listening now.
A1:Artist 1:A1
T1:Title 1:T1
A2:Artits 2:A2
T2:Title 2:T2

Now we need the PHP code to parse this file, and write the data to an image.

<?php

    
// Cut Function
    
function cut($str,$len) {
        if (
strlen($str) > $len) {
            return
substr($str,0,$len).'...';
        } else {
            return
$str;
        }
    }
    
// EO :: Cut Function
    
    // Create The Image

    
header("Content-type: image/png");
    
$bg = "bg.png";

    
$im = @ImageCreateFromPNG($bg);
    
    
// EO :: Create Image

    // NP

    
$file = file("data.txt");
    if(!
$file)
    {
        
$now = "Error.";
    }
    if (
trim($file[0]) == "Last")
    {
        
$now = "Not Listening To Winamp.";
        
$a1        = explode("A1:", $file[1]);
        
$a2        = explode(":A1", $a1[1]);
        
$a         = $a2[0];
    
        
$t1        = explode("T1:", $file[2]);
        
$t2        = explode(":T1", $t1[1]);
        
$t         = $t2[0];
        
        
$last = "Last Played: ".$a." - ".$t;
    }
    
    if (
trim($file[0]) == "Now")
    {
    
        
$a1        = explode("A1:", $file[1]);
        
$a2        = explode(":A1", $a1[1]);
        
$a         = $a2[0];
        
        
$t1        = explode("T1:", $file[2]);
        
$t2        = explode(":T1", $t1[1]);
        
$t         = $t2[0];
        
        
$now  = "Now Playing: ".$a." - ".$t;

        
$a3        = explode("A2:", $file[3]);
        
$a4        = explode(":A2", $a3[1]);
        
$la        = $a4[0];
        
        
$t3        = explode("T2:", $file[4]);
        
$t4        = explode(":T2", $t3[1]);
        
$lt        = $t4[0];
        
        if (
$la == "" || $lt == "" || $la == " " || $lt == " ")
            
$last = "Last Played: No data.";
        else
            
$last = "Last Played: ".$la." - ".$lt;
        
    }
    
    
// EO :: NP

    
$width = 50;
    
$font  = 2;
    
$white = imagecolorallocate($im, 255, 255, 255);
    
$black = imagecolorallocate($im, 0, 0, 0);
    
    
imagestring($im, $font, 10, 6, cut($now, $width), $white);
    
imagestring($im, $font, 10, 6, cut($last, $width), $white);
    
    
ImagePNG($im);
    
ImageDestroy($im);
    

?>



Now for the explanation:
The first few lines are a cut function that cuts a string to the specified length. We will use this for chopping off text that is longer than the length of the image.

Then we actually create the image from a background file that you have.
After that we parse the data.txt file that has all of the song info:
1. Check to see if you are listening now, or not.
2. Extract the Artists and Titles.

Now we will have two variables set – one for current, and one for last.

All we have to do now is write the strings to the image.

$width will be the width of the text (anything more will get turnicated)

$font will be the size of the font.

Not much more to explain, the rest is self explanitory.
Check this out to see what I've done with this.

Tutorial Pages:
» PHP Dynamic Signature: Now Playing (Winamp)


© 2005 Joey Shamah


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.