[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * ImageThumb.inc.php |
---|
| 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * The ImageThumb class resizes images. |
---|
| 9 | * |
---|
| 10 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 11 | * @requires Netpbm binaries from http://sourceforge.net/projects/netpbm/ |
---|
| 12 | * @version 1.0 |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | require_once dirname(__FILE__) . '/App.inc.php'; |
---|
| 16 | |
---|
| 17 | define('IMAGETHUMB_FIT_WIDTH', 1); |
---|
| 18 | define('IMAGETHUMB_FIT_HEIGHT', 2); |
---|
| 19 | define('IMAGETHUMB_FIT_LARGER', 3); |
---|
| 20 | define('IMAGETHUMB_STRETCH', 4); |
---|
| 21 | define('IMAGETHUMB_NO_SCALE', 5); |
---|
| 22 | |
---|
| 23 | class ImageThumb { |
---|
| 24 | |
---|
| 25 | // The location for images to create thumbnails from. |
---|
| 26 | var $source_dir = null; |
---|
| 27 | |
---|
| 28 | // Specifications for thumbnail images. |
---|
| 29 | var $spec; |
---|
| 30 | |
---|
| 31 | // Array of acceptable file extensions (lowercase). |
---|
| 32 | var $valid_file_extensions = array('jpg', 'jpeg', 'gif', 'png'); |
---|
| 33 | |
---|
| 34 | // The uploaded files will be owned by user 'apache'. Set world-read/write |
---|
| 35 | // if the website admin needs to read/delete these files. Must be at least 0400 with owner=apache. |
---|
| 36 | var $dest_file_perms = 0644; |
---|
| 37 | |
---|
| 38 | // Must be at least 0700 with owner=apache. |
---|
| 39 | var $dest_dir_perms = 0777; |
---|
| 40 | |
---|
| 41 | // Executable binary locations. |
---|
| 42 | var $anytopnm_binary = '/usr/bin/anytopnm'; |
---|
| 43 | var $pnmscale_binary = '/usr/bin/pnmscale'; |
---|
| 44 | var $cjpeg_binary = '/usr/bin/cjpeg'; |
---|
| 45 | var $_valid_binaries = true; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * |
---|
| 49 | */ |
---|
| 50 | function ImageThumb() |
---|
| 51 | { |
---|
| 52 | // if (!file_exists($this->anytopnm_binary)) { |
---|
| 53 | // logMsg(sprintf('ImageThumb error: %s not found.', $this->anytopnm_binary), LOG_ERR, __FILE__, __LINE__); |
---|
| 54 | // $this->_valid_binaries = false; |
---|
| 55 | // return false; |
---|
| 56 | // } |
---|
| 57 | // if (!file_exists($this->pnmscale_binary)) { |
---|
| 58 | // logMsg(sprintf('ImageThumb error: %s not found.', $this->pnmscale_binary), LOG_ERR, __FILE__, __LINE__); |
---|
| 59 | // $this->_valid_binaries = false; |
---|
| 60 | // return false; |
---|
| 61 | // } |
---|
| 62 | // if (!file_exists($this->cjpeg_binary)) { |
---|
| 63 | // logMsg(sprintf('ImageThumb error: %s not found.', $this->cjpeg_binary), LOG_ERR, __FILE__, __LINE__); |
---|
| 64 | // $this->_valid_binaries = false; |
---|
| 65 | // return false; |
---|
| 66 | // } |
---|
| 67 | return true; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * |
---|
| 72 | */ |
---|
| 73 | function setSourceDirectory($source_dir) |
---|
| 74 | { |
---|
| 75 | |
---|
| 76 | // Set the source directory path, stripping any extra slashes if needed. |
---|
| 77 | $this->source_dir = preg_replace('!/+$!', '', $source_dir); |
---|
| 78 | |
---|
| 79 | if (!is_dir($this->source_dir)) { |
---|
| 80 | logMsg(sprintf('ImageThumb error: source directory not found: %s', $this->source_dir), LOG_ERR, __FILE__, __LINE__); |
---|
| 81 | return false; |
---|
| 82 | } |
---|
| 83 | if (!is_readable($this->source_dir)) { |
---|
| 84 | logMsg(sprintf('ImageThumb error: source directory not readable: %s', $this->source_dir), LOG_ERR, __FILE__, __LINE__); |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | return true; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * |
---|
| 92 | */ |
---|
| 93 | function setSpec($spec = array()) |
---|
| 94 | { |
---|
| 95 | $dest_dir = preg_replace('!/+$!', '', $spec['dest_dir']); |
---|
| 96 | $width = $spec['width']; |
---|
| 97 | $height = $spec['height']; |
---|
| 98 | $scaling_type = $spec['scaling_type']; |
---|
| 99 | $quality = isset($spec['quality']) ? $spec['quality'] : 75; |
---|
| 100 | $progressive = isset($spec['progressive']) ? $spec['progressive'] : false; |
---|
| 101 | $allow_upscaling = isset($spec['allow_upscaling']) ? $spec['allow_upscaling'] : false; |
---|
| 102 | $keep_filesize = isset($spec['keep_filesize']) ? $spec['keep_filesize'] : null; |
---|
| 103 | |
---|
| 104 | // Define pnmscale arguments. |
---|
| 105 | switch ($scaling_type) { |
---|
| 106 | case IMAGETHUMB_FIT_WIDTH : |
---|
| 107 | if (empty($width)) { |
---|
| 108 | logMsg('ImageThumb error: width not specified for IMAGETHUMB_FIT_WIDTH.', LOG_ERR, __FILE__, __LINE__); |
---|
| 109 | return false; |
---|
| 110 | } |
---|
| 111 | $pnmscale_args = sprintf(' -width %s ', escapeshellcmd($width)); |
---|
| 112 | break; |
---|
| 113 | case IMAGETHUMB_FIT_HEIGHT : |
---|
| 114 | if (empty($height)) { |
---|
| 115 | logMsg('ImageThumb error: height not specified for IMAGETHUMB_FIT_HEIGHT.', LOG_ERR, __FILE__, __LINE__); |
---|
| 116 | return false; |
---|
| 117 | } |
---|
| 118 | $pnmscale_args = sprintf(' -height %s ', escapeshellcmd($height)); |
---|
| 119 | break; |
---|
| 120 | case IMAGETHUMB_FIT_LARGER : |
---|
| 121 | if (empty($width) || empty($height)) { |
---|
| 122 | logMsg('ImageThumb error: width or height not specified for IMAGETHUMB_FIT_LARGER.', LOG_ERR, __FILE__, __LINE__); |
---|
| 123 | return false; |
---|
| 124 | } |
---|
| 125 | $pnmscale_args = sprintf(' -xysize %s %s ', escapeshellcmd($width), escapeshellcmd($height)); |
---|
| 126 | break; |
---|
| 127 | case IMAGETHUMB_STRETCH : |
---|
| 128 | if (empty($width) || empty($height)) { |
---|
| 129 | logMsg('ImageThumb error: width or height not specified for IMAGETHUMB_STRETCH.', LOG_ERR, __FILE__, __LINE__); |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | $pnmscale_args = sprintf(' -width %s -height %s ', escapeshellcmd($width), escapeshellcmd($height)); |
---|
| 133 | break; |
---|
| 134 | case IMAGETHUMB_NO_SCALE : |
---|
| 135 | default : |
---|
| 136 | $pnmscale_args = ' 1 '; |
---|
| 137 | break; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | // Define cjpeg arguments. |
---|
| 141 | $cjpeg_args = sprintf(' -optimize -quality %s ', escapeshellcmd($quality)); |
---|
| 142 | $cjpeg_args .= (true === $progressive) ? ' -progressive ' : ''; |
---|
| 143 | |
---|
| 144 | $this->spec[] = array( |
---|
| 145 | 'dest_dir' => $dest_dir, |
---|
| 146 | 'width' => $width, |
---|
| 147 | 'height' => $height, |
---|
| 148 | 'scaling_type' => $scaling_type, |
---|
| 149 | 'quality' => $quality, |
---|
| 150 | 'progressive' => $progressive, |
---|
| 151 | 'pnmscale_args' => $pnmscale_args, |
---|
| 152 | 'cjpeg_args' => $cjpeg_args, |
---|
| 153 | 'allow_upscaling' => $allow_upscaling, |
---|
| 154 | 'keep_filesize' => $keep_filesize, |
---|
| 155 | ); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * |
---|
| 160 | */ |
---|
| 161 | function createDestDirs() |
---|
| 162 | { |
---|
| 163 | // Ensure we have a source. |
---|
| 164 | if (!isset($this->source_dir)) { |
---|
| 165 | logMsg(sprintf('Source directory not set before creating destination directories.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 166 | return false; |
---|
| 167 | } |
---|
| 168 | $return_val = 0; |
---|
| 169 | foreach ($this->spec as $s) { |
---|
| 170 | if (!is_dir($this->source_dir . '/' . $s['dest_dir'])) { |
---|
| 171 | if (!mkdir($this->source_dir . '/' . $s['dest_dir'], $this->dest_dir_perms)) { |
---|
| 172 | $return_val += 1; |
---|
| 173 | logMsg(sprintf('mkdir failure: %s', $this->source_dir . '/' . $s['dest_dir']), LOG_ERR, __FILE__, __LINE__); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | // If > 0, there was a problem creating dest dirs. |
---|
| 179 | return (0 == $return_val); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * |
---|
| 184 | */ |
---|
| 185 | function processFile($file_name) |
---|
| 186 | { |
---|
| 187 | // Ensure we have valid binaries. |
---|
| 188 | if (!$this->_valid_binaries) { |
---|
| 189 | logMsg(sprintf('Netpnm binaries invalid.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // Ensure we have a source. |
---|
| 194 | if (!isset($this->source_dir)) { |
---|
| 195 | logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 196 | return false; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // To keep this script running even if user tries to stop browser. |
---|
| 200 | ignore_user_abort(true); |
---|
| 201 | if (!ini_get('safe_mode')) { |
---|
| 202 | set_time_limit(300); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | // Confirm source image exists. |
---|
| 206 | if (!file_exists($this->source_dir . '/' . $file_name)) { |
---|
| 207 | // raiseMsg(sprintf(_("Image resizing failed: source image not found: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 208 | logMsg(sprintf('Source image not found: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); |
---|
| 209 | return false; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // Confirm source image is readable. |
---|
| 213 | if (!is_readable($this->source_dir . '/' . $file_name)) { |
---|
| 214 | // raiseMsg(sprintf(_("Image resizing failed: source image not readable: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 215 | logMsg(sprintf('Source image not readable: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); |
---|
| 216 | return false; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | // Confirm source image contains data. |
---|
| 220 | if (filesize($this->source_dir . '/' . $file_name) < 1) { |
---|
| 221 | // raiseMsg(sprintf(_("Image resizing failed: source image corrupt: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 222 | logMsg(sprintf('Source image is zero bytes: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); |
---|
| 223 | return false; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | // Confirm source image has a valid file extension. |
---|
| 227 | if (!$this->validFileExtension($file_name)) { |
---|
| 228 | // raiseMsg(sprintf(_("Image resizing failed: source image not of valid type: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 229 | logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $file_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 230 | return false; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // Output file will be a jpg. Set file extension. // FIXME: is there a better way to do this? |
---|
| 234 | preg_match('/.*?\.(\w+)$/i', $file_name, $ext); |
---|
| 235 | if (!in_array(strtolower($ext[1]), array('jpg', 'jpeg'))) { |
---|
| 236 | $file_name = substr($file_name, 0, strrpos($file_name, '.')) . '.jpg'; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // This remains zero until something goes wrong. |
---|
| 240 | $final_return_val = 0; |
---|
| 241 | |
---|
| 242 | foreach ($this->spec as $s) { |
---|
| 243 | |
---|
| 244 | // Skip existing thumbnails with file size below $s['keep_filesize']. |
---|
| 245 | if (file_exists(realpath($this->source_dir . '/' . $s['dest_dir'] . '/' . $file_name)) && isset($s['keep_filesize'])) { |
---|
| 246 | $file_size = filesize(realpath($this->source_dir . '/' . $s['dest_dir'] . '/' . $file_name)); |
---|
| 247 | if ($file_size && $file_size < $s['keep_filesize']) { |
---|
| 248 | logMsg(sprintf('Skipping thumbnail %s. File already exists and file size is less than %s bytes.', $s['dest_dir'] . '/' . $file_name, $s['keep_filesize']), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 249 | continue; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | // Determine if original file size is smaller than specified thumbnail size. Do not scale-up if allow_upscaling config is set to false. |
---|
| 254 | $image_size = getimagesize(realpath($this->source_dir . '/' . $file_name)); |
---|
| 255 | if ($image_size['0'] <= $s['width'] && $image_size['1'] <= $s['height'] && !$s['allow_upscaling']) { |
---|
| 256 | $pnmscale_args = ' 1 '; |
---|
| 257 | logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, $s['dest_dir']), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 258 | } else { |
---|
| 259 | $pnmscale_args = $s['pnmscale_args']; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | // Execute the command that creates the thumbnail. |
---|
| 263 | $command = sprintf('%s %s/%s | %s %s | %s %s > %s/%s', |
---|
| 264 | escapeshellcmd($this->anytopnm_binary), |
---|
| 265 | escapeshellcmd($this->source_dir), |
---|
| 266 | escapeshellcmd($file_name), |
---|
| 267 | escapeshellcmd($this->pnmscale_binary), |
---|
| 268 | escapeshellcmd($pnmscale_args), |
---|
| 269 | escapeshellcmd($this->cjpeg_binary), |
---|
| 270 | escapeshellcmd($s['cjpeg_args']), |
---|
| 271 | escapeshellcmd(realpath($this->source_dir . '/' . $s['dest_dir'])), |
---|
| 272 | escapeshellcmd($file_name) |
---|
| 273 | ); |
---|
| 274 | logMsg(sprintf('ImageThumb command: %s', $command), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 275 | exec($command, $output, $return_val); |
---|
| 276 | |
---|
| 277 | if (0 == $return_val) { |
---|
| 278 | // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. |
---|
| 279 | chmod(realpath($this->source_dir . '/' . $s['dest_dir'] . '/' . $file_name), $this->dest_file_perms); |
---|
| 280 | logMsg(sprintf('Successfully resized image %s', $s['dest_dir'] . '/' . $file_name, $return_val), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 281 | } else { |
---|
| 282 | logMsg(sprintf('Image %s failed resizing with return value: %s%s', $s['dest_dir'] . '/' . $file_name, $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | // Return from the command will be > 0 if there was an error. |
---|
| 286 | $final_return_val += $return_val; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | // If > 0, there was a problem thumbnailing. |
---|
| 290 | return (0 == $final_return_val); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | * |
---|
| 295 | */ |
---|
| 296 | function processAll() |
---|
| 297 | { |
---|
| 298 | // Ensure we have a source. |
---|
| 299 | if (!isset($this->source_dir)) { |
---|
| 300 | logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 301 | return false; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // Get all files in source directory. |
---|
| 305 | $dir_handle = opendir($this->source_dir); |
---|
| 306 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
| 307 | // If the file name does not start with a dot (. or .. or .htaccess). |
---|
| 308 | if (!preg_match('/^\./', $file) && in_array(strtolower(substr($file, strrpos($file, '.') + 1)), $this->valid_file_extensions)) { |
---|
| 309 | $files[] = $file; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | // Process each found file. |
---|
| 314 | if (is_array($files) && !empty($files)) { |
---|
| 315 | foreach ($files as $file_name) { |
---|
| 316 | $this->processFile($file_name); |
---|
| 317 | } |
---|
| 318 | return sizeof($files); |
---|
| 319 | } else { |
---|
| 320 | logMsg(sprintf('No images found to thumbnail in directory %s.', $this->source_dir), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 321 | return 0; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | * |
---|
| 327 | */ |
---|
| 328 | function deleteThumbs($file_name) |
---|
| 329 | { |
---|
| 330 | // Ensure we have a source. |
---|
| 331 | if (!isset($this->source_dir)) { |
---|
| 332 | logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 333 | return false; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | $ret = 0; |
---|
| 337 | foreach ($this->spec as $s) { |
---|
| 338 | $file_path_name = realpath($this->source_dir . '/' . $s['dest_dir'] . '/' . $file_name); |
---|
| 339 | if (file_exists($file_path_name)) { |
---|
| 340 | if (!unlink($file_path_name)) { |
---|
| 341 | $ret++; |
---|
| 342 | logMsg(sprintf(_("Delete thumbs failed: %s"), $file_path_name), LOG_WARNING, __FILE__, __LINE__); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | // raiseMsg(sprintf(_("The thumbnails for file <strong>%s</strong> have been deleted."), $file_name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
| 347 | return (0 == $ret); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | /** |
---|
| 351 | * |
---|
| 352 | */ |
---|
| 353 | function deleteOriginal($file_name) |
---|
| 354 | { |
---|
| 355 | // Ensure we have a source. |
---|
| 356 | if (!isset($this->source_dir)) { |
---|
| 357 | logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 358 | return false; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | $file_path_name = $this->source_dir . '/' . $file_name; |
---|
| 362 | if (!unlink($file_path_name)) { |
---|
| 363 | logMsg(sprintf(_("Delete original failed: %s"), $file_path_name), LOG_WARNING, __FILE__, __LINE__); |
---|
| 364 | return false; |
---|
| 365 | } |
---|
| 366 | // raiseMsg(sprintf(_("The original file <strong>%s</strong> has been deleted."), $file_name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
| 367 | return true; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | /** |
---|
| 371 | * |
---|
| 372 | */ |
---|
| 373 | function exists($file_name) |
---|
| 374 | { |
---|
| 375 | // Ensure we have a source. |
---|
| 376 | if (!isset($this->source_dir)) { |
---|
| 377 | logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 378 | return false; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | return file_exists($this->source_dir . '/' . $file_name); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | * |
---|
| 386 | */ |
---|
| 387 | function validFileExtension($file_name) |
---|
| 388 | { |
---|
| 389 | preg_match('/.*?\.(\w+)$/i', $file_name, $ext); |
---|
| 390 | return in_array(strtolower($ext[1]), $this->valid_file_extensions); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | } // End of class. |
---|
| 394 | ?> |
---|