Changeset 42 for trunk/lib/ImageThumb.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ImageThumb.inc.php
r41 r42 1 1 <?php 2 2 /** 3 * ImageThumb.inc.php 3 * ImageThumb.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 5 */ … … 20 20 21 21 class ImageThumb { 22 22 23 23 // The location for images to create thumbnails from. 24 24 var $source_dir = null; 25 25 26 26 // Specifications for thumbnail images. 27 var $spec; 28 27 var $spec; 28 29 29 // Array of acceptable file extensions (lowercase). 30 30 var $valid_file_extensions = array('jpg', 'jpeg', 'gif', 'png'); 31 32 // The uploaded files will be owned by user 'apache'. Set world-read/write 31 32 // The uploaded files will be owned by user 'apache'. Set world-read/write 33 33 // if the website admin needs to read/delete these files. Must be at least 0400 with owner=apache. 34 34 var $dest_file_perms = 0644; 35 35 36 36 // Must be at least 0700 with owner=apache. 37 37 var $dest_dir_perms = 0777; … … 42 42 var $cjpeg_binary = '/usr/bin/cjpeg'; 43 43 var $_valid_binaries = true; 44 44 45 45 // Display messages raised in this object? 46 46 var $display_messages = true; … … 76 76 function setSourceDirectory($source_dir) 77 77 { 78 78 79 79 // Set the source directory path, stripping any extra slashes if needed. 80 80 $this->source_dir = preg_replace('!/+$!', '', $source_dir); 81 81 82 82 if (!is_dir($this->source_dir)) { 83 83 App::logMsg(sprintf('ImageThumb error: source directory not found: %s', $this->source_dir), LOG_ERR, __FILE__, __LINE__); … … 108 108 $allow_upscaling = isset($spec['allow_upscaling']) ? $spec['allow_upscaling'] : false; 109 109 $keep_filesize = isset($spec['keep_filesize']) ? $spec['keep_filesize'] : null; 110 110 111 111 // Define pnmscale arguments. 112 112 switch ($scaling_type) { … … 144 144 break; 145 145 } 146 146 147 147 // Define cjpeg arguments. 148 148 $cjpeg_args = sprintf(' -optimize -quality %s ', escapeshellcmd($quality)); 149 149 $cjpeg_args .= (true === $progressive) ? ' -progressive ' : ''; 150 150 151 151 $this->spec[] = array( 152 152 'dest_dir' => $dest_dir, … … 185 185 } 186 186 } 187 187 188 188 // If > 0, there was a problem creating dest dirs. 189 189 return (0 == $return_val); … … 203 203 return false; 204 204 } 205 205 206 206 // Ensure we have a source. 207 207 if (!isset($this->source_dir)) { … … 209 209 return false; 210 210 } 211 211 212 212 // To keep this script running even if user tries to stop browser. 213 ignore_user_abort(true); 214 if (!ini_get('safe_mode')) { 215 set_time_limit(300); 216 } 217 213 ignore_user_abort(true); 214 if (!ini_get('safe_mode')) { 215 set_time_limit(300); 216 } 217 218 218 // Confirm source image exists. 219 219 if (!file_exists($this->source_dir . '/' . $file_name)) { … … 222 222 return false; 223 223 } 224 224 225 225 // Confirm source image is readable. 226 226 if (!is_readable($this->source_dir . '/' . $file_name)) { … … 229 229 return false; 230 230 } 231 231 232 232 // Confirm source image contains data. 233 233 if (filesize($this->source_dir . '/' . $file_name) < 1) { … … 236 236 return false; 237 237 } 238 238 239 239 // Confirm source image has a valid file extension. 240 240 if (!$this->validFileExtension($file_name)) { … … 243 243 return false; 244 244 } 245 245 246 246 // Output file will be a jpg. Set file extension. 247 247 $file_name = substr($file_name, 0, strrpos($file_name, '.')) . '.jpg'; … … 249 249 // This remains zero until something goes wrong. 250 250 $final_return_val = 0; 251 251 252 252 foreach ($this->spec as $s) { 253 253 254 254 // Skip existing thumbnails with file size below $s['keep_filesize']. 255 255 if (file_exists(realpath($this->source_dir . '/' . $s['dest_dir'] . '/' . $file_name)) && isset($s['keep_filesize'])) { … … 260 260 } 261 261 } 262 262 263 263 // Determine if original file size is smaller than specified thumbnail size. Do not scale-up if allow_upscaling config is set to false. 264 264 $image_size = getimagesize(realpath($this->source_dir . '/' . $file_name)); … … 269 269 $pnmscale_args = $s['pnmscale_args']; 270 270 } 271 271 272 272 // Execute the command that creates the thumbnail. 273 273 $command = sprintf('%s %s/%s | %s %s | %s %s > %s/%s', … … 278 278 escapeshellcmd($pnmscale_args), 279 279 escapeshellcmd($this->cjpeg_binary), 280 escapeshellcmd($s['cjpeg_args']), 280 escapeshellcmd($s['cjpeg_args']), 281 281 escapeshellcmd(realpath($this->source_dir . '/' . $s['dest_dir'])), 282 282 escapeshellcmd($file_name) … … 292 292 App::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__); 293 293 } 294 294 295 295 // Return from the command will be > 0 if there was an error. 296 296 $final_return_val += $return_val; 297 297 } 298 298 299 299 // If > 0, there was a problem thumbnailing. 300 300 return (0 == $final_return_val); … … 314 314 return false; 315 315 } 316 316 317 317 // Get all files in source directory. 318 318 $dir_handle = opendir($this->source_dir); … … 323 323 } 324 324 } 325 325 326 326 // Process each found file. 327 327 if (is_array($files) && !empty($files)) { … … 350 350 return false; 351 351 } 352 352 353 353 $ret = 0; 354 354 foreach ($this->spec as $s) { … … 379 379 return false; 380 380 } 381 381 382 382 $file_path_name = $this->source_dir . '/' . $file_name; 383 383 if (!unlink($file_path_name)) { … … 388 388 return true; 389 389 } 390 390 391 391 /** 392 392 * Returns true if file exists. … … 403 403 return false; 404 404 } 405 405 406 406 return file_exists($this->source_dir . '/' . $file_name); 407 407 } 408 408 409 409 /** 410 410 * Tests if extention of $file_name is in the array valid_file_extensions. … … 419 419 return in_array(strtolower($ext[1]), $this->valid_file_extensions); 420 420 } 421 421 422 422 /** 423 423 * An alias for App::raiseMsg that only sends messages if display_messages is true.
Note: See TracChangeset
for help on using the changeset viewer.