PHP class mp3Data – Get ID3 Info from MP3
So recently, I submitted a bid on a project to make a lyrics website using PHP. I haven’t been contacted about whether or not I won the bid yet, but I decided that I might as well start some preliminary work on backend coding – It’ll make my life easier, and deliver a faster and better result to the client, so why not?
Why am I mentioning this, you ask? What does my personal life have to do with my tech blog? Well, I’m getting to that!
I almost immediately thought. “Hey, wouldn’t it be really cool if the admin of the website could simply upload an MP3 file and pull almost, if not all of the data from it to use on the site?” After which, I quickly searched around for something that would allow me to do just that, to read the ID3 tags on an MP3 file.
The first thing I came to, was the search result on the PHP website for “id3,” the ID3 PECL Library. Unfortunately, after trying for a very long time on DreamHost, I not only couldn’t get it to install, but had just wasted probably an hour in an attempt to. Not only that, but the library was still in its alpha stage.
Some quick internet searching took me to the getid3 library, a robust library programmed to read ID3 data tags (and other types of metadata?) from almost any type of file. I quickly set about to using it, and found its internal structure complicated and confusing. It didn’t even offer me a quick and easy way to access the album artwork.
After a good period of time learning some of the basic structure of ID3 and how the getid3 library worked, I set about to coding up a library of my own to quickly interface with getid3 and retrieve the information I needed.
The result is class::mp3Data. A very, very simple library built exclusively to interface with getid3 and retrieve basic information about an mp3 file while still retaining more advanced information for coders who need it. Take a look at some example code and the licensing permissions after the break.

Retrieving and Displaying the Artwork as a Picture:
1: <?php
2: require_once("mp3Data.class.php");
3: $data = new mp3Data("filename.mp3");
4: $img = $data->getArt();
5: if ($img)
6: {
7: header("Content-type: image/jpeg");
8: imagejpeg($img);
9: }
10: ?>
Displaying Basic Data:
1: <?php
2: require_once("mp3Data.class.php");
3: $data = new mp3Data("filename.mp3");
4: print("Title: ".$data->getName()."<br />");
5: print("Album: ".$data->getAlbum()."<br />");
6: print("Artist: ".$data->getArtist());
7: ?>
You can copy the source code from pastebin at this link.
Licensing Permissions:
Pingback: Navarr's Tech Side » Small Update: class::mp3Data v0.2