Changeset 152 for trunk/lib/ImageThumb.inc.php
- Timestamp:
- Jun 7, 2006 5:35:16 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ImageThumb.inc.php
r146 r152 9 9 */ 10 10 11 // Image resizeoptions.11 // Image proprtion options. 12 12 define('IMAGETHUMB_FIT_WIDTH', 1); 13 13 define('IMAGETHUMB_FIT_HEIGHT', 2); … … 15 15 define('IMAGETHUMB_STRETCH', 4); 16 16 define('IMAGETHUMB_NO_SCALE', 5); 17 18 // Image resize options. 17 19 define('IMAGETHUMB_METHOD_NETPBM', 6); 18 20 define('IMAGETHUMB_METHOD_GD', 7); … … 32 34 33 35 // Permissions of autocreated directories. Must be at least 0700 with owner=apache. 34 'dest_dir_perms' => 07 77,36 'dest_dir_perms' => 0700, 35 37 36 38 // Require file to have one of the following file name extentions. … … 50 52 51 53 // Default image size specs. 52 var $default_image_specs = array( 53 // The destination for an image thumbnail size. Path relative to source_dir (eg: ../thumbs). 54 var $_default_image_specs = array( 55 // The destination for an image thumbnail size. 56 // Use initial / to specify absolute paths, leave off to specify a path relative to source_dir (eg: ../thumbs). 54 57 'dest_dir' => null, 55 58 … … 85 88 86 89 // Final specifications for image sizes, set with setSpec(). 87 var $ image_specs = array();90 var $_image_specs = array(); 88 91 89 92 /** … … 99 102 if (isset($params) && is_array($params)) { 100 103 101 // Enforce valid upload_pathparameter.104 // Enforce valid source_dir parameter. 102 105 if (isset($params['source_dir'])) { 103 106 $params['source_dir'] = realpath($params['source_dir']); 104 // Must be directory.107 // Source must be directory. 105 108 if (!is_dir($params['source_dir'])) { 106 $app->logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); 107 trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR); 108 } 109 // Must be readable. 109 $app->logMsg(sprintf('Attempting to auto-create source directory: %s', $params['source_dir']), LOG_NOTICE, __FILE__, __LINE__); 110 if (phpversion() > '5') { 111 // Recursive. 112 mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms'), true); 113 } else { 114 mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms')); 115 } 116 if (!is_dir($params['source_dir'])) { 117 $app->logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); 118 trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR); 119 } 120 } 121 // Source must be readable. 110 122 if (!is_readable($params['source_dir'])) { 111 123 $app->logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); … … 151 163 152 164 // A little sanity checking. 153 if (!isset($spec['dest_dir']) || '' == $spec['dest_dir']) {165 if (!isset($spec['dest_dir']) || '' == trim($spec['dest_dir'])) { 154 166 $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); 167 } else { 168 $spec['dest_dir'] = trim($spec['dest_dir']); 155 169 } 156 170 if (isset($spec['dest_file_type'])) { … … 198 212 } 199 213 200 if (isset($index) && isset($this->image_specs[$index])) { 201 // Merge with previous. 202 $final_spec = array_merge($this->image_specs[$index], $spec); 203 $this->image_specs[$index] = $final_spec; 214 // Add to _image_specs array. 215 if (isset($index) && isset($this->_image_specs[$index])) { 216 // Merge with existing spec if index is provided. 217 $final_spec = array_merge($this->_image_specs[$index], $spec); 218 $this->_image_specs[$index] = $final_spec; 204 219 } else { 205 // Merge with defaults.206 $final_spec = array_merge($this-> default_image_specs, $spec);207 $this-> image_specs[] = $final_spec;220 // Merge with spec defaults. 221 $final_spec = array_merge($this->_default_image_specs, $spec); 222 $this->_image_specs[] = $final_spec; 208 223 } 209 224 … … 245 260 return 0 === $return_val; 246 261 } else { 247 $app->logMsg(sprintf('No images found to thumbnail in directory %s.', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__);262 $app->logMsg(sprintf('No source images found in directory: %s', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__); 248 263 return false; 249 264 } … … 266 281 267 282 // Ensure we have a source. 268 if (sizeof($this-> image_specs) < 1) {283 if (sizeof($this->_image_specs) < 1) { 269 284 if (is_array($runtime_specs)) { 270 285 $this->setSpec($runtime_specs, 0); … … 284 299 if (!file_exists($source_file)) { 285 300 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s was not found."), $file_name), MSG_ERR, __FILE__, __LINE__); 286 $app->logMsg(sprintf('Source image not found: %s', $source_file), LOG_ ALERT, __FILE__, __LINE__);301 $app->logMsg(sprintf('Source image not found: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 287 302 return false; 288 303 } … … 291 306 if (!is_readable($source_file)) { 292 307 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__); 293 $app->logMsg(sprintf('Source image not readable: %s', $source_file), LOG_ ALERT, __FILE__, __LINE__);308 $app->logMsg(sprintf('Source image not readable: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 294 309 return false; 295 310 } … … 298 313 if (filesize($source_file) <= 0) { 299 314 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); 300 $app->logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_ ALERT, __FILE__, __LINE__);315 $app->logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 301 316 return false; 302 317 } … … 310 325 311 326 // Ensure destination directories are created. This will only be called once per page load. 312 $this->_createDestDirs(); 327 if (!$this->_createDestDirs()) { 328 return false; 329 } 313 330 314 331 // To keep this script running even if user tries to stop browser. … … 320 337 $return_val = 0; 321 338 322 foreach ($this-> image_specs as $index => $spec) {339 foreach ($this->_image_specs as $index => $spec) { 323 340 324 341 if (is_array($runtime_specs)) { … … 328 345 329 346 // Destination filename uses the extention defined by dest_file_extention. 330 $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention'])); 347 if ('/' == $spec['dest_dir']{0}) { 348 // Absolute path. 349 $dest_file = sprintf('%s/%s.%s', $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention']); 350 } else { 351 // Relative path. 352 $dest_file = sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention']); 353 } 354 355 // Ensure destination directory exists and is writable. 356 if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { 357 $this->_createDestDirs($dest_file); 358 if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { 359 $app->logMsg(sprintf('Image resizing failed, dest_dir invalid: %s', dirname($dest_file)), LOG_ERR, __FILE__, __LINE__); 360 $return_val++; 361 continue; 362 } 363 } 331 364 332 365 // Skip existing thumbnails with file size below $spec['keep_filesize']. … … 343 376 if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) { 344 377 $spec['scaling_type'] = IMAGETHUMB_NO_SCALE; 345 $app->logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, $spec['dest_dir']), LOG_DEBUG, __FILE__, __LINE__);378 $app->logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, basename($spec['dest_dir'])), LOG_DEBUG, __FILE__, __LINE__); 346 379 } 347 380 … … 349 382 if (IMAGETHUMB_METHOD_NETPBM === $this->getParam('resize_method') && file_exists($this->getParam('anytopnm_binary')) && file_exists($this->getParam('pnmscale_binary')) && file_exists($this->getParam('cjpeg_binary'))) { 350 383 // Resize using Netpbm binaries. 351 $app->logMsg(sprintf('Resizing with Netpbm ...', null), LOG_DEBUG, __FILE__, __LINE__);384 $app->logMsg(sprintf('Resizing with Netpbm: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); 352 385 $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec); 353 386 } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) { 354 387 // Resize with GD. 355 $app->logMsg(sprintf('Resizing with GD ...', null), LOG_DEBUG, __FILE__, __LINE__);388 $app->logMsg(sprintf('Resizing with GD: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); 356 389 $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec); 357 390 } else { 358 $app->logMsg(sprintf('Image thumbnailing failed. Neither Netpbm or GD is available.', null), LOG_DEBUG, __FILE__, __LINE__);391 $app->logMsg(sprintf('Image thumbnailing canceled. Neither Netpbm or GD is available.', null), LOG_ERR, __FILE__, __LINE__); 359 392 return false; 360 393 } 361 394 } 362 395 363 // If > 0, there was a problem thumb -nailing.396 // If > 0, there was a problem thumbnailing. 364 397 return 0 === $return_val; 365 398 } … … 516 549 if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) { 517 550 $app->logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 551 // Always cleanup images from memory. 552 imagedestroy($source_image_resource); 553 imagedestroy($dest_image_resource); 518 554 return 1; 519 555 } … … 543 579 default : 544 580 $app->logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__); 581 // Always cleanup images from memory. 582 imagedestroy($source_image_resource); 583 imagedestroy($dest_image_resource); 545 584 return 1; 546 585 break; 547 586 } 587 588 // Always cleanup images from memory. 589 imagedestroy($source_image_resource); 590 imagedestroy($dest_image_resource); 548 591 549 592 if ($return_val) { 550 593 // Success! 551 594 // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. 552 chmod($dest_file, $this->getParam('dest_file_perms')); 553 $app->logMsg(sprintf('Successfully resized image %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); 595 if (!chmod($dest_file, $this->getParam('dest_file_perms'))) { 596 $app->logMsg(sprintf('chmod failed on file: %s', $dest_file), LOG_ERR, __FILE__, __LINE__); 597 } 598 $app->logMsg(sprintf('Successfully resized image: %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); 554 599 return 0; 555 600 } else { 556 601 // An error occurred. 557 $app->logMsg(sprintf(' Image %s failed resizing.', $dest_file), LOG_ERR, __FILE__, __LINE__);602 $app->logMsg(sprintf('Failed resizing image: %s', $dest_file), LOG_ERR, __FILE__, __LINE__); 558 603 return 1; 559 604 } … … 578 623 579 624 $return_val = 0; 580 foreach ($this->image_specs as $spec) { 581 $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention'])); 625 foreach ($this->_image_specs as $spec) { 626 if ('/' == $spec['dest_dir']{0}) { 627 // Absolute path. 628 $dest_file = realpath(sprintf('%s/%s.%s', $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention'])); 629 } else { 630 // Relative path. 631 $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $spec['dest_file_extention'])); 632 } 582 633 if (file_exists($dest_file)) { 583 634 if (!unlink($dest_file)) { … … 657 708 * @return bool true on success, false on failure. 658 709 */ 659 function _createDestDirs() 660 { 661 $app =& App::getInstance(); 662 663 static $already_checked = false; 664 665 if (!$already_checked) { 710 function _createDestDirs($filename=null) 711 { 712 $app =& App::getInstance(); 713 714 // Keep track of directories we've already created. 715 $dd_hash = md5(isset($filename) ? dirname($filename) : 'none'); 716 static $already_checked = array(); 717 718 $return_val = 0; 719 720 if (!isset($already_checked[$dd_hash])) { 666 721 // Ensure we have a source. 667 722 if ('' == $this->getParam('source_dir')) { … … 671 726 672 727 // Loop through specs and ensure all dirs are created. 673 $return_val = 0; 674 foreach ($this->image_specs as $spec) { 675 if (!file_exists($this->getParam('source_dir') . '/' . $spec['dest_dir'])) { 676 if (!mkdir($this->getParam('source_dir') . '/' . $spec['dest_dir'], $this->getParam('dest_dir_perms'))) { 677 $return_val++; 678 $app->logMsg(sprintf('mkdir failure: %s', $this->getParam('source_dir') . '/' . $spec['dest_dir']), LOG_ERR, __FILE__, __LINE__); 728 foreach ($this->_image_specs as $spec) { 729 if (isset($filename)) { 730 $dest_dir = dirname($filename); 731 } else { 732 if ('/' == $spec['dest_dir']{0}) { 733 // Absolute path. 734 $dest_dir = $spec['dest_dir']; 735 } else { 736 // Relative path. 737 $dest_dir = sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']); 679 738 } 680 739 } 681 } 682 683 // If > 0, there was a problem creating dest dirs. 684 return 0 === $return_val; 685 } 686 687 $already_checked = true; 740 if (!file_exists($dest_dir)) { 741 if (phpversion() > '5' && false) { /// 742 // Recursive. 743 if (! ($ret = mkdir($dest_dir, $this->getParam('dest_dir_perms'), true))) { 744 $return_val++; 745 $app->logMsg(sprintf('mkdir failure: %s', $dest_dir), LOG_ERR, __FILE__, __LINE__); 746 } 747 } else { 748 // Recursive mkdir for php 4. 749 $path = ''; 750 foreach (array_diff(explode('/', $dest_dir), array('')) as $dir) { 751 $path .= '/' . $dir; 752 if (! ($ret = file_exists($path) ? true : mkdir($path, $this->getParam('dest_dir_perms')))) { 753 $return_val++; 754 $app->logMsg(sprintf('mkdir failure: %s', $path), LOG_ERR, __FILE__, __LINE__); 755 break; 756 } 757 } 758 } 759 760 if ($ret) { 761 $app->logMsg(sprintf('mkdir success: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); 762 } 763 } 764 } 765 } 766 767 $already_checked[$dd_hash] = true; 768 769 // If > 0, there was a problem creating dest dirs. 770 return 0 === $return_val; 688 771 } 689 772
Note: See TracChangeset
for help on using the changeset viewer.