如何从MP3 ID3标签中提取专辑封面?

4

我可以使用php从我的mp3应用程序中成功获取mp3 ID3标签。但是,我得到了一个巨大的BLOB数据。我相信这很简单,但我只需要一点方向上的帮助。我想最终得到的是(BLOB数据已更正):

$media_year = implode($ThisFileInfo['comments_html']['year']);
$media_genre = implode($ThisFileInfo['comments_html']['genre']);
$media_jpg = [BLOBDATA];

( 我知道这句话在编程上没有意义,但是你可以理解我的意思。前两行对我来说很好用,但我不知道如何从ID3标签中获取BLOBDATA并将其转换为JPG以便我能够显示。)

在另一个教程中我看到了这样的内容,但它没有让我明白:

[comments] => Array
    (
        [picture] => Array
            (
                [0] => Array
                    (
                        [data] => <binary data>
                        [image_mime] => image/jpeg
                    )

            )

    )

很抱歉要求读者“替我完成”,但我已经尽力寻找各种资源。非常感谢您的帮助。

3个回答

12

我使用getid3库,这个工作得很好。

  <?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">

我在这段代码中使用 base64_encode 将图片嵌入到HTML中,你可以看到


我刚找到了这个。请忽略我的问题,非常感谢 StackOverflow!!! http://stackoverflow.com/questions/10483781/does-anyone-know-how-to-convert-binary-data-stored-into-a-database-back-into-an - Ted Robonson
Fillay知道我需要包含getId3文件夹中的所有文件才能使其正常工作。现在它已经可以使用了。非常感谢。 - yourkishore

2

您可以使用file_put_contents()函数将ID3图像数据保存到文件中。

 $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'];


这不会起作用,因为file_put_contents需要第一个参数以字符串格式而不是blob格式。你需要使用fwrite来完成它。 - Cemal

0
我找到了一篇文章,解释了ID3标签的结构。
http://www.stagetwo.eu/article/19-MP3-ID3-Tags-Wie-kann-man-die-teile-lesen 这应该让你能够理解和解析它们。
虽然是德语,但也许关于ID3标签结构的文章也可以用Google翻译阅读。
我喜欢这些重要字节的描述,例如SyncSafe整数的解释。
针对您的问题,它描述了APIC(图像)框架的结构。
当您解析了这些结构并获得了字节流后,只需使用imagecreatefromstring($byteStream),现在您就可以从MP3中获得您的图像资源啦 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接