writing APIC to mp3 file with getid3 (id3v2)

Utku Dalmaz picture Utku Dalmaz · Jan 30, 2011 · Viewed 7k times · Source

I am trying to write APIC picture to mp3 file with getid3. here is the code;

$cover = "/home/user/public_html/artwork/cover.jpg";
$TagData['attached_picture'][]=array(
'picturetypeid'=>2, // Cover. More: module.tag.id3v2.php -> function APICPictureTypeLookup
'description'=>'cover', // text field
'mime'=>'image/jpeg', // Mime type image
'data'=>$cover // Image data
);

but it doesnt work. image size is around 1.5 MB. should i resize it or sth ?

where am i wrong ?

Thanks

Answer

Adil Hashem picture Adil Hashem · Feb 5, 2011

Looking at the demo they have on their website: http://www.getid3.org/source/demo.write.phps

snippet of code:

$fd = fopen($_FILES['userfile']['tmp_name'], 'rb')
$APICdata = fread($fd, filesize($_FILES['userfile']['tmp_name']));
fclose ($fd);

$imagetypes = array(1=>'gif', 2=>'jpeg', 3=>'png');
if (isset($imagetypes[$APIC_imageTypeID])) {
    $TagData['attached_picture'][0]['data']          = $APICdata;
    $TagData['attached_picture'][0]['picturetypeid'] = $_POST['APICpictureType'];
    $TagData['attached_picture'][0]['description']   = $_FILES['userfile']['name'];
    $TagData['attached_picture'][0]['mime']          = 'image/'.$imagetypes[$APIC_imageTypeID];
}

Seems like the data key needs to be the image content, not just the path to the image file. So in your case, it should be something like:

$cover = "/home/user/public_html/artwork/cover.jpg";
$fd = fopen($cover, 'rb')
$APICdata = fread($fd, filesize($coverFile));
fclose ($fd);

$TagData['attached_picture'][]=array(
'picturetypeid'=>2, // Cover. More: module.tag.id3v2.php -> function APICPictureTypeLookup
'description'=>'cover', // text field
'mime'=>'image/jpeg', // Mime type image
'data'=>$APICdata  // Image data
);

Note: This is just after a quick glance at the demo code, I have not used this library or tested this code.