Just listen to Alex

July 12, 2008

Batch downloading cover art with PHP and Google Image Search

Filed under: programming — Tags: — bosmeeuw @ 2:58 pm

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?

Screenshot

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";
	}
}
?>
Advertisements

8 Comments »

  1. 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

  2. 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

  3. great code, with this code you can download images from google automaticly.

    Comment by Gofrit Toerme — April 24, 2009 @ 10:11 pm

  4. 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

  5. hey this is sweet! well done :)

    thanks a lot!

    Comment by telaxu — March 20, 2010 @ 9:43 pm

  6. […] gesucht, schnell und einfach an einige Bilder von der Google Bildersuche zu kommen.Mit dem Code von bosmeeuw ist mir dies dann endlich gelungen.bosmeeuw hat seinen Code benutzt um […]

    Pingback by Antraxia.com » Google Bildersuche – Bilder via PHP anzeigen lassen — August 11, 2010 @ 4:54 am

  7. Hello…

    i love this script ! but i’ve a personal request…

    finding image cover from a list of .mp3 in same folder (all in one folder)

    and

    download and save every image with the same name of the .mp3 file but in another folder (or the same no problem!)

    please help me…(i’ve install a lot of application but nothing worked)

    Comment by Marco — January 17, 2012 @ 5:16 pm

  8. Does this still work? I run it, and it tells me that nothing was found in the folders, but it doesn’t do anything after that. What gives? I’m running Appache/php on Xampp. Here’s what is reported back in the browser window:

    “Skipping fetch_images_google.php Checking you-yourself.. nothing found! Checking you-yourselves.. nothing found! Checking zero.. nothing found! Checking zone.. nothing found!”

    It added no images to the folders whatsoever.

    I really want to make this work. It is EXACTLY what I need, and I can’t find anything else like it anywhere on the web. I’m actually using it to find pictures for a massive flashcard database automatically.

    Please advise!

    :-)

    Comment by Austin — May 21, 2012 @ 10:14 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: