diff -r 38dbcda3cf20 -r bd3372a2afc1 playlist.php --- a/playlist.php Mon Sep 01 13:05:52 2008 -0400 +++ b/playlist.php Mon Sep 01 13:06:50 2008 -0400 @@ -61,6 +61,7 @@ 'position.js' )); $smarty->assign('allow_control', $allowcontrol); + $smarty->register_function('sprite', 'smarty_function_sprite'); $smarty->display('playlist.tpl'); } @@ -68,6 +69,48 @@ { global $amarok_home; + // get PATH_INFO + $pathinfo = @substr(@substr($_SERVER['REQUEST_URI'], 1), @strpos(@substr($_SERVER['REQUEST_URI'], 1), '/')+1); + + // should we do a collage (for CSS sprites instead of sending hundreds of individual images)? + if ( preg_match('/^collage(?:\/([0-9]+))?$/', $pathinfo, $match) ) + { + // default size is 50px per image + $collage_size = ( isset($match[1]) ) ? intval($match[1]) : 50; + + $artwork_dir = "$amarok_home/albumcovers"; + if ( !file_exists("$artwork_dir/collage_{$collage_size}.png") ) + { + if ( !generate_artwork_collage("$artwork_dir/collage_{$collage_size}.png", $collage_size) ) + { + echo 'Error: generate_artwork_collage() failed'; + return; + } + } + + $target_file = "$artwork_dir/collage_{$collage_size}.png"; + // 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'); + + // kinda sorta a hack. + $headers = implode("\r\n", $httpd->response_headers); + $httpd->send_client_headers($socket, $httpd->response_code, $httpd->content_type, $headers); + + while ( $d = fread($fh, 10240) ) + { + $socket->write($d); + } + fclose($fh); + + return; + } + if ( !isset($_GET['artist']) || !isset($_GET['album']) ) { echo 'Please specify artist and album.'; @@ -94,6 +137,7 @@ $artwork_filetype = get_image_filetype("$artwork_dir/large/$artwork_hash"); if ( !$artwork_filetype ) { + // image is not supported (PNG, GIF, or JPG required) 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) @@ -117,12 +161,18 @@ $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'); + + // kinda sorta a hack. + $headers = implode("\r\n", $httpd->response_headers); + $httpd->send_client_headers($socket, $httpd->response_code, $httpd->content_type, $headers); + while ( !feof($fh) ) { - socket_write($socket, fread($fh, 51200)); + $socket->write(fread($fh, 51200)); } fclose($fh); } @@ -135,4 +185,300 @@ } } +/** + * Generates a collage of all album art for use as a CSS sprite. Also generates a textual .map file in the format of "hash xpos ypos\n" + * to allow retrieving positions of images. Requires GD. + * @param string Name of the collage file. Map file will be the same filename except with the extension ".map" + * @param int Size of each image, in pixels. Artwork images will be stretched to a 1:1 aspect ratio. Optional, defaults to 50. + * @return bool True on success, false on failure. + */ +function generate_artwork_collage($target_file, $size = 50) +{ + // check for required GD functionality + if ( !function_exists('imagecopyresampled') || !function_exists('imagepng') ) + return false; + + status("generating size $size collage"); + $stderr = fopen('php://stderr', 'w'); + if ( !$stderr ) + // this should really never fail. + return false; + + // import amarok globals + global $amarok_home; + $artwork_dir = "$amarok_home/albumcovers"; + + // map file path + $mapfile = preg_replace('/\.[a-z]+$/', '', $target_file) . '.map'; + + // open map file + $maphandle = @fopen($mapfile, 'w'); + if ( !$maphandle ) + return false; + + $mapheader = << row $srow of $rows\r"); + $time_map = microtime(true); + foreach ( $artwork_list as $artwork_file ) + { + // calculate where we are + $col++; + if ( $col == $cols ) + { + // reached column limit, reset $cols and increment row + $col = 0; + $row++; + $srow = $row + 1; + fwrite($stderr, " -> row $srow of $rows\r"); + } + // x and y offset of scaled image + $xoff = $col * $size; + $yoff = $row * $size; + // set offset + fwrite($maphandle, "$artwork_file $col $row\n"); + // load image + $createfunc = ( get_image_filetype("$artwork_dir/large/$artwork_file") == 'jpg' ) ? 'imagecreatefromjpeg' : 'imagecreatefrompng'; + $aw = @$createfunc("$artwork_dir/large/$artwork_file"); + if ( !$aw ) + { + $aw = @imagecreatefromwbmp("$artwork_dir/large/$artwork_file"); + if ( !$aw ) + { + // couldn't load image, silently continue + continue; + } + } + list($aw_width, $aw_height) = array(imagesx($aw), imagesy($aw)); + // scale and position image + $result = imagecopyresampled($collage, $aw, $xoff, $yoff, 0, 0, $size, $size, $aw_width, $aw_height); + if ( !$result ) + { + // couldn't scale image, silently continue + continue; + } + // free the temp image + imagedestroy($aw); + } + $time_map = round(1000 * (microtime(true) - $time_map)); + $time_write = microtime(true); + fclose($maphandle); + fwrite($stderr, " -> saving image\r"); + if ( !imagepng($collage, $target_file) ) + return false; + imagedestroy($collage); + $time_write = round(1000 * (microtime(true) - $time_write)); + + $avg = round($time_map / count($artwork_list)); + + status("collage generation complete, returning success; time (ms): map/avg/write $time_map/$avg/$time_write"); + return true; +} + +/** + * Returns an img tag showing artwork from the specified size collage sprite. + * @param string Artist + * @param string Album + * @param int Collage size + * @return string + */ + +function get_artwork_sprite($artist, $album, $size = 50) +{ + // import amarok globals + global $amarok_home; + $artwork_dir = "$amarok_home/albumcovers"; + + if ( !is_int($size) ) + return ''; + + // hash of cover + $coverid = md5(strtolower(trim($artist)) . strtolower(trim($album))); + + $tag = '