imagetools.php
author Dan
Tue, 23 Sep 2008 23:26:18 -0400
changeset 50 1b4288399b1f
parent 40 bd3372a2afc1
child 73 1f55c324efcf
permissions -rw-r--r--
Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     1
<?php
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     2
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     3
/**
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     4
 * Greyhound - real web management for Amarok
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     5
 * Copyright (C) 2008 Dan Fuhry
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     6
 *
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     7
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     8
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
     9
 *
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    11
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    12
 */
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    13
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    14
/**
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    15
 * Determines an image's filetype based on its signature.
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    16
 * @param string Path to image file
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    17
 * @return string One of gif, png, or jpg, or false if none of these.
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    18
 */
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    19
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    20
function get_image_filetype($filename)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    21
{
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    22
  $filecontents = @file_get_contents($filename);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    23
  if ( empty($filecontents) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    24
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    25
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    26
  if ( substr($filecontents, 0, 8) == "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    27
    return 'png';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    28
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    29
  if ( substr($filecontents, 0, 6) == 'GIF87a' || substr($filecontents, 0, 6) == 'GIF89a' )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    30
    return 'gif';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    31
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    32
  if ( substr($filecontents, 0, 2) == "\xFF\xD8" )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    33
    return 'jpg';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    34
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    35
  return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    36
}
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    37
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    38
/**
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    39
 * Scales an image to the specified width and height, and writes the output to the specified
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    40
 * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
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: 25
diff changeset
    41
 * always scale images proportionally, unless $preserve_ratio is set to false.
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    42
 * 
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    43
 * Ported from Enano CMS (which is also my project)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    44
 * 
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    45
 * @param string Path to image file
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    46
 * @param string Path to output file
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    47
 * @param int Image width, in pixels
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    48
 * @param int Image height, in pixels
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    49
 * @param bool If true, the output file will be deleted if it exists before it is written
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: 25
diff changeset
    50
 * @param bool If true, preserves the image's aspect ratio (default)
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    51
 * @return bool True on success, false on failure
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    52
 */
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    53
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: 25
diff changeset
    54
function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false, $preserve_ratio = true)
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    55
{
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    56
  global $db, $session, $paths, $template, $plugins; // Common objects
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    57
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    58
  if ( !is_int($width) || !is_int($height) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    59
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    60
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    61
  if ( !file_exists($in_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    62
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    63
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    64
  if ( preg_match('/["\'\/\\]/', $in_file) || preg_match('/["\'\/\\]/', $out_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    65
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    66
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    67
  if ( file_exists($out_file) && !$unlink )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    68
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    69
  else if ( file_exists($out_file) && $unlink )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    70
    @unlink($out_file);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    71
  if ( file_exists($out_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    72
    // couldn't unlink (delete) the output file
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    73
    return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    74
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    75
  $file_ext = substr($in_file, ( strrpos($in_file, '.') + 1));
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    76
  switch($file_ext)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    77
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    78
    case 'png':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    79
      $func = 'imagecreatefrompng';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    80
      break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    81
    case 'jpg':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    82
    case 'jpeg':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    83
      $func = 'imagecreatefromjpeg';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    84
      break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    85
    case 'gif':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    86
      $func = 'imagecreatefromgif';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    87
      break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    88
    case 'xpm':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    89
      $func = 'imagecreatefromxpm';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    90
      break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    91
    default:
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    92
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    93
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    94
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    95
  // try to find convert in the PATH
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    96
  // FIXME: unix specific (won't work on windows)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    97
  $path = ( isset($_ENV['PATH']) ) ? $_ENV['PATH'] : ( isset($_SERVER['PATH']) ? $_SERVER['PATH'] : '/usr/local/bin:/usr/bin:/bin' );
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    98
  $path = explode(':', $path);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
    99
  foreach ( $path as $dir )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   100
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   101
    if ( file_exists("$dir/convert") && is_executable("$dir/convert") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   102
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   103
      $magick_path = "$dir/convert";
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   104
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   105
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   106
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   107
  $can_use_magick = isset($magick_path);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   108
  $can_use_gd = (
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   109
      function_exists('getimagesize')         &&
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   110
      function_exists('imagecreatetruecolor') &&
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   111
      function_exists('imagecopyresampled')   &&
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   112
      function_exists($func)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   113
    );
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   114
  if ( $can_use_magick )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   115
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   116
    if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   117
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   118
      // ImageMagick path seems screwy
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   119
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   120
    }
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: 25
diff changeset
   121
    $op = ( $preserve_ratio ) ? '>' : '!';
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   122
    $cmdline = "\"$magick_path\" \"$in_file\" -resize '{$width}x{$height}$op' \"$out_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: 25
diff changeset
   123
    exec($cmdline, $output, $return);
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   124
    if ( !file_exists($out_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   125
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   126
    return true;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   127
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   128
  else if ( $can_use_gd )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   129
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   130
    @list($width_orig, $height_orig) = @getimagesize($in_file);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   131
    if ( !$width_orig || !$height_orig )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   132
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   133
    // calculate new width and height
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   134
    
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: 25
diff changeset
   135
    if ( $preserve_ratio )
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   136
    {
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: 25
diff changeset
   137
      $ratio = $width_orig / $height_orig;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   138
      if ( $ratio > 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: 25
diff changeset
   139
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   140
        // orig. width is greater that 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: 25
diff changeset
   141
        $new_width = $width;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   142
        $new_height = round( $width / $ratio );
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   143
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   144
      else if ( $ratio < 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: 25
diff changeset
   145
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   146
        // orig. height is greater than width
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   147
        $new_width = round( $height / $ratio );
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   148
        $new_height = $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: 25
diff changeset
   149
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   150
      else if ( $ratio == 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: 25
diff changeset
   151
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   152
        $new_width = $width;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   153
        $new_height = $width;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   154
      }
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   155
      if ( $new_width > $width_orig || $new_height > $height_orig )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   156
      {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   157
        // Too big for our britches here; set it to only convert the 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: 25
diff changeset
   158
        $new_width = $width_orig;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   159
        $new_height = $height_orig;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 25
diff changeset
   160
      }
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   161
    }
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: 25
diff changeset
   162
    else
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   163
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   164
      $new_width = $width;
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: 25
diff changeset
   165
      $new_height = $height;
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   166
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   167
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   168
    $newimage = @imagecreatetruecolor($new_width, $new_height);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   169
    if ( !$newimage )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   170
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   171
    $oldimage = @$func($in_file);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   172
    if ( !$oldimage )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   173
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   174
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   175
    // Perform scaling
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   176
    imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   177
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   178
    // Get output format
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   179
    $out_ext = substr($out_file, ( strrpos($out_file, '.') + 1));
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   180
    switch($out_ext)
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   181
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   182
      case 'png':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   183
        $outfunc = 'imagepng';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   184
        break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   185
      case 'jpg':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   186
      case 'jpeg':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   187
        $outfunc = 'imagejpeg';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   188
        break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   189
      case 'gif':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   190
        $outfunc = 'imagegif';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   191
        break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   192
      case 'xpm':
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   193
        $outfunc = 'imagexpm';
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   194
        break;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   195
      default:
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   196
        imagedestroy($newimage);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   197
        imagedestroy($oldimage);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   198
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   199
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   200
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   201
    // Write output
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   202
    $outfunc($newimage, $out_file);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   203
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   204
    // clean up
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   205
    imagedestroy($newimage);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   206
    imagedestroy($oldimage);
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   207
    
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   208
    // done!
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   209
    return true;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   210
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   211
  if ( file_exists($out_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   212
    return true;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   213
  return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   214
}
5c377ceb0e4c Added initial album artwork support.
Dan
parents:
diff changeset
   215