When using XBMC we usually use the ‘Files’ menu. The folder icons under ‘Files’ do not show a preview thumbnail unless there is an image in the folder named ‘folder.jpg’. I could manually search for folder images for each movie but decided that would be time-consuming so I put together a wee PHP script – getMovieThumbs.php – to retrieve the image from omdbapi.com and save it into the appropriate directory as folder.jpg
Running this script saved over 230 new thumbnail images for me in one folder alone.
$DirectoryToCheck="/movies/kids";
chdir($DirectoryToCheck);
$directories = glob('*' , GLOB_ONLYDIR);
$arrStripFrom=array("19", "20","1080p","720p","1080","720","DTS","HDTV","RETAIL","LIMITED","BDRip","BRip","WebDL","Widescreen","XVid","PROPER","X264","Xvid","AVI","Unrated","REPACK","S01","HD","TS", "LIMITED","DVD");
function my_strpos($haystack, $needle) {
if (is_array($needle)) {
foreach ($needle as $need) {
if (strpos($haystack, $need) !== false) {
return strpos($haystack, $need);
}
}
return false;
}
}
foreach($directories as $directory){
$directory_unmodified=$directory;
$img = $directory_unmodified.'/folder.jpg';
if (file_exists($img)){
printf("folder.jpg already exists - skipping ".$directory_unmodified."...\n");
}else{
$symbols = array(".", "-", "_","[","]","(",")");
$directory = str_replace($symbols, " ", $directory);
// Check the string to see if there is a year that can be added to the search to improve accuracy
if(preg_match("/[1,2][9,0][0-9][0-9]/", $directory_unmodified, $matches)==true){
if($matches[0]!="1080"){
$strSearchYear="y=".$matches[0]."&";
}
}else{
$strSearchYear="";
}
$pos = my_strpos($directory, $arrStripFrom);
printf("\n-----------".$DirectoryToCheck."/".$directory_unmodified."------------------------------- \n");
if ($pos !== false) {
$directory=substr($directory, 0, $pos-1);
}
printf("Checking ".$directory." .... \n");
$directory=urlencode($directory);
$jsonurl = "http://www.omdbapi.com/?".$strSearchYear."i=&s=".$directory;
echo $jsonurl;
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
if(isset($json_output['Response'])&&$json_output['Response']=="False"){
printf("\nNo Results found .. move on\n");
}else{
$jsontitleurl = "http://www.omdbapi.com/?i=".$json_output['Search'][0]['imdbID'];
$jsontitle = file_get_contents($jsontitleurl,0,null,null);
$json_title_output = json_decode($jsontitle,true);
printf("\nSaving to folder.jpg image URL: \n".$json_title_output['Poster']."\n");
//Now save the image as folder.jpg in the appropriate directory
$img = $directory_unmodified.'/folder.jpg';
file_put_contents($img, file_get_contents($json_title_output['Poster']));
printf("folder.jpg saved\n");
}
} // end if folder.jpg does not exist
printf("\n------------------------------------------ \n");
}