Ignore:
Timestamp:
Jun 7, 2006 5:35:16 AM (18 years ago)
Author:
scdev
Message:

Q - In the middle of working on the Prefs and Cache instantiation mode...can't decide to use singleton pattern or global vars. Updated ImageThumb? to allow filenames with path elements such as 01/23/4567_file.jpg.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/ImageThumb.inc.php

    r146 r152  
    99 */
    1010
    11 // Image resize options.
     11// Image proprtion options.
    1212define('IMAGETHUMB_FIT_WIDTH', 1);
    1313define('IMAGETHUMB_FIT_HEIGHT', 2);
     
    1515define('IMAGETHUMB_STRETCH', 4);
    1616define('IMAGETHUMB_NO_SCALE', 5);
     17
     18// Image resize options.
    1719define('IMAGETHUMB_METHOD_NETPBM', 6);
    1820define('IMAGETHUMB_METHOD_GD', 7);
     
    3234
    3335        // Permissions of autocreated directories. Must be at least 0700 with owner=apache.
    34         'dest_dir_perms' => 0777,
     36        'dest_dir_perms' => 0700,
    3537
    3638        // Require file to have one of the following file name extentions.
     
    5052   
    5153    // 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).
    5457        'dest_dir' => null,
    5558       
     
    8588
    8689    // Final specifications for image sizes, set with setSpec().
    87     var $image_specs = array();
     90    var $_image_specs = array();
    8891
    8992    /**
     
    99102        if (isset($params) && is_array($params)) {
    100103
    101             // Enforce valid upload_path parameter.
     104            // Enforce valid source_dir parameter.
    102105            if (isset($params['source_dir'])) {
    103106                $params['source_dir'] = realpath($params['source_dir']);
    104                 // Must be directory.
     107                // Source must be directory.
    105108                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.
    110122                if (!is_readable($params['source_dir'])) {
    111123                    $app->logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__);
     
    151163
    152164        // A little sanity checking.
    153         if (!isset($spec['dest_dir']) || '' == $spec['dest_dir']) {
     165        if (!isset($spec['dest_dir']) || '' == trim($spec['dest_dir'])) {
    154166            $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__);
     167        } else {
     168            $spec['dest_dir'] = trim($spec['dest_dir']);           
    155169        }
    156170        if (isset($spec['dest_file_type'])) {
     
    198212        }
    199213       
    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;
    204219        } 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;
    208223        }
    209224       
     
    245260            return 0 === $return_val;
    246261        } 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__);
    248263            return false;
    249264        }
     
    266281       
    267282        // Ensure we have a source.
    268         if (sizeof($this->image_specs) < 1) {
     283        if (sizeof($this->_image_specs) < 1) {
    269284            if (is_array($runtime_specs)) {
    270285                $this->setSpec($runtime_specs, 0);
     
    284299        if (!file_exists($source_file)) {
    285300            $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__);
    287302            return false;
    288303        }
     
    291306        if (!is_readable($source_file)) {
    292307            $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__);
    294309            return false;
    295310        }
     
    298313        if (filesize($source_file) <= 0) {
    299314            $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__);
    301316            return false;
    302317        }
     
    310325
    311326        // 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        }
    313330       
    314331        // To keep this script running even if user tries to stop browser.
     
    320337        $return_val = 0;
    321338
    322         foreach ($this->image_specs as $index => $spec) {
     339        foreach ($this->_image_specs as $index => $spec) {
    323340           
    324341            if (is_array($runtime_specs)) {
     
    328345           
    329346            // 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            }
    331364
    332365            // Skip existing thumbnails with file size below $spec['keep_filesize'].
     
    343376            if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) {
    344377                $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__);
    346379            }
    347380
     
    349382            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'))) {
    350383                // 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__);
    352385                $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec);
    353386            } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) {
    354387                // 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__);
    356389                $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec);
    357390            } 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__);
    359392                return false;
    360393            }
    361394        }
    362395
    363         // If > 0, there was a problem thumb-nailing.
     396        // If > 0, there was a problem thumbnailing.
    364397        return 0 === $return_val;
    365398    }
     
    516549        if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) {
    517550            $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);
    518554            return 1;
    519555        }
     
    543579        default :
    544580            $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);
    545584            return 1;
    546585            break;
    547586        }
     587
     588        // Always cleanup images from memory.
     589        imagedestroy($source_image_resource);
     590        imagedestroy($dest_image_resource);
    548591
    549592        if ($return_val) {
    550593            // Success!
    551594            // 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__);
    554599            return 0;
    555600        } else {
    556601            // 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__);
    558603            return 1;
    559604        }
     
    578623
    579624        $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            }
    582633            if (file_exists($dest_file)) {
    583634                if (!unlink($dest_file)) {
     
    657708     * @return  bool true on success, false on failure.
    658709     */
    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])) {
    666721            // Ensure we have a source.
    667722            if ('' == $this->getParam('source_dir')) {
     
    671726       
    672727            // 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']);
    679738                    }
    680739                }
    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;
    688771    }
    689772
Note: See TracChangeset for help on using the changeset viewer.