Index: /branches/release-40/php/lib/thumbnail_lib.php
===================================================================
--- /branches/release-40/php/lib/thumbnail_lib.php (revision 2580)
+++ /branches/release-40/php/lib/thumbnail_lib.php (revision 2580)
@@ -0,0 +1,164 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+class Thumbnail {
+
+    var $src_file;
+    var $src_w;
+    var $src_h;
+    var $src_type;
+
+    # construct
+    function Thumbnail ($src) {
+        $this->src_file = $src;
+    }
+
+    # Calculate image size
+    # This function returns array object.
+    #   [0] thumbnail width
+    #   [1] thumbnail height
+    #   [2] thumbnail width for file name
+    #   [3] thumbnail width for file name
+    function _calculate_size ($width, $height, $scale) {
+        # Calculate thumbnail size
+        $thumb_w = $this->src_w;
+        $thumb_h = $this->src_h;
+        $thumb_w_name = $this->src_w;
+        $thumb_h_name = $this->src_h;
+
+        if ($scale > 0) {
+            $thumb_w = $this->src_w * $scale / 100;
+            $thumb_h = $this->src_h * $scale / 100;
+            $thumb_w_name = $thumb_w;
+            $thumb_h_name = $thumb_h;
+        } elseif ($width > 0 || $height > 0) {
+            $thumb_w_name = 'auto';
+            $thumb_h_name = 'auto';
+            $x = $width; if ($width > 0) $thumb_w;
+            $y = $height; if ($height > 0) $thumb_h;
+            $pct = $width > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
+            $thumb_w = (int)($thumb_w * $pct);
+            $thumb_h = (int)($thumb_h * $pct);
+            if ($width > 0) $thumb_w_name = $width;
+            if ($height > 0) $thumb_h_name = $height;
+        }
+
+        return array($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name);
+    }
+
+    function _make_dest_name ($w, $h, $format, $dest_type) {
+        $output = $this->src_type;
+        if ($dest_type != 'auto') {
+            $output = strtolower($dest_type) == 'gif' ? 1
+              : strtolower($dest_type) == 'jpeg' ? 2
+              : strtolower($dest_type) == 'png' ? 3
+              : $src_type;
+        }
+        switch($output) {
+        case 1: #GIF
+            $ext = '.gif';
+            break;
+        case 2: #JPEG
+            $ext = '.jpg';
+            break;
+        case 3: #PNG
+            $ext = '.png';
+            break;
+        }
+
+        $pathinfo = pathinfo($this->src_file);
+        $basename = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
+
+        $patterns[0] = '/%w/';
+        $patterns[1] = '/%h/';
+        $patterns[2] = '/%f/';
+        $patterns[3] = '/%x/';
+        $replacement[0] = $w;
+        $replacement[1] = $h;
+        $replacement[2] = $basename;
+        $replacement[3] = $ext;
+
+        return preg_replace($patterns, $replacement, $format);
+    }
+
+    # Load or generate a thumbnail.
+    function get_thumbnail (&$dest, &$width, &$height, $scale = 0, $format = '%f-thumb-%wx%h%x', $dest_type = 'auto') {
+        if (empty($this->src_file)) return false;
+        if (!file_exists($this->src_file)) return false;
+
+        # Get source image information
+        list($this->src_w, $this->src_h, $this->src_type, $src_attr) = getimagesize($this->src_file);
+
+        # Load source image
+        $src_img;
+        switch($this->src_type) {
+        case 1: #GIF
+            $src_img = @imagecreatefromgif($this->src_file);
+            break;
+        case 2: #JPEG
+            $src_img = @imagecreatefromjpeg($this->src_file);
+            break;
+        case 3: #PNG
+            $src_img = @imagecreatefrompng($this->src_file);
+            break;
+        default: #Unsupported format
+            return false;
+        }
+        if (empty($src_img)) {
+            return false;
+        }
+
+        # Calculate thumbnail size
+        list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale);
+        $width = $thumb_w;
+        $height = $thumb_h;
+
+        # Decide a destination file name
+        if (empty($dest)) {
+            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type);
+        }
+
+        # Generate
+        if(!file_exists($dest)) {
+            $dir_name = dirname($dest);
+            if (!file_exists($dir_name))
+                mkpath($dir_name, 0777);
+            if (!is_writable($dir_name)) {
+                imagedestroy($src_img);
+                return false;
+            }
+
+            # Create thumbnail
+            $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h );
+            $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0,
+                    $thumb_w, $thumb_h, $this->src_w, $this->src_h);
+
+            $output = $this->src_type;
+            if ($dest_type != 'auto') {
+                $output = strtolower($dest_type) == 'gif' ? 1
+                  : strtolower($dest_type) == 'jpeg' ? 2
+                  : strtolower($dest_type) == 'png' ? 3
+                  : $src_type;
+            }
+            switch($output) {
+            case 1: #GIF
+                imagegif($dst_img, $dest);
+                break;
+            case 2: #JPEG
+                imagejpeg($dst_img, $dest);
+                break;
+            case 3: #PNG
+                imagepng($dst_img, $dest);
+                break;
+            }
+            imagedestroy($dst_img);
+        }
+        imagedestroy($src_img);
+
+    }
+}
+?>
Index: /branches/release-40/php/lib/MTUtil.php
===================================================================
--- /branches/release-40/php/lib/MTUtil.php (revision 2478)
+++ /branches/release-40/php/lib/MTUtil.php (revision 2580)
@@ -1287,12 +1287,4 @@
     $format = $mt->translate('userpic-[_1]-%wx%h%x', array($author['author_id']));
     $max_dim = $mt->config('UserpicThumbnailSize');
