| 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 | |
|---|
| 8 | class 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) { |
|---|
| 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] = '/%x/'; |
|---|
| 85 | $replacement[0] = $w; |
|---|
| 86 | $replacement[1] = $h; |
|---|
| 87 | $replacement[2] = $basename; |
|---|
| 88 | $replacement[3] = $ext; |
|---|
| 89 | |
|---|
| 90 | return preg_replace($patterns, $replacement, $format); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | # Load or generate a thumbnail. |
|---|
| 94 | function get_thumbnail (&$dest, &$width, &$height, $scale = 0, $format = '%f-thumb-%wx%h%x', $dest_type = 'auto') { |
|---|
| 95 | if (empty($this->src_file)) return false; |
|---|
| 96 | if (!file_exists($this->src_file)) return false; |
|---|
| 97 | if (!$this->is_available()) { |
|---|
| 98 | global $mt; |
|---|
| 99 | $mt->warning_log($mt->translate('GD support has not been available. Please install GD support.')); |
|---|
| 100 | return false; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | # Get source image information |
|---|
| 104 | list($this->src_w, $this->src_h, $this->src_type, $src_attr) = getimagesize($this->src_file); |
|---|
| 105 | |
|---|
| 106 | # Load source image |
|---|
| 107 | $src_img; |
|---|
| 108 | switch($this->src_type) { |
|---|
| 109 | case 1: #GIF |
|---|
| 110 | $src_img = @imagecreatefromgif($this->src_file); |
|---|
| 111 | break; |
|---|
| 112 | case 2: #JPEG |
|---|
| 113 | $src_img = @imagecreatefromjpeg($this->src_file); |
|---|
| 114 | break; |
|---|
| 115 | case 3: #PNG |
|---|
| 116 | $src_img = @imagecreatefrompng($this->src_file); |
|---|
| 117 | break; |
|---|
| 118 | default: #Unsupported format |
|---|
| 119 | return false; |
|---|
| 120 | } |
|---|
| 121 | if (empty($src_img)) { |
|---|
| 122 | return false; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | # Calculate thumbnail size |
|---|
| 126 | list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size($width, $height, $scale); |
|---|
| 127 | $width = $thumb_w; |
|---|
| 128 | $height = $thumb_h; |
|---|
| 129 | |
|---|
| 130 | # Decide a destination file name |
|---|
| 131 | if (empty($dest)) { |
|---|
| 132 | $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | # Generate |
|---|
| 136 | if(!file_exists($dest)) { |
|---|
| 137 | $dir_name = dirname($dest); |
|---|
| 138 | if (!file_exists($dir_name)) |
|---|
| 139 | mkpath($dir_name, 0777); |
|---|
| 140 | if (!is_writable($dir_name)) { |
|---|
| 141 | imagedestroy($src_img); |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | # Create thumbnail |
|---|
| 146 | $dst_img = imagecreatetruecolor ( $thumb_w, $thumb_h ); |
|---|
| 147 | $result = imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0, |
|---|
| 148 | $thumb_w, $thumb_h, $this->src_w, $this->src_h); |
|---|
| 149 | |
|---|
| 150 | $output = $this->src_type; |
|---|
| 151 | if ($dest_type != 'auto') { |
|---|
| 152 | $output = strtolower($dest_type) == 'gif' ? 1 |
|---|
| 153 | : strtolower($dest_type) == 'jpeg' ? 2 |
|---|
| 154 | : strtolower($dest_type) == 'png' ? 3 |
|---|
| 155 | : $src_type; |
|---|
| 156 | } |
|---|
| 157 | switch($output) { |
|---|
| 158 | case 1: #GIF |
|---|
| 159 | imagegif($dst_img, $dest); |
|---|
| 160 | break; |
|---|
| 161 | case 2: #JPEG |
|---|
| 162 | imagejpeg($dst_img, $dest); |
|---|
| 163 | break; |
|---|
| 164 | case 3: #PNG |
|---|
| 165 | imagepng($dst_img, $dest); |
|---|
| 166 | break; |
|---|
| 167 | } |
|---|
| 168 | imagedestroy($dst_img); |
|---|
| 169 | } |
|---|
| 170 | imagedestroy($src_img); |
|---|
| 171 | |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | ?> |
|---|