使用 getID3 在浏览器中显示图像
Display image in browser with getID3
我想创建一个 php 文件,当作为 html 的 img 标签中的 src 参数调用时,该文件将 return mp3 歌曲的图像。像这样:
<img src="/imageReturn.php?song=SongName">
为了从 mp3 文件中获取图像,我使用了 getID3 库。我的 PHP 代码:
$song= $_GET["song"];
require_once("getID3-1.9.15/getid3/getid3.php");
$Path=$song.".mp3";
$getID3 = new getID3;
$OldThisFileInfo = $getID3->analyze($Path);
if(isset($OldThisFileInfo['comments']['picture'][0]))
{
$ImageBase='data:'.$OldThisFileInfo['comments']['picture'][0]['image_mime'].';charset=utf-8;base64,'.base64_encode($OldThisFileInfo['comments']['picture'][0]['data']);
}
header('Content-Type: image/jpeg');
echo $ImageBase;
问题是我无法php显示图像。如何解决?
您不必将图像编码为base64,您可以直接echo
图像数据。
$getID3 = new getID3;
$OldThisFileInfo = $getID3->analyze($Path);
if(isset($OldThisFileInfo['comments']['picture'][0]))
{
header('Content-Type: ' . $OldThisFileInfo['comments']['picture'][0]['image_mime']);
echo $OldThisFileInfo['comments']['picture'][0]['data'];
die;
}
我想创建一个 php 文件,当作为 html 的 img 标签中的 src 参数调用时,该文件将 return mp3 歌曲的图像。像这样:
<img src="/imageReturn.php?song=SongName">
为了从 mp3 文件中获取图像,我使用了 getID3 库。我的 PHP 代码:
$song= $_GET["song"];
require_once("getID3-1.9.15/getid3/getid3.php");
$Path=$song.".mp3";
$getID3 = new getID3;
$OldThisFileInfo = $getID3->analyze($Path);
if(isset($OldThisFileInfo['comments']['picture'][0]))
{
$ImageBase='data:'.$OldThisFileInfo['comments']['picture'][0]['image_mime'].';charset=utf-8;base64,'.base64_encode($OldThisFileInfo['comments']['picture'][0]['data']);
}
header('Content-Type: image/jpeg');
echo $ImageBase;
问题是我无法php显示图像。如何解决?
您不必将图像编码为base64,您可以直接echo
图像数据。
$getID3 = new getID3;
$OldThisFileInfo = $getID3->analyze($Path);
if(isset($OldThisFileInfo['comments']['picture'][0]))
{
header('Content-Type: ' . $OldThisFileInfo['comments']['picture'][0]['image_mime']);
echo $OldThisFileInfo['comments']['picture'][0]['data'];
die;
}