root/trunk/php/lib/thumbnail_lib.php @ 3082

Revision 3082, 5.5 kB (checked in by bchoate, 14 months ago)

Merging fireball branch changes to-date to trunk: svn merge -r2974:3081 http://code.sixapart.com/svn/movabletype/branches/fireball .

  • 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    # Can we use function of GD?
21    function is_available () {
22        return extension_loaded('gd');
23    }
24
25    # Calculate image size
26    # This function returns array object.
27    #   [0] thumbnail width
28    #   [1] thumbnail height
29    #   [2] thumbnail width for file name
30    #   [3] thumbnail width for file name
31    function _calculate_size ($width, $height, $scale) {
32        # Calculate thumbnail size
33        $thumb_w = $this->src_w;
34        $thumb_h = $this->src_h;
35        $thumb_w_name = $this->src_w;
36        $thumb_h_name = $this->src_h;
37
38        if ($scale > 0) {
39            $thumb_w = $this->src_w * $scale / 100;
40            $thumb_h = $this->src_h * $scale / 100;
41            $thumb_w_name = $thumb_w;
42            $thumb_h_name = $thumb_h;
43        } elseif ($width > 0 || $height > 0) {
44            $thumb_w_name = 'auto';
45            $thumb_h_name = 'auto';
46            $x = $width; if ($width > 0) $thumb_w;
47            $y = $height; if ($height > 0) $thumb_h;
48            $pct = $width > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
49            $thumb_w = (int)($thumb_w * $pct);
50            $thumb_h = (int)($thumb_h * $pct);
51            if ($width > 0) $thumb_w_name = $width;
52            if ($height > 0) $thumb_h_name = $height;
53        }
54
55        return array($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name);
56    }
57
58    function _make_dest_name ($w, $h, $format, $dest_type, $id) {
59        $output = $this->src_type;
60        if ($dest_type != 'auto') {
61            $output = strtolower($dest_type) == 'gif' ? 1
62              : strtolower($dest_type) == 'jpeg' ? 2
63              : strtolower($dest_type) == 'png' ? 3
64              : $src_type;
65        }
66        switch($output) {
67        case 1: #GIF
68            $ext = '.gif';
69            break;
70        case 2: #JPEG
71            $ext = '.jpg';
72            break;
73        case 3: #PNG
74            $ext = '.png';
75            break;
76        }
77
78        $pathinfo = pathinfo($this->src_file);
79        $basename = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
80
81        $patterns[0] = '/%w/';
82        $patterns[1] = '/%h/';
83        $patterns[2] = '/%f/';
84        $patterns[3] = '/%i/';
85        $patterns[4] = '/%x/';
86        $replacement[0] = $w;
87        $replacement[1] = $h;
88        $replacement[2] = $basename;
89        $replacement[3] = $id;
90        $replacement[4] = $ext;
91
92        return preg_replace($patterns, $replacement, $format);
93    }
94
95    # 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') {
97        if (empty($this->src_file)) return false;
98        if (!file_exists($this->src_file)) return false;
99        if (!$this->is_available()) {
100            global $mt;
101            $mt->warning_log($mt->translate('GD support has not been available. Please install GD support.'));
102            return false;
103        }
104
105        # Get source image information
106        list($this->src_w, $this->src_h, $this->src_type, $src_attr) = getimagesize($this->src_file);
107
108        # Load source image
109        $src_img;
110        switch($this->src_type) {
111        case 1: #GIF
112            $src_img = @imagecreatefromgif($this->src_file);
113            break;
114        case 2: #JPEG
115            $src_img = @imagecreatefromjpeg($this->src_file);
116            break;
117        case 3: #PNG
118            $src_img = @imagecreatefrompng($this->src_file);
119            break;
120        default: #Unsupported format
121            return false;
122        }
123        if (empty($src_img)) {
124            return false;
125        }
126
127        # Calculate thumbnail size
128        list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale);
129        $width = $thumb_w;
130        $height = $thumb_h;
131
132        # Decide a destination file name
133        if (empty($dest)) {
134            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type, $id);
135        }
136
137        # Generate
138        if(!file_exists($dest)) {
139            $dir_name = dirname($dest);
140            if (!file_exists($dir_name))
141                mkpath($dir_name, 0777);
142            if (!is_writable($dir_name)) {
143                imagedestroy($src_img);
144                return false;
145            }
146
147            # Create thumbnail
148            $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);
151
152            $output = $this->src_type;
153            if ($dest_type != 'auto') {
154                $output = strtolower($dest_type) == 'gif' ? 1
155                  : strtolower($dest_type) == 'jpeg' ? 2
156                  : strtolower($dest_type) == 'png' ? 3
157                  : $src_type;
158            }
159            switch($output) {
160            case 1: #GIF
161                imagegif($dst_img, $dest);
162                break;
163            case 2: #JPEG
164                imagejpeg($dst_img, $dest);
165                break;
166            case 3: #PNG
167                imagepng($dst_img, $dest);
168                break;
169            }
170            imagedestroy($dst_img);
171        }
172        imagedestroy($src_img);
173
174        return true;
175    }
176}
177?>
Note: See TracBrowser for help on using the browser.