Changeset 1827

Show
Ignore:
Timestamp:
01/27/08 02:50:37 (7 months ago)
Author:
Shadowhand
Message:

Updates to Image:

  • Moved sanitize_geometry to Image_Driver
  • Made properties() an abstract method
  • Updated language files with new and missing messages
  • Removed exif_imagetype() usage in favor of getimagesize(), which is more common and provides more information
Location:
trunk/system
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/i18n/en_US/image.php

    r1772 r1827  
    33$lang = array 
    44( 
     5        'getimagesize_missing'    => 'The Image library requires the <tt>getimagesize</tt> PHP function, which is not available in your installation.', 
    56        'driver_not_supported'    => 'The requested Image driver, %s, was not found.', 
     7        'unsupported_method'      => 'Your configured driver does not support the %s image transformation.', 
     8        'file_not_found'          => 'The specified image, %s was not found. Please verify that images exist by using <tt>file_exists</tt> before manipulating them.', 
     9        'type_not_allowed'        => 'The specified image, %s, is not an allowed image type.', 
     10 
     11        // ImageMagick specific messages 
     12        'imagemagick' => array 
     13        ( 
     14                'not_found' => 'The ImageMagick directory specified does not contain a required program, %s.', 
     15        ), 
     16 
     17 
     18        // GD specific messages 
     19        'gd' => array 
     20        ( 
     21                'requires_v2' => 'The Image library requires GD2. Please see http://php.net/gd_info for more information.', 
     22        ), 
    623 
    724        // CI's Image_lib stuff below 
  • trunk/system/libraries/Image.php

    r1814 r1827  
    5151        public function __construct($image, $config = NULL) 
    5252        { 
    53                 // Load configuration 
    54                 $this->config = (array) $config + Config::item('image'); 
     53                static $check; 
     54 
     55                // Make the check exactly once 
     56                ($check === NULL) and $check = function_exists('getimagesize'); 
     57 
     58                if ($check === FALSE) 
     59                        throw new Kohana_Exception('image.getimagesize_missing'); 
    5560 
    5661                // Check to make sure the image exists 
     
    5863                        throw new Kohana_Exception('image.file_not_found', $image); 
    5964 
     65                // Disable error reporting, to prevent PHP warnings 
     66                $ER = error_reporting(0); 
     67 
     68                // Fetch the image size and mime type 
     69                $image_info = getimagesize($image); 
     70 
     71                // Turn on error reporting again 
     72                error_reporting($ER); 
     73 
     74                // Make sure that the image is readable and valid 
     75                if ( ! is_array($image_info) OR count($image_info) < 3) 
     76                        throw new Kohana_Exception('image.file_unreadable', $image); 
     77 
    6078                // Check to make sure the image type is allowed 
    61                 if (($type = exif_imagetype($image)) == FALSE OR ! isset(Image::$allowed_types[$type])) 
     79                if ( ! isset(Image::$allowed_types[$image_info[2]])) 
    6280                        throw new Kohana_Exception('image.type_not_allowed', $image); 
    6381 
    64                 $this->image = str_replace('\\', '/', realpath($image)); 
     82                // Image has been validated, load it 
     83                $this->image = array 
     84                ( 
     85                        'file' => str_replace('\\', '/', realpath($image)), 
     86                        'width' => $image_info[0], 
     87                        'height' => $image_info[1], 
     88                        'type' => $image_info[2], 
     89                        'ext' => Image::$allowed_types[$image_info[2]], 
     90                        'mime' => $image_info['mime'] 
     91                ); 
     92 
     93                // Load configuration 
     94                $this->config = (array) $config + Config::item('image'); 
    6595 
    6696                try 
     
    247277        { 
    248278                // If no new image is defined, use the current image 
    249                 empty($new_image) and $new_image = $this->image; 
     279                empty($new_image) and $new_image = $this->image['file']; 
    250280 
    251281                // Separate the directory and filename 
  • trunk/system/libraries/drivers/Image.php

    r1566 r1827  
    1010 
    1111        // Processing errors 
    12         protected $errors; 
     12        protected $errors = array(); 
    1313 
    1414        /** 
     
    2828                return TRUE; 
    2929        } 
     30 
     31        /** 
     32         * Sanitize and normalize a geometry array based on the temporary image 
     33         * width and height. Valid properties are: width, height, top, left. 
     34         * 
     35         * @param   array  geometry properties 
     36         * @return  void 
     37         */ 
     38        protected function sanitize_geometry( & $geometry) 
     39        { 
     40                list($width, $height) = $this->properties(); 
     41 
     42                // Turn off error reporting 
     43                $reporting = error_reporting(0); 
     44 
     45                // Width and height cannot exceed current image size 
     46                $geometry['width']  = min($geometry['width'], $width); 
     47                $geometry['height'] = min($geometry['height'], $height); 
     48 
     49                switch($geometry['top']) 
     50                { 
     51                        case 'center': 
     52                                $geometry['top'] = floor(($height / 2) - ($geometry['height'] / 2)); 
     53                        break; 
     54                        case 'top': 
     55                                $geometry['top'] = 0; 
     56                        break; 
     57                        case 'bottom': 
     58                                $geometry['top'] = $height - $geometry['height']; 
     59                        break; 
     60                } 
     61 
     62                switch($geometry['left']) 
     63                { 
     64                        case 'center': 
     65                                $geometry['left'] = floor(($width / 2) - ($geometry['width'] / 2)); 
     66                        break; 
     67                        case 'left': 
     68                                $geometry['left'] = 0; 
     69                        break; 
     70                        case 'right': 
     71                                $geometry['left'] = $width - $geometry['height']; 
     72                        break; 
     73                } 
     74 
     75                // Restore error reporting 
     76                error_reporting($reporting); 
     77        } 
     78 
     79        /** 
     80         * Return the current width and height of the temporary image. This is mainly 
     81         * needed for sanitizing the geometry. 
     82         * 
     83         * @return  array  width, height 
     84         */ 
     85        abstract protected function properties(); 
    3086 
    3187        /** 
  • trunk/system/libraries/drivers/Image_ImageMagick.php

    r1823 r1827  
    1111        // Temporary image filename 
    1212        protected $tmp_image; 
    13  
    14         // Processing errors 
    15         protected $errors = array(); 
    1613 
    1714        /** 
     
    3835                // Check to make sure the provided path is correct 
    3936                if ( ! file_exists(realpath($config['directory']).'/convert'.$this->ext)) 
    40                         throw new Kohana_Exception('image.imagemagick.not_found'); 
     37                        throw new Kohana_Exception('image.imagemagick.not_found', 'convert'.$this->ext); 
    4138 
    4239                // Set the installation directory 
     
    5047        public function process($image, $actions, $dir, $file) 
    5148        { 
     49                // We only need the filename 
     50                $image = $image['file']; 
     51 
    5252                // Unique temporary filename 
    5353                $this->tmp_image = $dir.'k2img--'.sha1($dir.$file).substr($file, strrpos($file, '.')); 
     
    173173        } 
    174174 
    175         /** 
    176          * Return the current width and height of the temporary image. This is mainly 
    177          * needed for sanitizing the geometry. 
    178          * 
    179          * @return  array  width, height 
    180          */ 
    181175        protected function properties() 
    182176        { 
    183                 // Return the width and height as an array. Use with list() 
    184177                return explode(',', exec(escapeshellcmd($this->dir.'identify'.$this->ext).' -format '.escapeshellarg('%w,%h').' '.$this->cmd_image)); 
    185178        } 
    186179 
    187         /** 
    188          * Sanitize and normalize a geometry array based on the temporary image 
    189          * width and height. Valid properties are: width, height, top, left. 
    190          * 
    191          * @param   array  geometry properties 
    192          * @return  void 
    193          */ 
    194         protected function sanitize_geometry( & $geometry) 
    195         { 
    196                 list($width, $height) = $this->properties(); 
    197  
    198                 // Turn off error reporting 
    199                 $reporting = error_reporting(0); 
    200  
    201                 // Width and height cannot exceed current image size 
    202                 $geometry['width']  = min($geometry['width'], $width); 
    203                 $geometry['height'] = min($geometry['height'], $height); 
    204  
    205                 switch($geometry['top']) 
    206                 { 
    207                         case 'center': 
    208                                 $geometry['top'] = floor(($height / 2) - ($geometry['height'] / 2)); 
    209                         break; 
    210                         case 'top': 
    211                                 $geometry['top'] = 0; 
    212                         break; 
    213                         case 'bottom': 
    214                                 $geometry['top'] = $height - $geometry['height']; 
    215                         break; 
    216                 } 
    217  
    218                 switch($geometry['left']) 
    219                 { 
    220                         case 'center': 
    221                                 $geometry['left'] = floor(($width / 2) - ($geometry['width'] / 2)); 
    222                         break; 
    223                         case 'left': 
    224                                 $geometry['left'] = 0; 
    225                         break; 
    226                         case 'right': 
    227                                 $geometry['left'] = $width - $geometry['height']; 
    228                         break; 
    229                 } 
    230  
    231                 // Restore error reporting 
    232                 error_reporting($reporting); 
    233         } 
    234  
    235180} // End Image ImageMagick Driver