root/branches/release-40/php/lib/thumbnail_lib.php @ 2580

Revision 2580, 5.0 kB (checked in by takayama, 18 months ago)

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

  • Property svn:keywords set to
    Id
    Date
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8class Thumbnail {
9
10    var $src_file;
11    var $src_w;
12    var $src_h;
13    var $src_type;
14
15    # construct
16    function Thumbnail ($src) {
17        $this->src_file = $src;
18    }
19
20    # Calculate image size
21    # This function returns array object.
22    #   [0] thumbnail width
23    #   [1] thumbnail height
24    #   [2] thumbnail width for file name
25    #   [3] thumbnail width for file name
26    function _calculate_size ($width, $height, $scale) {
27        # Calculate thumbnail size
28        $thumb_w = $this->src_w;
29        $thumb_h = $this->src_h;
30        $thumb_w_name = $this->src_w;
31        $thumb_h_name = $this->src_h;
32
33        if ($scale > 0) {
34            $thumb_w = $this->src_w * $scale / 100;
35            $thumb_h = $this->src_h * $scale / 100;
36            $thumb_w_name = $thumb_w;
37            $thumb_h_name = $thumb_h;
38        } elseif ($width > 0 || $height > 0) {
39            $thumb_w_name = 'auto';
40            $thumb_h_name = 'auto';
41            $x = $width; if ($width > 0) $thumb_w;
42            $y = $height; if ($height > 0) $thumb_h;
43            $pct = $width > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
44            $thumb_w = (int)($thumb_w * $pct);
45            $thumb_h = (int)($thumb_h * $pct);
46            if ($width > 0) $thumb_w_name = $width;
47            if ($height > 0) $thumb_h_name = $height;
48        }
49
50        return array($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name);
51    }
52
53    function _make_dest_name ($w, $h, $format, $dest_type) {
54        $output = $this->src_type;
55        if ($dest_type != 'auto') {
56            $output = strtolower($dest_type) == 'gif' ? 1
57              : strtolower($dest_type) == 'jpeg' ? 2
58              : strtolower($dest_type) == 'png' ? 3
59              : $src_type;
60        }
61        switch($output) {
62        case 1: #GIF
63            $ext = '.gif';
64            break;
65        case 2: #JPEG
66            $ext = '.jpg';
67            break;
68        case 3: #PNG
69            $ext = '.png';
70            break;
71        }
72
73        $pathinfo = pathinfo($this->src_file);
74        $basename = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
75
76        $patterns[0] = '/%w/';
77        $patterns[1] = '/%h/';
78        $patterns[2] = '/%f/';
79        $patterns[3] = '/%x/';
80        $replacement[0] = $w;
81        $replacement[1] = $h;
82        $replacement[2] = $basename;
83        $replacement[3] = $ext;
84
85        return preg_replace($patterns, $replacement, $format);
86    }
87
88    # Load or generate a thumbnail.
89    function get_thumbnail (&$dest, &$width, &$height, $scale = 0, $format = '%f-thumb-%wx%h%x', $dest_type = 'auto') {
90        if (empty($this->src_file)) return false;
91        if (!file_exists($this->src_file)) return false;
92
93        # Get source image information
94        list($this->src_w, $this->src_h, $this->src_type, $src_attr) = getimagesize($this->src_file);
95
96        # Load source image
97        $src_img;
98        switch($this->src_type) {
99        case 1: #GIF
100            $src_img = @imagecreatefromgif($this->src_file);
101            break;
102        case 2: #JPEG
103            $src_img = @imagecreatefromjpeg($this->src_file);
104            break;
105        case 3: #PNG
106            $src_img = @imagecreatefrompng($this->src_file);
107            break;
108        default: #Unsupported format
109            return false;
110        }
111        if (empty($src_img)) {
112            return false;
113        }
114
115        # Calculate thumbnail size
116        list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale);
117        $width = $thumb_w;
118        $height = $thumb_h;
119
120        # Decide a destination file name
121        if (empty($dest)) {
122            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type);
123        }
124
125        # Generate
126        if(!file_exists($dest)) {
127            $dir_name = dirname($dest);
128            if (!file_exists($dir_name))
129                mkpath($dir_name, 0777);
130            if (!is_writable($dir_name)) {
131                imagedestroy($src_img);
132                return false;
133            }
134
135            # Create thumbnail
136            $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h );
137            $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0,
138                    $thumb_w, $thumb_h, $this->src_w, $this->src_h);
139
140            $output = $this->src_type;
141            if ($dest_type != 'auto') {
142                $output = strtolower($dest_type) == 'gif' ? 1
143                  : strtolower($dest_type) == 'jpeg' ? 2
144                  : strtolower($dest_type) == 'png' ? 3
145                  : $src_type;
146            }
147            switch($output) {
148            case 1: #GIF
149                imagegif($dst_img, $dest);
150                break;
151            case 2: #JPEG
152                imagejpeg($dst_img, $dest);
153                break;
154            case 3: #PNG
155                imagepng($dst_img, $dest);
156                break;
157            }
158            imagedestroy($dst_img);
159        }
160        imagedestroy($src_img);
161
162    }
163}
164?>
Note: See TracBrowser for help on using the browser.