-    $ext = '.' . 'png';
-    $patterns[0] = '/%w/';
-    $patterns[1] = '/%h/';
-    $patterns[2] = '/%x/';
-    $replacement[0] = $max_dim;
-    $replacement[1] = $max_dim;
-    $replacement[2] = $ext;
-    $filename = preg_replace($patterns, $replacement, $format);
 
     # generate thumbnail
@@ -1302,92 +1294,29 @@
     $image_path = $cache_path . DIRECTORY_SEPARATOR . 'userpics';
     $static_file_path = static_file_path().'support';
-    make_thumbnail_file($src_file, $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$filename, $max_dim, $max_dim, 0, 'png');
+
+    require_once('thumbnail_lib.php');
+    $thumb = new Thumbnail($src_file);
+    $thumb_w = $max_dim;
+    $thumb_h = $max_dim;
+    $dest;
+    $thumb_name = $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$format;
+    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name, 'png');
+    $basename = basename($dest);
 
     $static_path = $mt->config('StaticWebPath');
     $static_path = preg_replace('/\/$/', '', $static_path);
     $static_path .= '/support';
-    $url = sprintf("%s/%s/%s", $static_path, $image_path, $filename);
-
+    $url = sprintf("%s/%s/%s", $static_path, $image_path, $basename);
     return $url;
 }
 
