Changeset 120


Ignore:
Timestamp:
May 21, 2006 6:59:28 AM (18 years ago)
Author:
scdev
Message:

Completed GD integration to ImageThumb?

File:
1 edited

Legend:

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

    r119 r120  
    3333        // Permissions of autocreated directories. Must be at least 0700 with owner=apache.
    3434        'dest_dir_perms' => 0777,
    35        
    36         // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP)
    37         'dest_file_type' => IMG_JPG,
    38 
    39         // Destination file types. ('jpg', 'png', 'gif', 'wbmp')
    40         'dest_file_extention' => 'jpg',
    4135
    4236        // Require file to have one of the following file name extentions.
     
    5953        // The destination for an image thumbnail size. Path relative to source_dir (eg: ../thumbs).
    6054        'dest_dir' => null,
     55       
     56        // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP)
     57        'dest_file_type' => IMG_JPG,
     58
     59        // Destination file types. ('jpg', 'png', 'gif', 'wbmp')
     60        'dest_file_extention' => 'jpg',
     61       
    6162        // Type of scaling to perform, and sizes used to calculate max dimentions.
    6263        'scaling_type' => IMAGETHUMB_FIT_LARGER,
    6364        'width' => null,
    6465        'height' => null,
     66
    6567        // Percentage quality of image compression output 0-100.
    6668        'quality' => 65,
     69
    6770        // Create progressive jpegs?
    6871        'progressive' => false,
     72
     73        // If using GD method, apply sharpen filter. Requires PHP > 5.1.
     74        'sharpen' => true,
     75       
     76        // Integers between 1-100, useful values are 65-85.
     77        'sharpen_value' => 75,
     78
    6979        // If source image is smaller than thumbnail, allow upscaling?
    7080        'allow_upscaling' => false,
     81
    7182        // If thumb exists and filesize is smaller than this, do not overwrite the thumb.
    7283        'keep_filesize' => null,
     
    100111                }
    101112            }
    102            
    103             if (isset($params['dest_file_type'])) {
    104                 switch ($params['dest_file_type']) {
    105                 case IMG_JPG :
    106                     if (imagetypes() & IMG_JPG == 0) {
    107                         App::logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);
    108                     }
    109                     $params['dest_file_extention'] = 'jpg';
    110                     break;
    111                 case IMG_PNG :
    112                     if (imagetypes() & IMG_PNG == 0) {
    113                         App::logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);
    114                     }
    115                     $params['dest_file_extention'] = 'png';
    116                     break;
    117                 case IMG_GIF :
    118                     if (imagetypes() & IMG_GIF == 0) {
    119                         App::logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);
    120                     }
    121                     $params['dest_file_extention'] = 'gif';
    122                     break;
    123                 case IMG_WBMP :
    124                     if (imagetypes() & IMG_WBMP == 0) {
    125                         App::logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);
    126                     }
    127                     $params['dest_file_extention'] = 'wbmp';
    128                     break;
    129                 default :
    130                     App::logMsg(sprintf('Invalid dest_file_type: %s', $params['dest_file_type']), LOG_ERR, __FILE__, __LINE__);
    131                     break;
    132                 }
    133             }
    134113
    135114            // Merge new parameters with old overriding only those passed.
     
    163142     * @param   array $spec The specifications for a size of output image.
    164143     */
    165     function setSpec($spec)
     144    function setSpec($spec, $index=null)
    166145    {
    167146        // A little sanity checking.
    168147        if (!isset($spec['dest_dir']) || '' == $spec['dest_dir']) {
    169148            App::logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__);
    170             return false;
     149        }
     150        if (isset($spec['dest_file_type'])) {
     151            switch ($spec['dest_file_type']) {
     152            case IMG_JPG :
     153                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__);
     155                }
     156                $spec['dest_file_extention'] = 'jpg';
     157                break;
     158            case IMG_PNG :
     159                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__);
     161                }
     162                $spec['dest_file_extention'] = 'png';
     163                break;
     164            case IMG_GIF :
     165                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__);
     167                }
     168                $spec['dest_file_extention'] = 'gif';
     169                break;
     170            case IMG_WBMP :
     171                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__);
     173                }
     174                $spec['dest_file_extention'] = 'wbmp';
     175                break;
     176            default :
     177                App::logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__);
     178                break;
     179            }
    171180        }
    172181        if (!isset($spec['width']) || !is_int($spec['width'])) {
    173182            App::logMsg('setSpec error: width not specified.', LOG_ERR, __FILE__, __LINE__);
    174             return false;
    175183        }
    176184        if (!isset($spec['height']) || !is_int($spec['height'])) {
    177185            App::logMsg('setSpec error: height not specified.', LOG_ERR, __FILE__, __LINE__);
    178             return false;
    179         }
    180         if (isset($spec['quality']) && IMG_JPG != $this->getParam('dest_file_type')) {
     186        }
     187        if (isset($spec['quality']) && IMG_JPG != $spec['dest_file_type']) {
    181188            App::logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__);
    182189        }
    183         if (isset($spec['progressive']) && IMG_JPG != $this->getParam('dest_file_type')) {
     190        if (isset($spec['progressive']) && IMG_JPG != $spec['dest_file_type']) {
    184191            App::logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__);
    185192        }
    186         // Merge defaults with provided.
    187         $this->image_specs[] = array_merge($this->default_image_specs, $spec);
     193       
     194        if (isset($index) && isset($this->image_specs[$index])) {
     195            // Merge with previous.
     196            $this->image_specs[$index] = array_merge($this->image_specs[$index], $spec);
     197        } else {
     198            // Merge with defaults.
     199            $this->image_specs[] = array_merge($this->default_image_specs, $spec);           
     200        }
    188201    }
    189202
     
    217230                $return_val += $this->processFile($file_name);
    218231            }
     232            $this->_raiseMsg(sprintf(_("Resized %s images."), sizeof($files)), MSG_SUCCESS, __FILE__, __LINE__);
    219233            return 0 === $return_val;
    220234        } else {
     
    250264        // Confirm source image exists.
    251265        if (!file_exists($source_file)) {
    252             $this->_raiseMsg(sprintf(_("Image resizing failed: source image not found: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);
     266            $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s was not found."), $file_name), MSG_ERR, __FILE__, __LINE__);
    253267            App::logMsg(sprintf('Source image not found: %s', $file_name), LOG_ALERT, __FILE__, __LINE__);
    254268            return false;
     
    257271        // Confirm source image is readable.
    258272        if (!is_readable($source_file)) {
    259             $this->_raiseMsg(sprintf(_("Image resizing failed: source image not readable: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);
     273            $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__);
    260274            App::logMsg(sprintf('Source image not readable: %s', $file_name), LOG_ALERT, __FILE__, __LINE__);
    261275            return false;
     
    264278        // Confirm source image contains data.
    265279        if (filesize($source_file) <= 0) {
    266             $this->_raiseMsg(sprintf(_("Image resizing failed: source image corrupt: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);
     280            $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__);
    267281            App::logMsg(sprintf('Source image is zero bytes: %s', $file_name), LOG_ALERT, __FILE__, __LINE__);
    268282            return false;
     
    271285        // Confirm source image has a valid file extension.
    272286        if (!$this->_validFileExtension($file_name)) {
    273             $this->_raiseMsg(sprintf(_("Image resizing failed: source image not of valid type: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);
     287            $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__);
    274288            App::logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $file_name), LOG_ERR, __FILE__, __LINE__);
    275289            return false;
     
    291305           
    292306            // Destination filename uses the extention defined by dest_file_extention.
    293             $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $this->getParam('dest_file_extention')));
     307            $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']));
    294308
    295309            // Skip existing thumbnails with file size below $spec['keep_filesize'].
     
    336350    * @param    string  $dest_file      Full path to destination image file.
    337351    * @param    array   $spec           Array of image size specifications.
    338     * @return   bool                    Success value.
     352    * @return   int                     0 if no error, n > 0 if errors.
    339353    * @author   Quinn Comendant <quinn@strangecode.com>
    340354    * @version  1.0
     
    387401            // Make the thumbnail writable so the user can delete it over ftp without being 'apache'.
    388402            chmod($dest_file, $this->getParam('dest_file_perms'));
    389             App::logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . $file_name, $return_val), LOG_DEBUG, __FILE__, __LINE__);
     403            App::logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__);
    390404        } else {
    391405            // An error occurred.
    392             App::logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . $file_name, $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__);
     406            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__);
    393407        }
    394408
     
    404418    * @param    string  $dest_file      Full path to destination image file.
    405419    * @param    array   $spec           Array of image size specifications.
    406     * @return   bool                    Success value.
     420    * @return   int                     0 if no error, n > 0 if errors.
    407421    * @author   Quinn Comendant <quinn@strangecode.com>
    408422    * @version  1.0
     
    417431        switch ($spec['scaling_type']) {
    418432        case IMAGETHUMB_FIT_WIDTH :
     433            $dest_image_width = $spec['width'];
    419434            $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width);
    420             $dest_image_width = $spec['width'];
    421435            break;
    422436        case IMAGETHUMB_FIT_HEIGHT :
     437            $dest_image_height = $spec['height'];
    423438            $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height);
    424             $dest_image_height = $spec['height'];
    425439            break;
    426440        case IMAGETHUMB_FIT_LARGER :
    427             if ($source_image_width < $source_image_height) {
    428                $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height);
    429                $dest_image_height = $source_image_height;
     441            if (($source_image_width * ($spec['height'] / $source_image_height)) <= $spec['width']) {
     442                // Height is larger.
     443                $dest_image_height = $spec['height'];
     444                $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height);
    430445            } else {
    431                $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width);
    432                $dest_image_width = $source_image_width;
     446                // Width is larger.
     447                $dest_image_width = $spec['width'];
     448                $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width);
    433449            }
    434450            break;
     
    443459            break;
    444460        }
    445        
    446         echo '<li> sourceWidth: ' . $source_image_width;
    447         echo '<li> sourceHeight: ' . $source_image_height;
    448         echo '<li> Width: ' . $dest_image_width;
    449         echo '<li> Height: ' . $dest_image_height;
    450         die;///
    451461
    452462        // Create source image data in memory.
    453463        switch ($source_image_type) {
    454464        case IMAGETYPE_JPEG :
    455             $source_image_data = imagecreatefromjpeg($source_file);
     465            $source_image_resource = imagecreatefromjpeg($source_file);
    456466            break;
    457467        case IMAGETYPE_PNG :
    458             $source_image_data = imagecreatefrompng($source_file);
     468            $source_image_resource = imagecreatefrompng($source_file);
    459469            break;
    460470        case IMAGETYPE_GIF :
    461             $source_image_data = imagecreatefromgif($source_file);
     471            $source_image_resource = imagecreatefromgif($source_file);
    462472            break;
    463473        case IMAGETYPE_WBMP :
    464             $source_image_data = imagecreatefromwbmp($source_file);
     474            $source_image_resource = imagecreatefromwbmp($source_file);
    465475        default :
    466476            App::logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__);
    467             return false;
    468             break;
    469         }
    470         if (!$source_image_data) {
     477            return 1;
     478            break;
     479        }
     480        if (!$source_image_resource) {
    471481            App::logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__);
    472             return false;
     482            return 1;
    473483        }
    474484       
    475485        // Create destination image data in memory.
    476         $dest_image_data = imagecreatetruecolor($dest_image_width, $dest_image_height);
     486        $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height);
    477487
    478488        // Resample!
    479         if (!imagecopyresampled($dest_image_data, $source_image_data, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) {
     489        if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) {
    480490            App::logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__);
    481             return false;
     491            return 1;
     492        }
     493       
     494        // Sharpen image using a custom filter matrix.
     495        if (phpversion() > '5.1' && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) {
     496            $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10);
     497            imageconvolution($dest_image_resource, array(array(-1,-1,-1),array(-1,$sharpen_value,-1),array(-1,-1,-1)), ($sharpen_value - 8), 0);
    482498        }
    483499
    484500        // Save image.
    485501        $return_val = true;
    486         switch ($this->getParam('dest_file_type')) {
     502        switch ($spec['dest_file_type']) {
    487503        case IMG_JPG :
    488             imageinterlace($dest_image_data, (true == $spec['progressive'] ? 1 : 0));
    489             $return_val = imagejpeg($dest_image_data, $dest_file, $spec['quality']);
     504            imageinterlace($dest_image_resource, (true == $spec['progressive'] ? 1 : 0));
     505            $return_val = imagejpeg($dest_image_resource, $dest_file, $spec['quality']);
    490506            break;
    491507        case IMG_PNG :
    492             $return_val = imagepng($dest_image_data, $dest_file);
     508            $return_val = imagepng($dest_image_resource, $dest_file);
    493509            break;
    494510        case IMG_GIF :
    495             $return_val = imagegif($dest_image_data, $dest_file);
     511            $return_val = imagegif($dest_image_resource, $dest_file);
    496512            break;
    497513        case IMG_WBMP :
    498             $return_val = imagewbmp($dest_image_data, $dest_file);
     514            $return_val = imagewbmp($dest_image_resource, $dest_file);
    499515            break;
    500516        default :
    501             App::logMsg(sprintf('Destination image type %s not supported for image %s.', $this->getParam('dest_file_type'), $dest_file), LOG_WARNING, __FILE__, __LINE__);
    502             return false;
     517            App::logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__);
     518            return 1;
    503519            break;
    504520        }
     
    507523            // Success!
    508524            // Make the thumbnail writable so the user can delete it over ftp without being 'apache'.
    509             chmod($source_file, $this->getParam('dest_file_perms'));
     525            chmod($dest_file, $this->getParam('dest_file_perms'));
    510526            App::logMsg(sprintf('Successfully resized image %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__);
    511             return true;
     527            return 0;
    512528        } else {
    513529            // An error occurred.
    514530            App::logMsg(sprintf('Image %s failed resizing.', $dest_file), LOG_ERR, __FILE__, __LINE__);
    515             return false;
     531            return 1;
    516532        }
    517533    }
     
    534550        $return_val = 0;
    535551        foreach ($this->image_specs as $spec) {
    536             $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $this->getParam('dest_file_extention')));
     552            $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']));
    537553            if (file_exists($dest_file)) {
    538554                if (!unlink($dest_file)) {
Note: See TracChangeset for help on using the changeset viewer.