Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

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

    r121 r136  
    22/**
    33 * ImageThumb.inc.php
    4  * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
    55 *
    66 * @author   Quinn Comendant <quinn@strangecode.com>
     
    9595    function setParam($params)
    9696    {
     97        $app =& App::getInstance();
     98
    9799        if (isset($params) && is_array($params)) {
    98100
     
    102104                // Must be directory.
    103105                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__);
    105107                    trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR);
    106108                }
    107109                // Must be readable.
    108110                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__);
    110112                    trigger_error(sprintf('Source directory not readable: %s', $params['source_dir']), E_USER_ERROR);
    111113                }
     
    115117            $this->_params = array_merge($this->_params, $params);
    116118        } 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__);
    118120        }
    119121    }
     
    128130    function getParam($param)
    129131    {
     132        $app =& App::getInstance();
     133   
    130134        if (isset($this->_params[$param])) {
    131135            return $this->_params[$param];
    132136        } 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__);
    134138            return null;
    135139        }
     
    144148    function setSpec($spec, $index=null)
    145149    {
     150        $app =& App::getInstance();
     151
    146152        // A little sanity checking.
    147153        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__);
    149155        }
    150156        if (isset($spec['dest_file_type'])) {
     
    152158            case IMG_JPG :
    153159                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__);
    155161                }
    156162                $spec['dest_file_extention'] = 'jpg';
     
    158164            case IMG_PNG :
    159165                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__);
    161167                }
    162168                $spec['dest_file_extention'] = 'png';
     
    164170            case IMG_GIF :
    165171                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__);
    167173                }
    168174                $spec['dest_file_extention'] = 'gif';
     
    170176            case IMG_WBMP :
    171177                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__);
    173179                }
    174180                $spec['dest_file_extention'] = 'wbmp';
    175181                break;
    176182            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__);
    178184                break;
    179185            }
    180186        }
    181187        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__);
    183189        }
    184190        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__);
    186192        }
    187193        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__);
    189195        }
    190196        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__);
    192198        }
    193199       
     
    213219    function processAll($runtime_specs=null)
    214220    {
     221        $app =& App::getInstance();
     222
    215223        // Ensure we have a source.
    216224        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__);
    218226            return false;
    219227        }
     
    237245            return 0 === $return_val;
    238246        } 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__);
    240248            return false;
    241249        }
     
    252260    function processFile($file_name, $runtime_specs=null)
    253261    {
     262        $app =& App::getInstance();
     263
    254264        // Source file determinted by provided file_name.
    255265        $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name));
     
    260270                $this->setSpec($runtime_specs, 0);
    261271            } 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__);
    263273                return false;               
    264274            }
     
    267277        // Ensure we have a source.
    268278        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__);
    270280            return false;
    271281        }
     
    274284        if (!file_exists($source_file)) {
    275285            $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__);
    277287            return false;
    278288        }
     
    281291        if (!is_readable($source_file)) {
    282292            $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__);
    284294            return false;
    285295        }
     
    288298        if (filesize($source_file) <= 0) {
    289299            $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__);
    291301            return false;
    292302        }
     
    295305        if (!$this->_validFileExtension($file_name)) {
    296306            $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__);
    298308            return false;
    299309        }
     
    324334                $file_size = filesize($dest_file);
    325335                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__);
    327337                    continue;
    328338                }
     
    333343            if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) {
    334344                $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__);
    336346            }
    337347
     
    339349            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'))) {
    340350                // 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__);
    342352                $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec);
    343353            } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) {
    344354                // 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__);
    346356                $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec);
    347357            } 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__);
    349359                return false;
    350360            }
     
    370380    function _resizeWithNetpbm($source_file, $dest_file, $spec)
    371381    {
     382        $app =& App::getInstance();
     383
    372384        // Define pnmscale arguments.
    373385        switch ($spec['scaling_type']) {
     
    405417            escapeshellcmd($file_name)
    406418        );
    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__);
    408420       
    409421        // Execute!
     
    414426            // Make the thumbnail writable so the user can delete it over ftp without being 'apache'.
    415427            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__);
    417429        } else {
    418430            // 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__);
    420432        }
    421433
     
    438450    function _resizeWithGD($source_file, $dest_file, $spec)
    439451    {
     452        $app =& App::getInstance();
     453
    440454        // Get original file dimensions and type.
    441455        list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_file);
     
    487501            $source_image_resource = imagecreatefromwbmp($source_file);
    488502        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__);
    490504            return 1;
    491505            break;
    492506        }
    493507        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__);
    495509            return 1;
    496510        }
     
    501515        // Resample!
    502516        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__);
    504518            return 1;
    505519        }
     
    528542            break;
    529543        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__);
    531545            return 1;
    532546            break;
     
    537551            // Make the thumbnail writable so the user can delete it over ftp without being 'apache'.
    538552            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__);
    540554            return 0;
    541555        } else {
    542556            // 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__);
    544558            return 1;
    545559        }
     
    555569    function deleteThumbs($file_name)
    556570    {
     571        $app =& App::getInstance();
     572
    557573        // Ensure we have a source.
    558574        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__);
    560576            return false;
    561577        }
     
    567583                if (!unlink($dest_file)) {
    568584                    $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__);
    570586                }
    571587            }
     
    584600    function deleteOriginal($file_name)
    585601    {
     602        $app =& App::getInstance();
     603
    586604        // Ensure we have a source.
    587605        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__);
    589607            return false;
    590608        }
     
    592610        $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name));
    593611        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__);
    595613            return false;
    596614        }
     
    608626    function exists($file_name)
    609627    {
     628        $app =& App::getInstance();
     629
    610630        // Ensure we have a source.
    611631        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__);
    613633            return false;
    614634        }
     
    639659    function _createDestDirs()
    640660    {
     661        $app =& App::getInstance();
     662
    641663        static $already_checked = false;
    642664
     
    644666            // Ensure we have a source.
    645667            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__);
    647669                return false;
    648670            }
     
    654676                    if (!mkdir($this->getParam('source_dir') . '/' . $spec['dest_dir'], $this->getParam('dest_dir_perms'))) {
    655677                        $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__);
    657679                    }
    658680                }
     
    667689
    668690    /**
    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.
    670692     *
    671693     * @access public
     
    679701    function _raiseMsg($message, $type, $file, $line)
    680702    {
     703        $app =& App::getInstance();
     704
    681705        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);
    683707        }
    684708    }
Note: See TracChangeset for help on using the changeset viewer.