Changeset 136 for trunk/lib/ImageThumb.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ImageThumb.inc.php
r121 r136 2 2 /** 3 3 * ImageThumb.inc.php 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information 5 5 * 6 6 * @author Quinn Comendant <quinn@strangecode.com> … … 95 95 function setParam($params) 96 96 { 97 $app =& App::getInstance(); 98 97 99 if (isset($params) && is_array($params)) { 98 100 … … 102 104 // Must be directory. 103 105 if (!is_dir($params['source_dir'])) { 104 App::logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__);106 $app->logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); 105 107 trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR); 106 108 } 107 109 // Must be readable. 108 110 if (!is_readable($params['source_dir'])) { 109 App::logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__);111 $app->logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); 110 112 trigger_error(sprintf('Source directory not readable: %s', $params['source_dir']), E_USER_ERROR); 111 113 } … … 115 117 $this->_params = array_merge($this->_params, $params); 116 118 } else { 117 App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);119 $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); 118 120 } 119 121 } … … 128 130 function getParam($param) 129 131 { 132 $app =& App::getInstance(); 133 130 134 if (isset($this->_params[$param])) { 131 135 return $this->_params[$param]; 132 136 } else { 133 App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);137 $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); 134 138 return null; 135 139 } … … 144 148 function setSpec($spec, $index=null) 145 149 { 150 $app =& App::getInstance(); 151 146 152 // A little sanity checking. 147 153 if (!isset($spec['dest_dir']) || '' == $spec['dest_dir']) { 148 App::logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__);154 $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); 149 155 } 150 156 if (isset($spec['dest_file_type'])) { … … 152 158 case IMG_JPG : 153 159 if (imagetypes() & IMG_JPG == 0) { 154 App::logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);160 $app->logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 155 161 } 156 162 $spec['dest_file_extention'] = 'jpg'; … … 158 164 case IMG_PNG : 159 165 if (imagetypes() & IMG_PNG == 0) { 160 App::logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);166 $app->logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 161 167 } 162 168 $spec['dest_file_extention'] = 'png'; … … 164 170 case IMG_GIF : 165 171 if (imagetypes() & IMG_GIF == 0) { 166 App::logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);172 $app->logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 167 173 } 168 174 $spec['dest_file_extention'] = 'gif'; … … 170 176 case IMG_WBMP : 171 177 if (imagetypes() & IMG_WBMP == 0) { 172 App::logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);178 $app->logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 173 179 } 174 180 $spec['dest_file_extention'] = 'wbmp'; 175 181 break; 176 182 default : 177 App::logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__);183 $app->logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__); 178 184 break; 179 185 } 180 186 } 181 187 if (!isset($spec['width']) || !is_int($spec['width'])) { 182 App::logMsg('setSpec error: width not specified.', LOG_ERR, __FILE__, __LINE__);188 $app->logMsg('setSpec error: width not specified.', LOG_ERR, __FILE__, __LINE__); 183 189 } 184 190 if (!isset($spec['height']) || !is_int($spec['height'])) { 185 App::logMsg('setSpec error: height not specified.', LOG_ERR, __FILE__, __LINE__);191 $app->logMsg('setSpec error: height not specified.', LOG_ERR, __FILE__, __LINE__); 186 192 } 187 193 if (isset($spec['quality']) && IMG_JPG != $spec['dest_file_type']) { 188 App::logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__);194 $app->logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); 189 195 } 190 196 if (isset($spec['progressive']) && IMG_JPG != $spec['dest_file_type']) { 191 App::logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__);197 $app->logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); 192 198 } 193 199 … … 213 219 function processAll($runtime_specs=null) 214 220 { 221 $app =& App::getInstance(); 222 215 223 // Ensure we have a source. 216 224 if ('' == $this->getParam('source_dir')) { 217 App::logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);225 $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 218 226 return false; 219 227 } … … 237 245 return 0 === $return_val; 238 246 } else { 239 App::logMsg(sprintf('No images found to thumbnail in directory %s.', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__);247 $app->logMsg(sprintf('No images found to thumbnail in directory %s.', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__); 240 248 return false; 241 249 } … … 252 260 function processFile($file_name, $runtime_specs=null) 253 261 { 262 $app =& App::getInstance(); 263 254 264 // Source file determinted by provided file_name. 255 265 $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); … … 260 270 $this->setSpec($runtime_specs, 0); 261 271 } else { 262 App::logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__);272 $app->logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__); 263 273 return false; 264 274 } … … 267 277 // Ensure we have a source. 268 278 if ('' == $this->getParam('source_dir')) { 269 App::logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);279 $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 270 280 return false; 271 281 } … … 274 284 if (!file_exists($source_file)) { 275 285 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s was not found."), $file_name), MSG_ERR, __FILE__, __LINE__); 276 App::logMsg(sprintf('Source image not found: %s', $source_file), LOG_ALERT, __FILE__, __LINE__);286 $app->logMsg(sprintf('Source image not found: %s', $source_file), LOG_ALERT, __FILE__, __LINE__); 277 287 return false; 278 288 } … … 281 291 if (!is_readable($source_file)) { 282 292 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__); 283 App::logMsg(sprintf('Source image not readable: %s', $source_file), LOG_ALERT, __FILE__, __LINE__);293 $app->logMsg(sprintf('Source image not readable: %s', $source_file), LOG_ALERT, __FILE__, __LINE__); 284 294 return false; 285 295 } … … 288 298 if (filesize($source_file) <= 0) { 289 299 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); 290 App::logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_ALERT, __FILE__, __LINE__);300 $app->logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_ALERT, __FILE__, __LINE__); 291 301 return false; 292 302 } … … 295 305 if (!$this->_validFileExtension($file_name)) { 296 306 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s not a valid type. It must have one of the following file name extensions: %s"), $file_name, join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__); 297 App::logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $source_file), LOG_ERR, __FILE__, __LINE__);307 $app->logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $source_file), LOG_ERR, __FILE__, __LINE__); 298 308 return false; 299 309 } … … 324 334 $file_size = filesize($dest_file); 325 335 if (false !== $file_size && $file_size < $spec['keep_filesize']) { 326 App::logMsg(sprintf('Skipping thumbnail %s. File already exists and file size is less than %s bytes.', $spec['dest_dir'] . '/' . $file_name, $spec['keep_filesize']), LOG_DEBUG, __FILE__, __LINE__);336 $app->logMsg(sprintf('Skipping thumbnail %s. File already exists and file size is less than %s bytes.', $spec['dest_dir'] . '/' . $file_name, $spec['keep_filesize']), LOG_DEBUG, __FILE__, __LINE__); 327 337 continue; 328 338 } … … 333 343 if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) { 334 344 $spec['scaling_type'] = IMAGETHUMB_NO_SCALE; 335 App::logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, $spec['dest_dir']), LOG_DEBUG, __FILE__, __LINE__);345 $app->logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, $spec['dest_dir']), LOG_DEBUG, __FILE__, __LINE__); 336 346 } 337 347 … … 339 349 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'))) { 340 350 // Resize using Netpbm binaries. 341 App::logMsg(sprintf('Resizing with Netpbm...', null), LOG_DEBUG, __FILE__, __LINE__);351 $app->logMsg(sprintf('Resizing with Netpbm...', null), LOG_DEBUG, __FILE__, __LINE__); 342 352 $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec); 343 353 } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) { 344 354 // Resize with GD. 345 App::logMsg(sprintf('Resizing with GD...', null), LOG_DEBUG, __FILE__, __LINE__);355 $app->logMsg(sprintf('Resizing with GD...', null), LOG_DEBUG, __FILE__, __LINE__); 346 356 $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec); 347 357 } else { 348 App::logMsg(sprintf('Image thumbnailing failed. Neither Netpbm or GD is available.', null), LOG_DEBUG, __FILE__, __LINE__);358 $app->logMsg(sprintf('Image thumbnailing failed. Neither Netpbm or GD is available.', null), LOG_DEBUG, __FILE__, __LINE__); 349 359 return false; 350 360 } … … 370 380 function _resizeWithNetpbm($source_file, $dest_file, $spec) 371 381 { 382 $app =& App::getInstance(); 383 372 384 // Define pnmscale arguments. 373 385 switch ($spec['scaling_type']) { … … 405 417 escapeshellcmd($file_name) 406 418 ); 407 App::logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__);419 $app->logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__); 408 420 409 421 // Execute! … … 414 426 // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. 415 427 chmod($dest_file, $this->getParam('dest_file_perms')); 416 App::logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__);428 $app->logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__); 417 429 } else { 418 430 // An error occurred. 419 App::logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__);431 $app->logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__); 420 432 } 421 433 … … 438 450 function _resizeWithGD($source_file, $dest_file, $spec) 439 451 { 452 $app =& App::getInstance(); 453 440 454 // Get original file dimensions and type. 441 455 list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_file); … … 487 501 $source_image_resource = imagecreatefromwbmp($source_file); 488 502 default : 489 App::logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__);503 $app->logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__); 490 504 return 1; 491 505 break; 492 506 } 493 507 if (!$source_image_resource) { 494 App::logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__);508 $app->logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__); 495 509 return 1; 496 510 } … … 501 515 // Resample! 502 516 if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) { 503 App::logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__);517 $app->logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 504 518 return 1; 505 519 } … … 528 542 break; 529 543 default : 530 App::logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__);544 $app->logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__); 531 545 return 1; 532 546 break; … … 537 551 // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. 538 552 chmod($dest_file, $this->getParam('dest_file_perms')); 539 App::logMsg(sprintf('Successfully resized image %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__);553 $app->logMsg(sprintf('Successfully resized image %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); 540 554 return 0; 541 555 } else { 542 556 // An error occurred. 543 App::logMsg(sprintf('Image %s failed resizing.', $dest_file), LOG_ERR, __FILE__, __LINE__);557 $app->logMsg(sprintf('Image %s failed resizing.', $dest_file), LOG_ERR, __FILE__, __LINE__); 544 558 return 1; 545 559 } … … 555 569 function deleteThumbs($file_name) 556 570 { 571 $app =& App::getInstance(); 572 557 573 // Ensure we have a source. 558 574 if ('' == $this->getParam('source_dir')) { 559 App::logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);575 $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 560 576 return false; 561 577 } … … 567 583 if (!unlink($dest_file)) { 568 584 $return_val++; 569 App::logMsg(sprintf(_("Delete thumbs failed: %s"), $dest_file), LOG_WARNING, __FILE__, __LINE__);585 $app->logMsg(sprintf(_("Delete thumbs failed: %s"), $dest_file), LOG_WARNING, __FILE__, __LINE__); 570 586 } 571 587 } … … 584 600 function deleteOriginal($file_name) 585 601 { 602 $app =& App::getInstance(); 603 586 604 // Ensure we have a source. 587 605 if ('' == $this->getParam('source_dir')) { 588 App::logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);606 $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 589 607 return false; 590 608 } … … 592 610 $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); 593 611 if (!unlink($source_file)) { 594 App::logMsg(sprintf(_("Delete original failed: %s"), $source_file), LOG_WARNING, __FILE__, __LINE__);612 $app->logMsg(sprintf(_("Delete original failed: %s"), $source_file), LOG_WARNING, __FILE__, __LINE__); 595 613 return false; 596 614 } … … 608 626 function exists($file_name) 609 627 { 628 $app =& App::getInstance(); 629 610 630 // Ensure we have a source. 611 631 if ('' == $this->getParam('source_dir')) { 612 App::logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);632 $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 613 633 return false; 614 634 } … … 639 659 function _createDestDirs() 640 660 { 661 $app =& App::getInstance(); 662 641 663 static $already_checked = false; 642 664 … … 644 666 // Ensure we have a source. 645 667 if ('' == $this->getParam('source_dir')) { 646 App::logMsg(sprintf('Source directory not set before creating destination directories.'), LOG_ERR, __FILE__, __LINE__);668 $app->logMsg(sprintf('Source directory not set before creating destination directories.'), LOG_ERR, __FILE__, __LINE__); 647 669 return false; 648 670 } … … 654 676 if (!mkdir($this->getParam('source_dir') . '/' . $spec['dest_dir'], $this->getParam('dest_dir_perms'))) { 655 677 $return_val++; 656 App::logMsg(sprintf('mkdir failure: %s', $this->getParam('source_dir') . '/' . $spec['dest_dir']), LOG_ERR, __FILE__, __LINE__);678 $app->logMsg(sprintf('mkdir failure: %s', $this->getParam('source_dir') . '/' . $spec['dest_dir']), LOG_ERR, __FILE__, __LINE__); 657 679 } 658 680 } … … 667 689 668 690 /** 669 * An alias for App::raiseMsg that only sends messages configured by display_messages.691 * An alias for $app->raiseMsg that only sends messages configured by display_messages. 670 692 * 671 693 * @access public … … 679 701 function _raiseMsg($message, $type, $file, $line) 680 702 { 703 $app =& App::getInstance(); 704 681 705 if ($this->getParam('display_messages') === true || (is_int($this->getParam('display_messages')) && $this->getParam('display_messages') & $type > 0)) { 682 App::raiseMsg($message, $type, $file, $line);706 $app->raiseMsg($message, $type, $file, $line); 683 707 } 684 708 }
Note: See TracChangeset
for help on using the changeset viewer.