Changeset 2580

Show
Ignore:
Timestamp:
06/16/08 07:59:00 (20 months ago)
Author:
takayama
Message:

Fixed BugId:80060
* Added thumbnail generator class that class can generate same file name (dynamic and static)

Location:
branches/release-40/php/lib
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-40/php/lib/MTUtil.php

    r2478 r2580  
    12871287    $format = $mt->translate('userpic-[_1]-%wx%h%x', array($author['author_id'])); 
    12881288    $max_dim = $mt->config('UserpicThumbnailSize'); 
    1289     $ext = '.' . 'png'; 
    1290     $patterns[0] = '/%w/'; 
    1291     $patterns[1] = '/%h/'; 
    1292     $patterns[2] = '/%x/'; 
    1293     $replacement[0] = $max_dim; 
    1294     $replacement[1] = $max_dim; 
    1295     $replacement[2] = $ext; 
    1296     $filename = preg_replace($patterns, $replacement, $format); 
    12971289 
    12981290    # generate thumbnail 
     
    13021294    $image_path = $cache_path . DIRECTORY_SEPARATOR . 'userpics'; 
    13031295    $static_file_path = static_file_path().'support'; 
    1304     make_thumbnail_file($src_file, $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$filename, $max_dim, $max_dim, 0, 'png'); 
     1296 
     1297    require_once('thumbnail_lib.php'); 
     1298    $thumb = new Thumbnail($src_file); 
     1299    $thumb_w = $max_dim; 
     1300    $thumb_h = $max_dim; 
     1301    $dest; 
     1302    $thumb_name = $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$format; 
     1303    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name, 'png'); 
     1304    $basename = basename($dest); 
    13051305 
    13061306    $static_path = $mt->config('StaticWebPath'); 
    13071307    $static_path = preg_replace('/\/$/', '', $static_path); 
    13081308    $static_path .= '/support'; 
    1309     $url = sprintf("%s/%s/%s", $static_path, $image_path, $filename); 
    1310  
     1309    $url = sprintf("%s/%s/%s", $static_path, $image_path, $basename); 
    13111310    return $url; 
    13121311} 
    13131312 
     1313# for compatibility... 
    13141314function make_thumbnail_file($src, $dest, $width, $height, $scale = 0, $dest_type = 'auto') { 
    1315     # Get source image information 
    1316     list($src_w, $src_h, $src_type, $src_attr) = getimagesize($src); 
    1317  
    1318     # Load source image 
    1319     $src_img; 
    1320  
    1321     switch($src_type) { 
    1322     case 1: #GIF 
    1323         $src_img = @imagecreatefromgif($src); 
    1324         break; 
    1325     case 2: #JPEG 
    1326         $src_img = @imagecreatefromjpeg($src); 
    1327         break; 
    1328     case 3: #PNG 
    1329         $src_img = @imagecreatefrompng($src); 
    1330         break; 
    1331     default: #Unsupported format 
    1332         return ''; 
    1333     } 
    1334  
    1335     if (!$src_img) { 
    1336         return ''; 
    1337     } 
    1338  
    1339     # Calculate thumbnail size 
    1340     $thumb_w = $src_w; 
    1341     $thumb_h = $src_h; 
    1342  
    1343     if ($scale > 0) { 
    1344         $thumb_w = $src_w * $scale / 100; 
    1345         $thumb_h = $src_h * $scale / 100; 
    1346     } elseif ($width > 0 || $height > 0) { 
    1347         $x = $width; if ($width > 0) $thumb_w; 
    1348         $y = $height; if ($height > 0) $thumb_h; 
    1349         $pct = $width > 0 ? ($x / $thumb_w) : ($y / $thumb_h); 
    1350         $thumb_w = (int)($thumb_w * $pct); 
    1351         $thumb_h = (int)($thumb_h * $pct); 
    1352     } 
    1353  
    1354     # Generate 
    1355     if(!file_exists($dest)) { 
    1356         $dir_name = dirname($dest); 
    1357         if (!file_exists($dir_name)) { 
    1358           mkpath($dir_name, 0777); 
    1359         } 
    1360         if (!is_writable($dir_name)) { 
    1361             imagedestroy($src_img); 
    1362             return ''; 
    1363         } 
    1364  
    1365         # Create thumbnail 
    1366         $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h ); 
    1367         $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0, 
    1368                     $thumb_w, $thumb_h, $src_w, $src_h); 
    1369  
    1370         $output = $src_type; 
    1371         if ($dest_type != 'auto') { 
    1372             $output = strtolower($dest_type) == 'gif' ? 1 
    1373               : strtolower($dest_type) == 'jpeg' ? 2 
    1374               : strtolower($dest_type) == 'png' ? 3 
    1375               : $src_type; 
    1376         } 
    1377         switch($output) { 
    1378             case 1: #GIF 
    1379             imagegif($dst_img, $dest); 
    1380             break; 
    1381         case 2: #JPEG 
    1382             imagejpeg($dst_img, $dest); 
    1383             break; 
    1384         case 3: #PNG 
    1385             imagepng($dst_img, $dest); 
    1386             break; 
    1387         } 
    1388         imagedestroy($dst_img); 
    1389     } 
    1390  
    1391     imagedestroy($src_img); 
     1315    require_once('thumbnail_lib.php'); 
     1316    $thumb = new Thumbnail($src); 
     1317 
     1318    $thumb_w = $width; 
     1319    $thumb_h = $height; 
     1320    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, null, $dest_type); 
    13921321 
    13931322    return array($thumb_w, $thumb_h); 
     
    13991328    $site_path = preg_replace('/\/$/', '', $site_path); 
    14001329    $filename = asset_path($asset['asset_file_path'], $blog); 
    1401     $name_w = 'auto'; $name_h = 'auto'; 
    1402     if ($width > 0) 
    1403         $name_w = $width; 
    1404     if ($height > 0) 
    1405         $name_h = $height; 
    1406  
    1407     # Generate thumbnail file name 
    1408     $basename = basename($asset['asset_file_name'], '.' . $asset['asset_file_ext']); 
    1409     $id = $asset['asset_id']; 
    1410     $ext = '.' . $asset['asset_file_ext']; 
    1411     $patterns[0] = '/%w/'; 
    1412     $patterns[1] = '/%h/'; 
    1413     $patterns[2] = '/%f/'; 
    1414     $patterns[3] = '/%i/'; 
    1415     $patterns[4] = '/%x/'; 
    1416     $replacement[0] = $name_w; 
    1417     $replacement[1] = $name_h; 
    1418     $replacement[2] = $basename; 
    1419     $replacement[3] = $id; 
    1420     $replacement[4] = $ext; 
    1421     $thumb_filename = preg_replace($patterns, $replacement, $format); 
    14221330 
    14231331    # Retrieve thumbnail 
     
    14291337    $date_stamp = format_ts('%Y/%m', $ts, $blog); 
    14301338    $cache_dir = $site_path . DIRECTORY_SEPARATOR . $cache_path . DIRECTORY_SEPARATOR . $date_stamp . DIRECTORY_SEPARATOR; 
    1431     $thumb_name = $cache_dir . $thumb_filename; 
     1339    $thumb_name = $cache_dir . $format; 
    14321340  
    14331341    # generate thumbnail 
    1434     list ($thumb_w, $thumb_h) = make_thumbnail_file($filename, $thumb_name, $width, $height); 
     1342    require_once('thumbnail_lib.php'); 
     1343    $thumb = new Thumbnail($filename); 
     1344    $thumb_w = $width; 
     1345    $thumb_h = $height; 
     1346    $dest; 
     1347    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name); 
    14351348 
    14361349    # make url 
    1437     $basename = basename($thumb_name); 
     1350    $basename = basename($dest); 
    14381351    $thumb_url = $blog['blog_site_url'] . $cache_path . '/' . $date_stamp . '/' . $basename; 
    14391352