forked from sidooms/Recsys-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmovieposter.php
63 lines (49 loc) · 1.94 KB
/
getmovieposter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Recsys-frontend, Copyright (c) 2013, Simon Dooms
* http://github.com/sidooms/recsys-frontend
* MIT License
*/
$title = $_REQUEST['title'];
//strip slashes in the title
$title = stripslashes($title);
$year = $_REQUEST['year'];
$url = $_REQUEST['url'];
$notfoundurl = 'img/movieposters/not_found.gif';
$movieposterurl = "img/movieposters";
$uri = $_SERVER["REQUEST_URI"];
$pattern = '/(.*)getmovieposter.php\?.*/i';
$replacement = '$1';
$uri = preg_replace($pattern, $replacement, $uri);
$onlinemovieposterurl = 'http://' . $_SERVER['HTTP_HOST'] . str_replace('getmovieposter.php', '', $uri) . 'img/movieposters';
$hash = md5($title . "_" . $year);
$movieposterfile = $movieposterurl . "/" . $hash . ".jpg";
$onlinemovieposterfile = $onlinemovieposterurl . "/" . $hash . ".jpg";
if ($url == 'N/A'){
print googleImageSearch($title . ' ' . $year . ' poster');
exit();
}
//check if this movie poster is available
if (!file_exists($movieposterfile) || filesize($movieposterfile) == 0){
//not available so download
try{
$content = file_get_contents($url);
file_put_contents($movieposterfile, $content);
}catch (Exception $e){
print $notfoundurl;
exit();
}
}
if (file_exists($movieposterfile) && filesize($movieposterfile) != 0)
//return movie poster
print $onlinemovieposterfile;
else
print $notfoundurl;
//http://mikefigueroa.com/blog/2011/08/get-first-google-image-search-result-with-php/
function googleImageSearch($term){
$q = urlencode($term);
$jsonurl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".$q;
$result = json_decode(file_get_contents($jsonurl), true);
return $result['responseData']['results'][0]['tbUrl'];
}
?>