Update support for Amazon coverart grabbing (patch from Václav Nováček <waclaw66...
[patchfork.git] / player / metadata_cover.php
1 <?php
2 /* 
3     Pitchfork Music Player Daemon Client
4     Copyright (C) 2007  Roger Bystrøm
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; version 2 of the License.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20         $cover_providers = array("directory_cover", "amazon_search_cover");
21
22         /* If you want to use this you'll need php-gd as well */
23         /* first %s will be replaced by Artist and second %s will be replaced by Album */
24         /* can be local path or url (http://someserver/path/%s/%s/cover.jpg will become
25            e.g. http://someserver/path/Escape The Fate/Dying Is Your Latest Fashion/cover.jpg */
26         //$directory_search_url = "/path/to/somehere/%s/%s.jpg";
27         $directory_search_url = false;
28
29         function amazon_search_cover($artist, $album) {
30                 global $amazon_cover_url, $metadata_dir;
31
32                 $xml = amazon_album_query(array("Operation"=>"ItemSearch", "SearchIndex"=>"Music", "ResponseGroup"=>"Images", "Artist"=>"$artist", "Album"=>"$album"));
33                 if($xml) {
34                         if(isset($xml->Items[0])&&isset($xml->Items[0]->Item[0])) {
35                                 $item = $xml->Items[0]->Item[0];
36                                 $small = false;
37                                 $small_name = false;
38                                 $large = false;
39                                 $large_name = false;
40                                 
41                                 if(isset($item->SmallImage[0])) {
42                                         $small = @file_get_contents($item->SmallImage[0]->URL[0]);
43                                         if($small) {
44                                                 $small_name = escape_name($artist) . "-" . escape_name($album) . basename($item->SmallImage[0]->URL[0]);
45                                                 if(!@file_put_contents($metadata_dir . $small_name, $small)) {
46                                                         $small = false;
47                                                 }
48                                         }
49                                 }
50                                 if(isset($item->LargeImage[0])) {
51                                         $large = @file_get_contents($item->LargeImage[0]->URL[0]);
52                                         if($large) {
53                                                 $large_name = escape_name($artist) . "-" . escape_name($album) . basename($item->LargeImage[0]->URL[0]);
54                                                 if(!@file_put_contents($metadata_dir . $large_name, $large)) {
55                                                         $large = false;
56                                                 }
57                                         }
58                                 }
59                                 else if(isset($item->MediumImage[0])) {
60                                         $large = @file_get_contents($item->MediumImage[0]->URL[0]);
61                                         if($large) {
62                                                 $large_name = escape_name($artist) . "-" . escape_name($album) . basename($item->MediumImage[0]->URL[0]);
63                                                 if(!@file_put_contents($metadata_dir . $large_name, $large)) {
64                                                         $large = false;
65                                                 }
66                                         }
67                                 }
68                                 if($small && $large) {
69                                         $data = array();
70                                         $data['asin'] = $item->ASIN[0];
71                                         $data['thumbnail'] = $small_name;
72                                         $data['image'] =  $large_name;
73                                         return $data;
74                                 }
75                         }
76                 }
77                 return false;
78         }
79
80         function directory_cover($artist, $album) {
81                 global $directory_search_url, $metadata_dir;
82
83                 if(!$directory_search_url) 
84                         return false;
85                 
86                 $artist = escape_name($artist);
87                 $album = escape_name($album);
88
89                 $name = sprintf($directory_search_url, $artist, $album);
90
91                 return make_thumb_and_store_image($name, get_cover_base_name($artist, $album));
92         }
93
94         /* yay for me and function names */
95         function make_thumb_and_store_image($file, $store_bname) {
96                 // only supports jpeg for now..
97                 $max_size = 75; 
98                 $image = @imagecreatefromjpeg($file);
99                 if(!$image)
100                         return false;
101
102                 $ix = imagesx($image);
103                 $iy = imagesy($image);
104                 $rx = $ry = $max_size;
105                 
106                 $max_i = $ix>$iy?$ix:$iy;
107                 $res = array();
108                 $res['image'] = basename($store_bname . ".jpg");
109                 if(!@imagejpeg($image, $store_bname . ".jpg", 90)) {
110                         return false;
111                 }
112                 if($max_i>$max_size) {
113                         $ratio = (double)$max_i/(double)$max_size;
114                         $rx = (int)((double)$ix/$ratio);
115                         $ry = (int)((double)$iy/$ratio);
116                         $thumb = imagecreatetruecolor($rx,$ry);
117                         imagecopyresampled($thumb, $image, 0,0,0,0, $rx, $ry, $ix, $iy);
118                         $res['thumbnail'] = basename($store_bname . "_thumb.jpg");
119                         if(!@imagejpeg($thumb, $store_bname . "_thumb.jpg", 90)) {
120                                 return false;
121                         }
122                 }
123                 else {
124                         $res['thumb'] = $res['image'];
125                 }
126                 return $res;
127         }
128 ?>
This page took 0.025153 seconds and 4 git commands to generate.