Ignore:
Timestamp:
Feb 20, 2014 3:03:59 AM (10 years ago)
Author:
anonymous
Message:

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/lib/ImageThumb.inc.php

    r396 r468  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    4141
    4242class ImageThumb {
    43    
     43
    4444    // General object parameters.
    45     var $_params = array(
     45    private $_params = array(
    4646        // The location for images to create thumbnails from.
    4747        'source_dir' => null,
     
    5858        // Require file to have one of the following file name extensions.
    5959        'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png'),
    60        
     60
    6161        // Method to use for resizing. (IMAGETHUMB_METHOD_NETPBM or IMAGETHUMB_METHOD_GD)
    6262        'resize_method' => IMAGETHUMB_METHOD_NETPBM,
     
    7070        'display_messages' => MSG_ALL,
    7171    );
    72    
     72
    7373    // 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.
    7676        // Use initial / to specify absolute paths, leave off to specify a path relative to source_dir (eg: ../thumbs).
    7777        'dest_dir' => null,
    78        
     78
    7979        // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP)
    8080        'dest_file_type' => IMG_JPG,
     
    8282        // Destination file types. ('jpg', 'png', 'gif', 'wbmp')
    8383        'dest_file_extension' => 'jpg',
    84        
     84
    8585        // Type of scaling to perform, and sizes used to calculate max dimensions.
    8686        'scaling_type' => IMAGETHUMB_FIT_LARGER,
     
    9696        // If using GD method, apply sharpen filter. Requires PHP > 5.1.
    9797        'sharpen' => true,
    98        
     98
    9999        // Integers between 1-100, useful values are 65-85.
    100100        'sharpen_value' => 75,
     
    108108
    109109    // Final specifications for image sizes, set with setSpec().
    110     var $_image_specs = array();
     110    private $_image_specs = array();
    111111
    112112    /**
     
    116116     * @param  array    $params     Array of parameters (key => val pairs).
    117117     */
    118     function setParam($params)
     118    public function setParam($params)
    119119    {
    120120        $app =& App::getInstance();
     
    160160     * @return mixed               Configured parameter value.
    161161     */
    162     function getParam($param)
    163     {
    164         $app =& App::getInstance();
    165    
     162    public function getParam($param)
     163    {
     164        $app =& App::getInstance();
     165
    166166        if (isset($this->_params[$param])) {
    167167            return $this->_params[$param];
     
    180180     *                          Use to overwrite existing spec array values.
    181181     */
    182     function setSpec($spec, $index=null)
     182    public function setSpec($spec, $index=null)
    183183    {
    184184        $app =& App::getInstance();
     
    188188            $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__);
    189189        } else {
    190             $spec['dest_dir'] = trim($spec['dest_dir']);           
     190            $spec['dest_dir'] = trim($spec['dest_dir']);
    191191        }
    192192        if (isset($spec['dest_file_type'])) {
     
    233233            $app->logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__);
    234234        }
    235        
     235
    236236        // Add to _image_specs array.
    237237        if (isset($index) && isset($this->_image_specs[$index])) {
     
    241241        } else {
    242242            // Merge with spec defaults.
    243             $final_spec = array_merge($this->_default_image_specs, $spec);           
     243            $final_spec = array_merge($this->_default_image_specs, $spec);
    244244            $this->_image_specs[] = $final_spec;
    245245        }
    246        
     246
    247247        return $final_spec;
    248248    }
    249    
     249
    250250    /*
    251251    * Retrieve a value of a thumb specification.
     
    259259    * @since    08 May 2007 15:26:39
    260260    */
    261     function getSpec($key, $index=null)
     261    public function getSpec($key, $index=null)
    262262    {
    263263        $index = isset($index) ? $index : 0;
     
    271271     * @return  bool true on success, false on failure.
    272272     */
    273     function processAll($runtime_specs=null)
     273    public function processAll($runtime_specs=null)
    274274    {
    275275        $app =& App::getInstance();
     
    313313     * @return  bool true on success, false on failure.
    314314     */
    315     function processFile($file_name, $runtime_specs=null)
     315    public function processFile($file_name, $runtime_specs=null)
    316316    {
    317317        $app =& App::getInstance();
     
    319319        // Source file determined by provided file_name.
    320320        $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name));
    321        
     321
    322322        // Ensure we have a source.
    323323        if (sizeof($this->_image_specs) < 1) {
     
    326326            } else {
    327327                $app->logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__);
    328                 return false;               
     328                return false;
    329329            }
    330330        }
     
    369369            return false;
    370370        }
    371        
     371
    372372        // To keep this script running even if user tries to stop browser.
    373373        ignore_user_abort(true);
     
    379379
    380380        foreach ($this->_image_specs as $index => $spec) {
    381            
     381
    382382            if (is_array($runtime_specs)) {
    383383                // Override with runtime specs.
    384384                $spec = $this->setSpec($runtime_specs, $index);
    385385            }
    386            
     386
    387387            // Destination filename uses the extension defined by dest_file_extension.
    388388            if ('/' == $spec['dest_dir']{0}) {
     
    393393                $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']);
    394394            }
    395                  
     395
    396396            // Ensure destination directory exists and is writable.
    397397            if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) {
     
    438438        return 0 === $return_val;
    439439    }
    440    
     440
    441441    /*
    442442    * Use the Netpbm and libjpg cjpeg tools to generate a rescaled compressed image.
     
    452452    * @since    19 May 2006 13:55:46
    453453    */
    454     function _resizeWithNetpbm($source_file, $dest_file, $spec)
     454    private function _resizeWithNetpbm($source_file, $dest_file, $spec)
    455455    {
    456456        $app =& App::getInstance();
     
    491491        );
    492492        $app->logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__);
    493        
     493
    494494        // Execute!
    495495        exec($command, $output, $return_val);
     
    521521    * @since    19 May 2006 15:46:02
    522522    */
    523     function _resizeWithGD($source_file, $dest_file, $spec)
     523    private function _resizeWithGD($source_file, $dest_file, $spec)
    524524    {
    525525        $app =& App::getInstance();
     
    593593            return 1;
    594594        }
    595        
     595
    596596        // Create destination image data in memory.
    597597        $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height);
     
    605605            return 1;
    606606        }
    607        
     607
    608608        // Sharpen image using a custom filter matrix.
    609609        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);
    612612        }
    613613
     
    662662     * @return  bool true on success, false on failure.
    663663     */
    664     function deleteThumbs($file_name, $use_glob=false)
     664    public function deleteThumbs($file_name, $use_glob=false)
    665665    {
    666666        $app =& App::getInstance();
     
    677677            $dest_dir = '/' == $spec['dest_dir']{0} ? $spec['dest_dir'] : sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']);
    678678            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)));
    680680            } else {
    681681                $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
    684684            if (file_exists($dest_file)) {
    685685                if (!unlink($dest_file)) {
     
    700700     * @return  bool true on success, false on failure.
    701701     */
    702     function deleteOriginal($file_name)
     702    public function deleteOriginal($file_name)
    703703    {
    704704        $app =& App::getInstance();
     
    733733     * @return  bool true on success, false on failure.
    734734     */
    735     function exists($file_name)
     735    public function exists($file_name)
    736736    {
    737737        $app =& App::getInstance();
     
    754754     * @return  bool    True on success, false on failure.
    755755     */
    756     function _validFileExtension($file_name)
     756    private function _validFileExtension($file_name)
    757757    {
    758758        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'));
    760760    }
    761761
     
    766766     * @return  bool true on success, false on failure.
    767767     */
    768     function _createDestDirs($filename=null)
     768    private function _createDestDirs($filename=null)
    769769    {
    770770        $app =& App::getInstance();
     
    792792                }
    793793                if (!file_exists($dest_dir)) {
     794                    // Recursively create destination directory.
    794795                    $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__);
    818799                    }
    819 
    820800                    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__);
    822802                    }
    823803                } else {
     
    844824     * @param string $line    __LINE__.
    845825     */
    846     function _raiseMsg($message, $type, $file, $line)
     826    private function _raiseMsg($message, $type, $file, $line)
    847827    {
    848828        $app =& App::getInstance();
     
    852832        }
    853833    }
    854    
     834
    855835    /**
    856836     * Get filename by glob pattern. Searches a directory for an image that matches the
     
    864844     * @since   15 Nov 2005 20:55:22
    865845     */
    866     function _glob($pattern, $directory)
     846    private function _glob($pattern, $directory)
    867847    {
    868848        $file_list = glob(sprintf('%s/%s', $directory, $pattern));
     
    875855
    876856} // End of class.
    877 ?>
Note: See TracChangeset for help on using the changeset viewer.