1

I have the following code to get the album cover of an mp3 using getid3, the problem is though, how do I copy this image and put it into a specified directory?

<?php
require_once('getid3/getid3.php');
$file = "path/to/mp3/file.mp3"; 


$getID3 = new getID3; 
$getID3->option_tag_id3v2 = true;
$getID3->option_tags_images = true;
$getID3->analyze($file); 
if (isset($getID3->info['id3v2']['APIC'][0]['data'])) { 
   $cover = $getID3->info['id3v2']['APIC'][0]['data']; 
} elseif (isset($getID3->info['id3v2']['PIC'][0]['data'])) { 
   $cover = $getID3->info['id3v2']['PIC'][0]['data']; 
} else { 
   $cover = "no_cover"; 
} 
if (isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) { 
   $mimetype = $getID3->info['id3v2']['APIC'][0]['image_mime']; 
} else { 
   $mimetype = 'image/jpeg';
} 

if (!is_null($cover)) { 
// Send file 
header("Content-Type: " . $mimetype); 

if (isset($getID3->info['id3v2']['APIC'][0]['image_bytes'])) { 
    header("Content-Length: " . $getID3->info['id3v2']['APIC'][0]['image_bytes']); 
} 

echo ($cover);
?>

Is this possible, if yes how? Thanks for any help :)

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
Lennart
  • 1,560
  • 5
  • 20
  • 38

1 Answers1

2

have you tried:

file_put_contents('<filename>', $getID3->info['id3v2']['APIC'][0]['image_bytes']);
ghstcode
  • 2,902
  • 1
  • 20
  • 30
  • Thanks brilliant idea, it didn't quite work but this: `file_put_contents('cover.jpeg', $getID3->info['id3v2']['APIC'][0]['data']);` did. Thanks for your help :) – Lennart Oct 09 '11 at 19:18
  • awesome Lenny, glad it helped you! :) – ghstcode Oct 09 '11 at 19:45