# HG changeset patch # User Dan # Date 1217956511 14400 # Node ID 5c377ceb0e4cd270ab47a3dc8ebefbfa2cc8150f # Parent d275dc8f4203af5d01122c944a500fceb1a7b6ff Added initial album artwork support. diff -r d275dc8f4203 -r 5c377ceb0e4c greyhound.php --- a/greyhound.php Wed Jul 02 11:57:13 2008 -0400 +++ b/greyhound.php Tue Aug 05 13:15:11 2008 -0400 @@ -40,7 +40,7 @@ // Allow forking when an HTTP request is received. This has advantages // and disadvantages. If this experimental option is enabled, it will // result in faster responses and load times but more memory usage. -$allow_fork = false; +$allow_fork = true; // set to true to enable authentication // WARNING: THIS HAS SOME SERIOUS SECURITY PROBLEMS RIGHT NOW. I don't // know what's causing it to not prompt for authentication from any @@ -74,6 +74,7 @@ require(GREY_ROOT . '/playlist.php'); require(GREY_ROOT . '/json.php'); require(GREY_ROOT . '/ajax.php'); +require(GREY_ROOT . '/imagetools.php'); status('doing home directory detection'); @@ -118,6 +119,7 @@ status('initializing handlers'); $httpd->add_handler('index', 'function', 'amarok_playlist'); $httpd->add_handler('action.json', 'function', 'ajax_request_handler'); + $httpd->add_handler('artwork', 'function', 'artwork_request_handler'); $httpd->add_handler('scripts', 'dir', GREY_ROOT . '/scripts'); $httpd->add_handler('favicon.ico', 'file', GREY_ROOT . '/amarok_icon.ico'); $httpd->add_handler('apple-touch-icon.png', 'file', GREY_ROOT . '/apple-touch-icon.png'); diff -r d275dc8f4203 -r 5c377ceb0e4c imagetools.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagetools.php Tue Aug 05 13:15:11 2008 -0400 @@ -0,0 +1,205 @@ +\" \"$out_file\""; + system($cmdline, $return); + if ( !file_exists($out_file) ) + return false; + return true; + } + else if ( $can_use_gd ) + { + @list($width_orig, $height_orig) = @getimagesize($in_file); + if ( !$width_orig || !$height_orig ) + return false; + // calculate new width and height + + $ratio = $width_orig / $height_orig; + if ( $ratio > 1 ) + { + // orig. width is greater that height + $new_width = $width; + $new_height = round( $width / $ratio ); + } + else if ( $ratio < 1 ) + { + // orig. height is greater than width + $new_width = round( $height / $ratio ); + $new_height = $height; + } + else if ( $ratio == 1 ) + { + $new_width = $width; + $new_height = $width; + } + if ( $new_width > $width_orig || $new_height > $height_orig ) + { + // Too big for our britches here; set it to only convert the file + $new_width = $width_orig; + $new_height = $height_orig; + } + + $newimage = @imagecreatetruecolor($new_width, $new_height); + if ( !$newimage ) + return false; + $oldimage = @$func($in_file); + if ( !$oldimage ) + return false; + + // Perform scaling + imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); + + // Get output format + $out_ext = substr($out_file, ( strrpos($out_file, '.') + 1)); + switch($out_ext) + { + case 'png': + $outfunc = 'imagepng'; + break; + case 'jpg': + case 'jpeg': + $outfunc = 'imagejpeg'; + break; + case 'gif': + $outfunc = 'imagegif'; + break; + case 'xpm': + $outfunc = 'imagexpm'; + break; + default: + imagedestroy($newimage); + imagedestroy($oldimage); + return false; + } + + // Write output + $outfunc($newimage, $out_file); + + // clean up + imagedestroy($newimage); + imagedestroy($oldimage); + + // done! + return true; + } + if ( file_exists($out_file) ) + return true; + return false; +} + diff -r d275dc8f4203 -r 5c377ceb0e4c playlist.php --- 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."); + } +} + + diff -r d275dc8f4203 -r 5c377ceb0e4c themes/iphone/playlist.tpl --- a/themes/iphone/playlist.tpl Wed Jul 02 11:57:13 2008 -0400 +++ b/themes/iphone/playlist.tpl Tue Aug 05 13:15:11 2008 -0400 @@ -74,6 +74,9 @@ {$track.title|escape}
+
+  +
Artist: {$track.artist|escape}
Album: {$track.album|escape}