3     Pitchfork Music Player Daemon Client
 
   4     Copyright (C) 2007  Roger Bystrøm
 
   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.
 
  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.
 
  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.
 
  20         $cover_providers = array("directory_cover", "amazon_search_cover");
 
  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;
 
  29         function amazon_search_cover($artist, $album) {
 
  30                 global $amazon_cover_url, $metadata_dir;
 
  32                 $xml = amazon_album_query(array("Operation"=>"ItemSearch", "SearchIndex"=>"Music", "ResponseGroup"=>"Images", "Artist"=>"$artist"), $album);
 
  34                         if(isset($xml->Items[0])&&isset($xml->Items[0]->Item[0])) {
 
  35                                 $item = $xml->Items[0]->Item[0];
 
  41                                 if(isset($item->SmallImage[0])) {
 
  42                                         $small = @file_get_contents($item->SmallImage[0]->URL[0]);
 
  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)) {
 
  50                                 if(isset($item->LargeImage[0])) {
 
  51                                         $large = @file_get_contents($item->LargeImage[0]->URL[0]);
 
  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)) {
 
  59                                 else if(isset($item->MediumImage[0])) {
 
  60                                         $large = @file_get_contents($item->MediumImage[0]->URL[0]);
 
  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)) {
 
  68                                 if($small && $large) {
 
  70                                         $data['asin'] = $item->ASIN[0];
 
  71                                         $data['thumbnail'] = $small_name;
 
  72                                         $data['image'] =  $large_name;
 
  80         function directory_cover($artist, $album) {
 
  81                 global $directory_search_url, $metadata_dir;
 
  83                 if(!$directory_search_url) 
 
  86                 $artist = escape_name($artist);
 
  87                 $album = escape_name($album);
 
  89                 $name = sprintf($directory_search_url, $artist, $album);
 
  91                 return make_thumb_and_store_image($name, get_cover_base_name($artist, $album));
 
  94         /* yay for me and function names */
 
  95         function make_thumb_and_store_image($file, $store_bname) {
 
  96                 // only supports jpeg for now..
 
  98                 $image = @imagecreatefromjpeg($file);
 
 102                 $ix = imagesx($image);
 
 103                 $iy = imagesy($image);
 
 104                 $rx = $ry = $max_size;
 
 106                 $max_i = $ix>$iy?$ix:$iy;
 
 108                 $res['image'] = basename($store_bname . ".jpg");
 
 109                 if(!@imagejpeg($image, $store_bname . ".jpg", 90)) {
 
 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)) {
 
 124                         $res['thumb'] = $res['image'];