Do you happen to have come by a large collection of MP3’s, ordered in folders by album? Are they named something like “Artist Name – Album Name”? Would you like to download the cover for each album so you can browse them like this in explorer?

Yes? Then put the wad of PHP code below into a file (named get_covers.php or whatever you like) in the root folder of your MP3’s, and execute it using PHP. This script will do a google image search for every folder which doesn’t have a folder.jpg and download the resulting image into folder.jpg, providing a nice thumbnail for Windows Explorer. For 99% of my ~500 albums, this worked perfectly.
This script downloads arbitrary image results from Google to your hard drive, directly from the resulting websites, without any attempt at filtering out harmful results. The results may include corrupt or virus infected images and pictures of naked ladies, which can seriously harm your computer.
<?php
$folders = glob('*');
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 10
)
)
);
foreach($folders as $folder) {
if(!is_dir($folder)) {
echo "Skipping {$folder}\n";
continue;
}
if(is_file($folder."/folder.jpg")) {
echo "Already have cover for {$folder}\n";
continue;
}
$album = str_replace('_',' ',$folder);
echo "Checking {$album}.. ";
$googleUrl = "http://images.google.com/images?um=1&hl=en&safe=off&imgsz=medium&q=".urlencode($album);
$contents = file_get_contents($googleUrl,0,$ctx);
if(preg_match_all('/imgurl\\\\x3d(.*?)\\\\x26/i',$contents,$matches)) {
foreach($matches[1] as $image) {
if($imageContents = file_get_contents($image,0,$ctx)) {
file_put_contents("{$folder}/folder.jpg",$imageContents);
echo "Found image {$image}\n";
break;
}
}
}
else {
echo "nothing found!\n";
}
}
?>
This is a brilliant little script, not just for mp3s may I add ;)
I used a text file (line seperated) with a list of folders I wanted created and then ran this:
FOR /F “delims=~” %f in (FolderList.txt) DO MD “%f”
Which creates all the folders in the directory you run it from.
I then ran your helpful script to get images for every folder :) Can be used for just about anything!
One question I must ask though…is there anyway to make it so the images are named the same as the folder???
Thanks dude!
Comment by Anon — August 1, 2008 @ 12:52 pm
Dear Anonymous Guy,
glad it’s of help to you!
You can have the image named the same as the folder name in stead of folder.jpg by changing this line of script:
file_put_contents(“{$folder}/folder.jpg”,$imageContents);
to
file_put_contents(“{$folder}/{$folder}.jpg”,$imageContents);
This way you would get an image like c:\map\to\thumbnailize\some_folder_name\some_folder_name.jpg
Hope this helps!
Comment by bosmeeuw — August 1, 2008 @ 1:07 pm
great code, with this code you can download images from google automaticly.
Comment by Gofrit Toerme — April 24, 2009 @ 10:11 pm
Does this still work it looks like Google changed the layout of their page so that now it uses javascript to load the image
Comment by Edward — May 2, 2009 @ 2:29 am