playlist.php
changeset 25 5c377ceb0e4c
parent 21 74edc873234f
child 33 3b4aef1efff6
--- a/playlist.php	Wed Jul 02 11:57:13 2008 -0400
+++ b/playlist.php	Tue Aug 05 13:15:11 2008 -0400
@@ -64,3 +64,75 @@
   $smarty->display('playlist.tpl');
 }
 
+function artwork_request_handler($httpd, $socket)
+{
+  global $homedir;
+  
+  if ( !isset($_GET['artist']) || !isset($_GET['album']) )
+  {
+    echo 'Please specify artist and album.';
+    return;
+  }
+  // get hash
+  $artwork_hash = md5( strtolower(trim($_GET['artist'])) . strtolower(trim($_GET['album'])) );
+  $artwork_dir = "$homedir/.kde/share/apps/amarok/albumcovers";
+  if ( file_exists("$artwork_dir/large/$artwork_hash") )
+  {
+    // artwork file found - scale and convert to PNG
+    if ( !is_dir("$artwork_dir/greyhoundthumbnails") )
+    {
+      if ( !@mkdir("$artwork_dir/greyhoundthumbnails") )
+      {
+        return false;
+      }
+    }
+    // check for the scaled cover image
+    $target_file = "$artwork_dir/greyhoundthumbnails/$artwork_hash.png";
+    if ( !file_exists($target_file) )
+    {
+      // not scaled yet, scale to uniform 50x50 image
+      $artwork_filetype = get_image_filetype("$artwork_dir/large/$artwork_hash");
+      if ( !$artwork_filetype )
+      {
+        return false;
+      }
+      // we'll need to copy the existing artwork file to our thumbnail dir to let scale_image() detect the type properly (it doesn't use magic bytes)
+      if ( !copy("$artwork_dir/large/$artwork_hash", "$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
+      {
+        return false;
+      }
+      // finally, scale the image
+      if ( !scale_image("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype", $target_file, 50, 50) )
+      {
+        return false;
+      }
+      // delete our temp file
+      if ( !unlink("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
+      {
+        echo 'Couldn\'t delete the temp file';
+        return false;
+      }
+    }
+    // we have it now, send the image through
+    $fh = @fopen($target_file, 'r');
+    if ( !$fh )
+      return false;
+    $httpd->header('Content-type: image/png');
+    $httpd->header('Content-length: ' . filesize($target_file));
+    $httpd->header('Expires: Wed, 1 Jan 2020 01:00:00 GMT');
+    while ( !feof($fh) )
+    {
+      socket_write($socket, fread($fh, 51200));
+    }
+    fclose($fh);
+  }
+  else
+  {
+    // artwork file doesn't exist
+    $ar = htmlspecialchars($_GET['artist']);
+    $al = htmlspecialchars($_GET['album']);
+    $httpd->send_http_error($socket, 404, "The requested artwork file for $ar:$al could not be found on this server.");
+  }
+}
+
+