Changeset 468 for trunk/lib/ImageThumb.inc.php
- Timestamp:
- Feb 20, 2014 3:03:59 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
/branches/eli_branch merged eligible
-
Property
svn:mergeinfo
set to
-
trunk/lib/ImageThumb.inc.php
r396 r468 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 41 41 42 42 class ImageThumb { 43 43 44 44 // General object parameters. 45 var$_params = array(45 private $_params = array( 46 46 // The location for images to create thumbnails from. 47 47 'source_dir' => null, … … 58 58 // Require file to have one of the following file name extensions. 59 59 'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png'), 60 60 61 61 // Method to use for resizing. (IMAGETHUMB_METHOD_NETPBM or IMAGETHUMB_METHOD_GD) 62 62 'resize_method' => IMAGETHUMB_METHOD_NETPBM, … … 70 70 'display_messages' => MSG_ALL, 71 71 ); 72 72 73 73 // Default image size specs. 74 var$_default_image_specs = array(75 // The destination for an image thumbnail size. 74 private $_default_image_specs = array( 75 // The destination for an image thumbnail size. 76 76 // Use initial / to specify absolute paths, leave off to specify a path relative to source_dir (eg: ../thumbs). 77 77 'dest_dir' => null, 78 78 79 79 // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP) 80 80 'dest_file_type' => IMG_JPG, … … 82 82 // Destination file types. ('jpg', 'png', 'gif', 'wbmp') 83 83 'dest_file_extension' => 'jpg', 84 84 85 85 // Type of scaling to perform, and sizes used to calculate max dimensions. 86 86 'scaling_type' => IMAGETHUMB_FIT_LARGER, … … 96 96 // If using GD method, apply sharpen filter. Requires PHP > 5.1. 97 97 'sharpen' => true, 98 98 99 99 // Integers between 1-100, useful values are 65-85. 100 100 'sharpen_value' => 75, … … 108 108 109 109 // Final specifications for image sizes, set with setSpec(). 110 var$_image_specs = array();110 private $_image_specs = array(); 111 111 112 112 /** … … 116 116 * @param array $params Array of parameters (key => val pairs). 117 117 */ 118 function setParam($params)118 public function setParam($params) 119 119 { 120 120 $app =& App::getInstance(); … … 160 160 * @return mixed Configured parameter value. 161 161 */ 162 function getParam($param)163 { 164 $app =& App::getInstance(); 165 162 public function getParam($param) 163 { 164 $app =& App::getInstance(); 165 166 166 if (isset($this->_params[$param])) { 167 167 return $this->_params[$param]; … … 180 180 * Use to overwrite existing spec array values. 181 181 */ 182 function setSpec($spec, $index=null)182 public function setSpec($spec, $index=null) 183 183 { 184 184 $app =& App::getInstance(); … … 188 188 $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); 189 189 } else { 190 $spec['dest_dir'] = trim($spec['dest_dir']); 190 $spec['dest_dir'] = trim($spec['dest_dir']); 191 191 } 192 192 if (isset($spec['dest_file_type'])) { … … 233 233 $app->logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); 234 234 } 235 235 236 236 // Add to _image_specs array. 237 237 if (isset($index) && isset($this->_image_specs[$index])) { … … 241 241 } else { 242 242 // Merge with spec defaults. 243 $final_spec = array_merge($this->_default_image_specs, $spec); 243 $final_spec = array_merge($this->_default_image_specs, $spec); 244 244 $this->_image_specs[] = $final_spec; 245 245 } 246 246 247 247 return $final_spec; 248 248 } 249 249 250 250 /* 251 251 * Retrieve a value of a thumb specification. … … 259 259 * @since 08 May 2007 15:26:39 260 260 */ 261 function getSpec($key, $index=null)261 public function getSpec($key, $index=null) 262 262 { 263 263 $index = isset($index) ? $index : 0; … … 271 271 * @return bool true on success, false on failure. 272 272 */ 273 function processAll($runtime_specs=null)273 public function processAll($runtime_specs=null) 274 274 { 275 275 $app =& App::getInstance(); … … 313 313 * @return bool true on success, false on failure. 314 314 */ 315 function processFile($file_name, $runtime_specs=null)315 public function processFile($file_name, $runtime_specs=null) 316 316 { 317 317 $app =& App::getInstance(); … … 319 319 // Source file determined by provided file_name. 320 320 $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); 321 321 322 322 // Ensure we have a source. 323 323 if (sizeof($this->_image_specs) < 1) { … … 326 326 } else { 327 327 $app->logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__); 328 return false; 328 return false; 329 329 } 330 330 } … … 369 369 return false; 370 370 } 371 371 372 372 // To keep this script running even if user tries to stop browser. 373 373 ignore_user_abort(true); … … 379 379 380 380 foreach ($this->_image_specs as $index => $spec) { 381 381 382 382 if (is_array($runtime_specs)) { 383 383 // Override with runtime specs. 384 384 $spec = $this->setSpec($runtime_specs, $index); 385 385 } 386 386 387 387 // Destination filename uses the extension defined by dest_file_extension. 388 388 if ('/' == $spec['dest_dir']{0}) { … … 393 393 $dest_file = sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension']); 394 394 } 395 395 396 396 // Ensure destination directory exists and is writable. 397 397 if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { … … 438 438 return 0 === $return_val; 439 439 } 440 440 441 441 /* 442 442 * Use the Netpbm and libjpg cjpeg tools to generate a rescaled compressed image. … … 452 452 * @since 19 May 2006 13:55:46 453 453 */ 454 function _resizeWithNetpbm($source_file, $dest_file, $spec)454 private function _resizeWithNetpbm($source_file, $dest_file, $spec) 455 455 { 456 456 $app =& App::getInstance(); … … 491 491 ); 492 492 $app->logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__); 493 493 494 494 // Execute! 495 495 exec($command, $output, $return_val); … … 521 521 * @since 19 May 2006 15:46:02 522 522 */ 523 function _resizeWithGD($source_file, $dest_file, $spec)523 private function _resizeWithGD($source_file, $dest_file, $spec) 524 524 { 525 525 $app =& App::getInstance(); … … 593 593 return 1; 594 594 } 595 595 596 596 // Create destination image data in memory. 597 597 $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height); … … 605 605 return 1; 606 606 } 607 607 608 608 // Sharpen image using a custom filter matrix. 609 609 if (phpversion() > '5.1' && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) { 610 $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); // /WTF is this math?611 imageconvolution($dest_image_resource, array(array(-1, -1,-1),array(-1,$sharpen_value,-1),array(-1,-1,-1)), ($sharpen_value - 8), 0);610 $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); // TODO: WTF is this math? 611 imageconvolution($dest_image_resource, array(array(-1, -1, -1), array(-1, $sharpen_value, -1),array(-1, -1, -1)), ($sharpen_value - 8), 0); 612 612 } 613 613 … … 662 662 * @return bool true on success, false on failure. 663 663 */ 664 function deleteThumbs($file_name, $use_glob=false)664 public function deleteThumbs($file_name, $use_glob=false) 665 665 { 666 666 $app =& App::getInstance(); … … 677 677 $dest_dir = '/' == $spec['dest_dir']{0} ? $spec['dest_dir'] : sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']); 678 678 if ($use_glob) { 679 $dest_file = realpath(sprintf('%s/%s', $dest_dir, $this->_glob($file_name, $dest_dir))); 679 $dest_file = realpath(sprintf('%s/%s', $dest_dir, $this->_glob($file_name, $dest_dir))); 680 680 } else { 681 681 $dest_file = realpath(sprintf('%s/%s.%s', $dest_dir, mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension'])); 682 } 683 682 } 683 684 684 if (file_exists($dest_file)) { 685 685 if (!unlink($dest_file)) { … … 700 700 * @return bool true on success, false on failure. 701 701 */ 702 function deleteOriginal($file_name)702 public function deleteOriginal($file_name) 703 703 { 704 704 $app =& App::getInstance(); … … 733 733 * @return bool true on success, false on failure. 734 734 */ 735 function exists($file_name)735 public function exists($file_name) 736 736 { 737 737 $app =& App::getInstance(); … … 754 754 * @return bool True on success, false on failure. 755 755 */ 756 function _validFileExtension($file_name)756 private function _validFileExtension($file_name) 757 757 { 758 758 preg_match('/.*?\.(\w+)$/i', $file_name, $ext); 759 return !empty($ext) && in_array(mb_strtolower($ext[1]), $this->getParam('valid_file_extensions')); 759 return !empty($ext) && in_array(mb_strtolower($ext[1]), $this->getParam('valid_file_extensions')); 760 760 } 761 761 … … 766 766 * @return bool true on success, false on failure. 767 767 */ 768 function _createDestDirs($filename=null)768 private function _createDestDirs($filename=null) 769 769 { 770 770 $app =& App::getInstance(); … … 792 792 } 793 793 if (!file_exists($dest_dir)) { 794 // Recursively create destination directory. 794 795 $app->logMsg(sprintf('Creating dest dir: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); 795 if (phpversion() > '5') { 796 // Recursive. 797 if (!file_exists($dest_dir) && !($ret = mkdir($dest_dir, $this->getParam('dest_dir_perms'), true))) { 798 $return_val++; 799 $app->logMsg(sprintf('mkdir failure: %s', $dest_dir), LOG_ERR, __FILE__, __LINE__); 800 } 801 } else { 802 // Recursive mkdir for php 4. 803 $path = ''; 804 $depth = 0; 805 $ret = true; 806 foreach (array_diff(explode('/', $dest_dir), array('')) as $dir) { 807 $path .= '/' . $dir; 808 $depth++; 809 /// FIXME: This is a dirty way to make this work when open_basedir prevents looking at the first 3 directories in an absolute path. 810 if ($depth > 3 && !($ret = file_exists($path) ? true : mkdir($path, $this->getParam('dest_dir_perms')))) { 811 $return_val++; 812 $app->logMsg(sprintf('mkdir failure: %s', $path), LOG_ERR, __FILE__, __LINE__); 813 break; 814 } else { 815 $app->logMsg(sprintf('mkdir attempt: %s', $path), LOG_DEBUG, __FILE__, __LINE__); 816 } 817 } 796 if (!file_exists($dest_dir) && !($ret = mkdir($dest_dir, $this->getParam('dest_dir_perms'), true))) { 797 $return_val++; 798 $app->logMsg(sprintf('mkdir failure: %s', $dest_dir), LOG_ERR, __FILE__, __LINE__); 818 799 } 819 820 800 if ($ret) { 821 $app->logMsg(sprintf('mkdir success: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); 801 $app->logMsg(sprintf('mkdir success: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); 822 802 } 823 803 } else { … … 844 824 * @param string $line __LINE__. 845 825 */ 846 function _raiseMsg($message, $type, $file, $line)826 private function _raiseMsg($message, $type, $file, $line) 847 827 { 848 828 $app =& App::getInstance(); … … 852 832 } 853 833 } 854 834 855 835 /** 856 836 * Get filename by glob pattern. Searches a directory for an image that matches the … … 864 844 * @since 15 Nov 2005 20:55:22 865 845 */ 866 function _glob($pattern, $directory)846 private function _glob($pattern, $directory) 867 847 { 868 848 $file_list = glob(sprintf('%s/%s', $directory, $pattern)); … … 875 855 876 856 } // End of class. 877 ?>
Note: See TracChangeset
for help on using the changeset viewer.