Show
Ignore:
Timestamp:
03/12/09 09:11:52 (9 months ago)
Author:
fumiakiy
Message:

Merged sockfish to trunk. "svn merge -r3114:3527 http://code.sixapart.com/svn/movabletype/branches/sockfish/ ."

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/php/lib/thumbnail_lib.php

    r3082 r3531  
    11<?php 
    2 # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. 
     2# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd. 
    33# This program is distributed under the terms of the 
    44# GNU General Public License, version 2. 
     
    2929    #   [2] thumbnail width for file name 
    3030    #   [3] thumbnail width for file name 
    31     function _calculate_size ($width, $height, $scale) { 
     31    function _calculate_size ($width, $height, $scale, $square) { 
    3232        # Calculate thumbnail size 
    3333        $thumb_w = $this->src_w; 
     
    4141            $thumb_w_name = $thumb_w; 
    4242            $thumb_h_name = $thumb_h; 
     43        } elseif ($square) { 
     44            if ($width > 0) { 
     45                $thumb_w = $width; 
     46                $thumb_h = $width; 
     47                $thumb_w_name = $width; 
     48                $thumb_h_name = $width; 
     49            } else { 
     50                $thumb_w = $height; 
     51                $thumb_h = $height; 
     52                $thumb_w_name = $height; 
     53                $thumb_h_name = $height; 
     54            } 
    4355        } elseif ($width > 0 || $height > 0) { 
    4456            $thumb_w_name = 'auto'; 
     
    94106 
    95107    # Load or generate a thumbnail. 
    96     function get_thumbnail (&$dest, &$width, &$height, $id, $scale = 0, $format = '%f-thumb-%wx%h-%i%x', $dest_type = 'auto') { 
     108    function get_thumbnail (&$dest, &$width, &$height, $id, $scale = 0, $format = '%f-thumb-%wx%h-%i%x', $dest_type = 'auto', $square = false) { 
    97109        if (empty($this->src_file)) return false; 
    98110        if (!file_exists($this->src_file)) return false; 
     
    126138 
    127139        # Calculate thumbnail size 
    128         list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale); 
     140        list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale, $square); 
    129141        $width = $thumb_w; 
    130142        $height = $thumb_h; 
     
    134146            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type, $id); 
    135147        } 
    136  
    137148        # Generate 
    138149        if(!file_exists($dest)) { 
     
    145156            } 
    146157 
     158            # if square modifier is enable, crop & resize 
     159            $src_x = 0; 
     160            $src_y = 0; 
     161            $target_w = $this->src_w; 
     162            $target_h = $this->src_h; 
     163            if ($square) { 
     164                if ($this->src_w > $this->src_h) { 
     165                    $src_x = (int)($this->src_w - $this->src_h) / 2; 
     166                    $src_y = 0; 
     167                    $target_w = $this->src_h; 
     168                } else { 
     169                    $src_x = 0; 
     170                    $src_y = (int)($this->src_h - $this->src_w) / 2; 
     171                    $target_h = $this->src_w; 
     172                } 
     173            } 
     174 
    147175            # Create thumbnail 
    148176            $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h ); 
    149             $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0, 
    150                     $thumb_w, $thumb_h, $this->src_w, $this->src_h); 
     177            $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, $src_x, $src_y, 
     178                    $thumb_w, $thumb_h, $target_w, $target_h); 
    151179 
    152180            $output = $this->src_type;