1

I want to create a php file that will return an image of an mp3 song when called as the src parameter in the img tag of html. Something like this:

<img src="/imageReturn.php?song=SongName">

In order to get the image from the mp3 file I used the getID3 library. My PHP code:

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

The problem is that I cant get php display the image. How to fix it?

Nick
  • 241
  • 1
  • 2
  • 10
  • Try to debug by browsing directly `/imageReturn.php?song=SongName` and comment the `header('Content-Type: image/jpeg');` line. What's displayed? And maybe use `var_dump($ImageBase);` to have more informations. – Syscall May 06 '18 at 10:10
  • @Syscall By commenting the header line I get the base64 format of the image. `var_dump` displays `string(277121) "data:image/jpeg;charset=utf-8;base64,/9j/4AAQSk......` – Nick May 06 '18 at 10:16
  • And what do you get going directly to `/imageReturn.php?song=SongName` with the header line there? – Dave May 06 '18 at 10:19
  • @Dave I get the black background of the image display and nothing at the center as image. – Nick May 06 '18 at 10:21

1 Answers1

4

You don't have to encode the image to base64, you can directly echo the image data.

$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'];
}
Arvy
  • 1,072
  • 2
  • 16
  • 29
Syscall
  • 19,327
  • 10
  • 37
  • 52