How can I extract the album art from an MP3 ID3 tag?

☆樱花仙子☆ 提交于 2019-12-04 12:41:23

i use getid3 library for this work fine like

  <?php
  $Path="mp3 file path";
  $getID3 = new getID3;
  $OldThisFileInfo = $getID3->analyze($Path);
  if(isset($OldThisFileInfo['comments']['picture'][0])){
     $Image='data:'.$OldThisFileInfo['comments']['picture'][0]['image_mime'].';charset=utf-8;base64,'.base64_encode($OldThisFileInfo['comments']['picture'][0]['data']);
  }
  ?>
  <img id="FileImage" width="150" src="<?php echo @$Image;?>" height="150">

in this code i embedded image in to HTML with base64_encode that you can see

you can use file_put_contents() to save the id3 image data to a file.

 $path = "/Applications/MAMP/htdocs/site/example.mp3"; //example. local file path to mp3
  $getID3 = new getID3;
  $tags = $getID3->analyze($Path);

if (isset($tags['comments']['picture']['0']['data'])) {
    $image = $tags['comments']['picture']['0']['data'];
    if(file_put_contents(FCPATH . "imageTest.jpg", $image)) {
        echo 'Image Added';
    } else {
        echo 'Image Not Added';
    }
}

or you can display the image in place using

header('Content-Type: image/jpeg');
echo $tags['comments']['picture']['0']['data'];

I found an Article which explains the Structure of ID3 Tags. http://www.stagetwo.eu/article/19-MP3-ID3-Tags-Wie-kann-man-die-teile-lesen This should make you able to understand and to parse them. It's in German but maybe the article about the ID3 Tags Structure is also readable with google translator. I like the description of the important bytes and for example the explanation about the SyncSafe integer.

To your Problem it describes how the APIC (Image) Frame is structured. When you parsed these Structure and you got the bytestream just use imagecreatefromstring($byteStream) Now you have your image resource out of the MP3 :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!