+# for compatibility...
 function make_thumbnail_file($src, $dest, $width, $height, $scale = 0, $dest_type = 'auto') {
-    # Get source image information
-    list($src_w, $src_h, $src_type, $src_attr) = getimagesize($src);
-
-    # Load source image
-    $src_img;
-
-    switch($src_type) {
-    case 1: #GIF
-        $src_img = @imagecreatefromgif($src);
-        break;
-    case 2: #JPEG
-        $src_img = @imagecreatefromjpeg($src);
-        break;
-    case 3: #PNG
-        $src_img = @imagecreatefrompng($src);
-        break;
-    default: #Unsupported format
-        return '';
-    }
-
-    if (!$src_img) {
-        return '';
-    }
-
-    # Calculate thumbnail size
-    $thumb_w = $src_w;
-    $thumb_h = $src_h;
-
-    if ($scale > 0) {
-        $thumb_w = $src_w * $scale / 100;
-        $thumb_h = $src_h * $scale / 100;
-    } elseif ($width > 0 || $height > 0) {
-        $x = $width; if ($width > 0) $thumb_w;
-        $y = $height; if ($height > 0) $thumb_h;
-        $pct = $width > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
-        $thumb_w = (int)($thumb_w * $pct);
-        $thumb_h = (int)($thumb_h * $pct);
-    }
-
-    # Generate
-    if(!file_exists($dest)) {
-        $dir_name = dirname($dest);
-        if (!file_exists($dir_name)) {
-          mkpath($dir_name, 0777);
-        }
-        if (!is_writable($dir_name)) {
-            imagedestroy($src_img);
-            return '';
-        }
-
-        # Create thumbnail
-        $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h );
-        $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0,
-                    $thumb_w, $thumb_h, $src_w, $src_h);
-
-        $output = $src_type;
-        if ($dest_type != 'auto') {
-            $output = strtolower($dest_type) == 'gif' ? 1
-              : strtolower($dest_type) == 'jpeg' ? 2
-              : strtolower($dest_type) == 'png' ? 3
-              : $src_type;
-        }
-        switch($output) {
-            case 1: #GIF
-            imagegif($dst_img, $dest);
-            break;
-        case 2: #JPEG
-            imagejpeg($dst_img, $dest);
-            break;
-        case 3: #PNG
-            imagepng($dst_img, $dest);
-            break;
-        }
-        imagedestroy($dst_img);
-    }
-
-    imagedestroy($src_img);
+    require_once('thumbnail_lib.php');
+    $thumb = new Thumbnail($src);
+
+    $thumb_w = $width;
+    $thumb_h = $height;
+    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, null, $dest_type);
 
     return array($thumb_w, $thumb_h);
@@ -1399,25 +1328,4 @@
     $site_path = preg_replace('/\/$/', '', $site_path);
     $filename = asset_path($asset['asset_file_path'], $blog);
-    $name_w = 'auto'; $name_h = 'auto';
-    if ($width > 0)
-        $name_w = $width;
-    if ($height > 0)
-        $name_h = $height;
-
-    # Generate thumbnail file name
-    $basename = basename($asset['asset_file_name'], '.' . $asset['asset_file_ext']);
-    $id = $asset['asset_id'];
-    $ext = '.' . $asset['asset_file_ext'];
-    $patterns[0] = '/%w/';
-    $patterns[1] = '/%h/';
-    $patterns[2] = '/%f/';
-    $patterns[3] = '/%i/';
-    $patterns[4] = '/%x/';
-    $replacement[0] = $name_w;
-    $replacement[1] = $name_h;
-    $replacement[2] = $basename;
-    $replacement[3] = $id;
-    $replacement[4] = $ext;
-    $thumb_filename = preg_replace($patterns, $replacement, $format);
 
     # Retrieve thumbnail
@@ -1429,11 +1337,16 @@
     $date_stamp = format_ts('%Y/%m', $ts, $blog);
     $cache_dir = $site_path . DIRECTORY_SEPARATOR . $cache_path . DIRECTORY_SEPARATOR . $date_stamp . DIRECTORY_SEPARATOR;
-    $thumb_name = $cache_dir . $thumb_filename;
+    $thumb_name = $cache_dir . $format;
  
     # generate thumbnail
-    list ($thumb_w, $thumb_h) = make_thumbnail_file($filename, $thumb_name, $width, $height);
+    require_once('thumbnail_lib.php');
+    $thumb = new Thumbnail($filename);
+    $thumb_w = $width;
+    $thumb_h = $height;
+    $dest;
+    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name);
 
     # make url
-    $basename = basename($thumb_name);
+    $basename = basename($dest);
     $thumb_url = $blog['blog_site_url'] . $cache_path . '/' . $date_stamp . '/' . $basename;
 
