Changeset 1815

Show
Ignore:
Timestamp:
01/25/08 16:22:37 (6 months ago)
Author:
armen
Message:

Fixing #337
You can validate a range of image size eg: 10x20-100x200
Added min_width and min_heigth into the i18n/en_US/validation.php

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/system/i18n/en_US/validation.php

    r1672 r1815  
    2727        'invalid_type'  => 'The %s file is not an allowed file type.', 
    2828        'max_size'      => 'The %s file you uploaded was too large. The maximum size allowed is %s.', 
    29         'max_width'     => 'The %s file has a maximum allowed width of %s is %spx.', 
    30         'max_height'    => 'The %s file has a maximum allowed image height of %s is %spx.', 
     29        'max_width'     => 'The %s file you uploaded was too big. The maximum allowed width is %spx.', 
     30        'max_height'    => 'The %s file you uploaded was too big, The maximum allowed height is %spx.', 
     31        'min_width'     => 'The %s file you uploaded was too big. The minimum allowed width is %spx.', 
     32        'min_height'    => 'The %s file you uploaded was too big, The minimum allowed height is %spx.', 
    3133 
    3234        // Field types 
  • trunk/system/libraries/Validation.php

    r1812 r1815  
    453453 
    454454                // Maximum sizes of various attributes 
    455                 $maxsize = array 
     455                $fileinfo = array 
    456456                ( 
    457457                        'file'   => FALSE, 
    458458                        'human'  => FALSE, 
    459                         'width'  => FALSE, 
    460                         'height' => FALSE 
     459                        'max_width'  => FALSE, 
     460                        'max_height' => FALSE 
    461461                ); 
    462462 
     
    505505                        foreach($params as $param) 
    506506                        { 
    507                                 if (preg_match('/[0-9]+x[0-9]+/', $param)) 
    508                                 { 
    509                                         // Maximum image size, eg: 200x100 
    510                                         list($maxsize['width'], $maxsize['height']) = explode('x', $param); 
     507                                if (preg_match('/[0-9]+x[0-9]+(-[0-9]+x[0-9]+)?/', $param)) 
     508                                { 
     509                                        // Image size, eg: 200x100 , 20x10-200x100 
     510                                        $param = (strpos($param, '-') === False) ? $param.'-'.$param : $param; 
     511 
     512                                        list($min, $max) = explode('-', $param); 
     513 
     514                                        list($fileinfo['max_width'], $fileinfo['max_height']) = explode('x', $max); 
     515                                        list($fileinfo['min_width'], $fileinfo['min_height']) = explode('x', $min); 
    511516                                } 
    512517                                elseif (preg_match('/[0-9]+[BKMG]/i', $param)) 
    513518                                { 
    514519                                        // Maximum file size, eg: 1M 
    515                                         $maxsize['human'] = strtoupper($param); 
     520                                        $fileinfo['human'] = strtoupper($param); 
    516521 
    517522                                        switch(strtoupper(substr($param, -1))) 
     
    523528                                        } 
    524529 
    525                                         $maxsize['file'] = $param; 
     530                                        $fileinfo['file'] = $param; 
    526531                                } 
    527532                                else 
     
    560565                        // Upload to large, based on php.ini settings 
    561566                        case UPLOAD_ERR_INI_SIZE: 
    562                                 if ($maxsize['human'] == FALSE) 
    563                                 { 
    564                                         $maxsize['human'] = ini_get('upload_max_filesize'); 
    565                                 } 
    566                                 $this->add_error('max_size', $this->current_field, $maxsize['human']); 
     567                                if ($fileinfo['human'] == FALSE) 
     568                                { 
     569                                        $fileinfo['human'] = ini_get('upload_max_filesize'); 
     570                                } 
     571                                $this->add_error('max_size', $this->current_field, $fileinfo['human']); 
    567572                                return FALSE; 
    568573                        break; 
     
    595600                        return FALSE;         
    596601 
    597                 if ($maxsize['file'] AND $data['size'] > $maxsize['file']) 
    598                 { 
    599                         $this->add_error('max_size', $this->current_field, $maxsize['human']); 
     602                if ($fileinfo['file'] AND $data['size'] > $fileinfo['file']) 
     603                { 
     604                        $this->add_error('max_size', $this->current_field, $fileinfo['human']); 
    600605                        return FALSE; 
    601606                } 
     
    611616 
    612617                        // Validate height and width 
    613                         if ($maxsize['width'] AND $mime[0] > $maxsize['width']) 
    614                         { 
    615                                 $this->add_error('max_width', $this->current_field, $maxsize['width']); 
     618                        if ($fileinfo['max_width'] AND $mime[0] > $fileinfo['max_width']) 
     619                        { 
     620                                $this->add_error('max_width', $this->current_field, $fileinfo['max_width']); 
    616621                                return FALSE; 
    617622                        } 
    618                         elseif ($maxsize['height'] AND $mime[1] > $maxsize['height']) 
    619                         { 
    620                                 $this->add_error('max_height', $this->current_field, $maxsize['height']); 
     623                        elseif ($fileinfo['min_width'] AND $mime[0] < $fileinfo['min_width']) 
     624                        { 
     625                                $this->add_error('min_width', $this->current_field, $fileinfo['min_width']); 
     626                                return FALSE; 
     627                        } 
     628                        elseif ($fileinfo['max_height'] AND $mime[1] > $fileinfo['max_height']) 
     629                        { 
     630                                $this->add_error('max_height', $this->current_field, $fileinfo['max_height']); 
     631                                return FALSE; 
     632                        } 
     633                        elseif ($fileinfo['min_height'] AND $mime[1] < $fileinfo['min_height']) 
     634                        { 
     635                                $this->add_error('min_height', $this->current_field, $fileinfo['min_height']); 
    621636                                return FALSE; 
    622637                        }