playlist.php
author Dan
Mon, 01 Sep 2008 16:48:09 -0400
changeset 41 6e0beb10566d
parent 40 bd3372a2afc1
child 44 92dd253f501c
permissions -rw-r--r--
Ehh, forgot to add the spacer image for sprites.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     1
<?php
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     3
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     4
 * Playlist displayer
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     5
 *
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     6
 * Greyhound - real web management for Amarok
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    11
 *
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    15
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    16
function amarok_playlist($httpd, $socket)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    17
{
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    18
  global $theme, $playlist, $allowcontrol;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    19
  global $use_auth, $auth_data;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    20
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    21
  if ( $use_auth )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    22
  {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    23
    if ( !isset($_SERVER['PHP_AUTH_USER']) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    24
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    25
      $httpd->header('WWW-Authenticate: basic');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    26
      $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    27
      return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    28
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    29
    if ( !isset($auth_data[$_SERVER['PHP_AUTH_USER']]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    30
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    31
      $httpd->header('WWW-Authenticate: basic');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    32
      $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    33
      return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    34
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    35
    else if ( $auth_data[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    36
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    37
      $httpd->header('WWW-Authenticate: basic');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    38
      $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    39
      return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    40
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 10
diff changeset
    41
  }
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    42
  
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    43
  $iphone = ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    44
       strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') ||
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    45
       strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') ||
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    46
       isset($_GET['m']) )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    47
       && !isset($_GET['f'])
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    48
       );
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    49
  $theme_id = ( $iphone ) ? 'iphone' : $theme;
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    50
  $smarty = load_theme($theme_id);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    51
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    52
  $active = dcop_action('playlist', 'getActiveIndex');
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    53
  $smarty->assign('theme', $theme_id);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    54
  $smarty->assign('playlist', $playlist);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
  $smarty->assign('active', $active);
1
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    56
  $smarty->assign('scripts', array(
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    57
      'ajax.js',
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    58
      'domutils.js',
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    59
      'volume.js',
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    60
      'dom-drag.js',
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    61
      'position.js'
1
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    62
    ));
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    63
  $smarty->assign('allow_control', $allowcontrol);
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    64
  $smarty->register_function('sprite', 'smarty_function_sprite');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    65
  $smarty->display('playlist.tpl');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    66
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    67
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    68
function artwork_request_handler($httpd, $socket)
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    69
{
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 25
diff changeset
    70
  global $amarok_home;
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    71
  
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    72
  // get PATH_INFO
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    73
  $pathinfo = @substr(@substr($_SERVER['REQUEST_URI'], 1), @strpos(@substr($_SERVER['REQUEST_URI'], 1), '/')+1);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    74
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    75
  // should we do a collage (for CSS sprites instead of sending hundreds of individual images)?
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    76
  if ( preg_match('/^collage(?:\/([0-9]+))?$/', $pathinfo, $match) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    77
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    78
    // default size is 50px per image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    79
    $collage_size = ( isset($match[1]) ) ? intval($match[1]) : 50;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    80
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    81
    $artwork_dir = "$amarok_home/albumcovers";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    82
    if ( !file_exists("$artwork_dir/collage_{$collage_size}.png") )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    83
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    84
      if ( !generate_artwork_collage("$artwork_dir/collage_{$collage_size}.png", $collage_size) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    85
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    86
        echo 'Error: generate_artwork_collage() failed';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    87
        return;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    88
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    89
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    90
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    91
    $target_file = "$artwork_dir/collage_{$collage_size}.png";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    92
    // we have it now, send the image through
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    93
    $fh = @fopen($target_file, 'r');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    94
    if ( !$fh )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    95
      return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    96
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    97
    $httpd->header('Content-type: image/png');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    98
    $httpd->header('Content-length: ' . filesize($target_file));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
    99
    $httpd->header('Expires: Wed, 1 Jan 2020 01:00:00 GMT');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   100
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   101
    // kinda sorta a hack.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   102
    $headers = implode("\r\n", $httpd->response_headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   103
    $httpd->send_client_headers($socket, $httpd->response_code, $httpd->content_type, $headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   104
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   105
    while ( $d = fread($fh, 10240) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   106
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   107
      $socket->write($d);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   108
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   109
    fclose($fh);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   110
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   111
    return;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   112
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   113
  
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   114
  if ( !isset($_GET['artist']) || !isset($_GET['album']) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   115
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   116
    echo 'Please specify artist and album.';
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   117
    return;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   118
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   119
  // get hash
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   120
  $artwork_hash = md5( strtolower(trim($_GET['artist'])) . strtolower(trim($_GET['album'])) );
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 25
diff changeset
   121
  $artwork_dir = "$amarok_home/albumcovers";
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   122
  if ( file_exists("$artwork_dir/large/$artwork_hash") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   123
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   124
    // artwork file found - scale and convert to PNG
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   125
    if ( !is_dir("$artwork_dir/greyhoundthumbnails") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   126
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   127
      if ( !@mkdir("$artwork_dir/greyhoundthumbnails") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   128
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   129
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   130
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   131
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   132
    // check for the scaled cover image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   133
    $target_file = "$artwork_dir/greyhoundthumbnails/$artwork_hash.png";
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   134
    if ( !file_exists($target_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   135
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   136
      // not scaled yet, scale to uniform 50x50 image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   137
      $artwork_filetype = get_image_filetype("$artwork_dir/large/$artwork_hash");
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   138
      if ( !$artwork_filetype )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   139
      {
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   140
        // image is not supported (PNG, GIF, or JPG required)
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   141
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   142
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   143
      // 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)
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   144
      if ( !copy("$artwork_dir/large/$artwork_hash", "$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   145
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   146
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   147
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   148
      // finally, scale the image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   149
      if ( !scale_image("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype", $target_file, 50, 50) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   150
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   151
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   152
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   153
      // delete our temp file
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   154
      if ( !unlink("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   155
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   156
        echo 'Couldn\'t delete the temp file';
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   157
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   158
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   159
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   160
    // we have it now, send the image through
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   161
    $fh = @fopen($target_file, 'r');
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   162
    if ( !$fh )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   163
      return false;
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   164
    
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   165
    $httpd->header('Content-type: image/png');
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   166
    $httpd->header('Content-length: ' . filesize($target_file));
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   167
    $httpd->header('Expires: Wed, 1 Jan 2020 01:00:00 GMT');
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   168
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   169
    // kinda sorta a hack.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   170
    $headers = implode("\r\n", $httpd->response_headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   171
    $httpd->send_client_headers($socket, $httpd->response_code, $httpd->content_type, $headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   172
    
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   173
    while ( !feof($fh) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   174
    {
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   175
      $socket->write(fread($fh, 51200));
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   176
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   177
    fclose($fh);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   178
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   179
  else
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   180
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   181
    // artwork file doesn't exist
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   182
    $ar = htmlspecialchars($_GET['artist']);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   183
    $al = htmlspecialchars($_GET['album']);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   184
    $httpd->send_http_error($socket, 404, "The requested artwork file for $ar:$al could not be found on this server.");
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   185
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   186
}
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   187
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   188
/**
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   189
 * 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"
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   190
 * to allow retrieving positions of images. Requires GD.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   191
 * @param string Name of the collage file. Map file will be the same filename except with the extension ".map"
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   192
 * @param int Size of each image, in pixels. Artwork images will be stretched to a 1:1 aspect ratio. Optional, defaults to 50.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   193
 * @return bool True on success, false on failure.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   194
 */
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   195
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   196
function generate_artwork_collage($target_file, $size = 50)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   197
{
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   198
  // check for required GD functionality
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   199
  if ( !function_exists('imagecopyresampled') || !function_exists('imagepng') )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   200
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   201
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   202
  status("generating size $size collage");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   203
  $stderr = fopen('php://stderr', 'w');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   204
  if ( !$stderr )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   205
    // this should really never fail.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   206
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   207
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   208
  // import amarok globals
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   209
  global $amarok_home;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   210
  $artwork_dir = "$amarok_home/albumcovers";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   211
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   212
  // map file path
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   213
  $mapfile = preg_replace('/\.[a-z]+$/', '', $target_file) . '.map';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   214
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   215
  // open map file
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   216
  $maphandle = @fopen($mapfile, 'w');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   217
  if ( !$maphandle )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   218
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   219
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   220
  $mapheader = <<<EOF
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   221
# this artwork collage map gives the locations of various artwork images within the collage
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   222
# format is:
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   223
# hash                           x y
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   224
# x and y are indices, not pixel values (obviously), and hash is the name of the artwork file in large/
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   225
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   226
EOF;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   227
  fwrite($maphandle, $mapheader);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   228
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   229
  // build a list of existing artwork files
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   230
  $artwork_list = array();
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   231
  if ( $dh = @opendir("$artwork_dir/large") )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   232
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   233
    while ( $fp = @readdir($dh) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   234
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   235
      if ( preg_match('/^[a-f0-9]{32}$/', $fp) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   236
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   237
        $artwork_list[] = $fp;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   238
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   239
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   240
    closedir($dh);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   241
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   242
  else
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   243
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   244
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   245
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   246
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   247
  // at least one image?
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   248
  if ( empty($artwork_list) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   249
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   250
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   251
  // asort it to make sure map is predictable
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   252
  asort($artwork_list);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   253
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   254
  // number of columns
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   255
  $cols = 20;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   256
  // number of rows
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   257
  $rows = ceil( count($artwork_list) / $cols );
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   258
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   259
  // image dimensions
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   260
  $image_width  = $cols * $size;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   261
  $image_height = $rows * $size;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   262
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   263
  // create image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   264
  $collage = imagecreatetruecolor($image_width, $image_height);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   265
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   266
  // generator loop
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   267
  // start at row 0, column 0
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   268
  $col = -1;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   269
  $row = 0;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   270
  $srow = $row + 1;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   271
  fwrite($stderr, "  -> row $srow of $rows\r");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   272
  $time_map = microtime(true);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   273
  foreach ( $artwork_list as $artwork_file )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   274
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   275
    // calculate where we are
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   276
    $col++;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   277
    if ( $col == $cols )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   278
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   279
      // reached column limit, reset $cols and increment row
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   280
      $col = 0;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   281
      $row++;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   282
      $srow = $row + 1;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   283
      fwrite($stderr, "  -> row $srow of $rows\r");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   284
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   285
    // x and y offset of scaled image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   286
    $xoff = $col * $size;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   287
    $yoff = $row * $size;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   288
    // set offset
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   289
    fwrite($maphandle, "$artwork_file $col $row\n");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   290
    // load image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   291
    $createfunc = ( get_image_filetype("$artwork_dir/large/$artwork_file") == 'jpg' ) ? 'imagecreatefromjpeg' : 'imagecreatefrompng';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   292
    $aw = @$createfunc("$artwork_dir/large/$artwork_file");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   293
    if ( !$aw )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   294
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   295
      $aw = @imagecreatefromwbmp("$artwork_dir/large/$artwork_file");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   296
      if ( !$aw )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   297
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   298
        // couldn't load image, silently continue
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   299
        continue;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   300
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   301
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   302
    list($aw_width, $aw_height) = array(imagesx($aw), imagesy($aw));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   303
    // scale and position image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   304
    $result = imagecopyresampled($collage, $aw, $xoff, $yoff, 0, 0, $size, $size, $aw_width, $aw_height);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   305
    if ( !$result )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   306
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   307
      // couldn't scale image, silently continue
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   308
      continue;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   309
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   310
    // free the temp image
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   311
    imagedestroy($aw);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   312
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   313
  $time_map = round(1000 * (microtime(true) - $time_map));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   314
  $time_write = microtime(true);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   315
  fclose($maphandle);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   316
  fwrite($stderr, "  -> saving image\r");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   317
  if ( !imagepng($collage, $target_file) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   318
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   319
  imagedestroy($collage);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   320
  $time_write = round(1000 * (microtime(true) - $time_write));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   321
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   322
  $avg = round($time_map / count($artwork_list));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   323
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   324
  status("collage generation complete, returning success; time (ms): map/avg/write $time_map/$avg/$time_write");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   325
  return true;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   326
}
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   327
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   328
/**
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   329
 * Returns an img tag showing artwork from the specified size collage sprite.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   330
 * @param string Artist
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   331
 * @param string Album
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   332
 * @param int Collage size
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   333
 * @return string
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   334
 */
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   335
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   336
function get_artwork_sprite($artist, $album, $size = 50)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   337
{
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   338
  // import amarok globals
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   339
  global $amarok_home;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   340
  $artwork_dir = "$amarok_home/albumcovers";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   341
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   342
  if ( !is_int($size) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   343
    return '';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   344
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   345
  // hash of cover
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   346
  $coverid = md5(strtolower(trim($artist)) . strtolower(trim($album)));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   347
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   348
  $tag = '<img alt=" " src="/spacer.gif" width="' . $size . '" height="' . $size . '" ';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   349
  if ( file_exists("$artwork_dir/collage_{$size}.map") )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   350
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   351
    $mapdata = parse_collage_map("$artwork_dir/collage_{$size}.map");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   352
    if ( isset($mapdata[$coverid]) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   353
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   354
      $css_x = -1 * $size * $mapdata[$coverid][0];
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   355
      $css_y = -1 * $size * $mapdata[$coverid][1];
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   356
      $tag .= "style=\"background-image: url(/artwork/collage/$size); background-repeat: no-repeat; background-position: {$css_x}px {$css_y}px;\" ";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   357
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   358
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   359
  $tag .= '/>';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   360
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   361
  return $tag;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   362
}
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   363
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   364
/**
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   365
 * Parses the specified artwork map file. Return an associative array, keys being the artwork file hashes and values being array(x, y).
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   366
 * @param string Map file
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   367
 * @return array
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   368
 */
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   369
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   370
function parse_collage_map($mapfile)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   371
{
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   372
  if ( !file_exists($mapfile) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   373
    return array();
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   374
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   375
  $fp = @fopen($mapfile, 'r');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   376
  if ( !$fp )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   377
    return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   378
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   379
  $map = array();
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   380
  while ( $line = fgets($fp) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   381
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   382
    // parse out comments
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   383
    $line = trim(preg_replace('/#(.+)$/', '', $line));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   384
    if ( empty($line) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   385
      continue;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   386
    list($hash, $x, $y) = explode(' ', $line);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   387
    if ( !preg_match('/^[a-f0-9]{32}$/', $hash) || !preg_match('/^[0-9]+$/', $x) || !preg_match('/^[0-9]+$/', $y) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   388
      // invalid line
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   389
      continue;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   390
      
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   391
    // valid line, append map array
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   392
    $map[$hash] = array(
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   393
        intval($x),
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   394
        intval($y)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   395
      );
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   396
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   397
  fclose($fp);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   398
  return $map;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   399
}
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   400
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   401
/**
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   402
 * Finds out if a collage file is outdated (e.g. missing artwork images)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   403
 * @param int Size of collage
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   404
 * @return bool true if outdated
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   405
 */
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   406
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   407
function collage_is_outdated($size = 50)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   408
{
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   409
  global $amarok_home;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   410
  $artwork_dir = "$amarok_home/albumcovers";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   411
  $mapfile = "$artwork_dir/collage_{$size}.map";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   412
  if ( !file_exists($mapfile) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   413
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   414
    // consider it outdated if it doesn't exist
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   415
    return true;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   416
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   417
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   418
  // load existing image map
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   419
  $map = parse_collage_map($mapfile);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   420
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   421
  // build a list of existing artwork files
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   422
  $artwork_list = array();
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   423
  if ( $dh = @opendir("$artwork_dir/large") )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   424
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   425
    while ( $fp = @readdir($dh) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   426
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   427
      if ( preg_match('/^[a-f0-9]{32}$/', $fp) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   428
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   429
        // found an artwork file
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   430
        if ( !isset($map[$fp]) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   431
        {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   432
          // this artwork isn't in the map file, return outdated
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   433
          closedir($dh);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   434
          status("size $size collage is outdated");
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   435
          return true;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   436
        }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   437
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   438
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   439
    closedir($dh);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   440
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   441
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   442
  // if we reach here, we haven't found anything missing.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   443
  return false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   444
}
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   445
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   446
/**
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   447
 * Smarty function for sprite generation.
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   448
 * @access private
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   449
 */
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   450
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   451
function smarty_function_sprite($params, &$smarty)
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   452
{
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   453
  // don't perform the exhaustive check more than once per execution
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   454
  static $checks_done = array();
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   455
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   456
  if ( empty($params['artist']) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   457
    return 'Error: missing "artist" parameter';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   458
  if ( empty($params['album']) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   459
    return 'Error: missing "album" parameter';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   460
  if ( empty($params['size']) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   461
    $params['size'] = 50;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   462
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   463
  $params['size'] = intval($params['size']);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   464
  $size =& $params['size'];
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   465
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   466
  // if the collage file doesn't exist or is missing artwork, renew it
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   467
  // but only perform this check once per execution per size
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   468
  if ( !isset($checks_done[$size]) )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   469
  {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   470
    global $amarok_home;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   471
    $artwork_dir = "$amarok_home/albumcovers";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   472
    
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   473
    $collage_file = "$artwork_dir/collage_{$size}";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   474
    $collage_is_good = file_exists("$collage_file.png") && file_exists("$collage_file.map") && !collage_is_outdated($size);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   475
    if ( !$collage_is_good )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   476
    {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   477
      generate_artwork_collage("$collage_file.png", $size);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   478
    }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   479
    $checks_done[$size] = true;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   480
  }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   481
  
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   482
  return get_artwork_sprite($params['artist'], $params['album'], $params['size']);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   483
}
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 33
diff changeset
   484