Development

Changeset 10949

You must first sign up to be able to contribute.

Changeset 10949

Show
Ignore:
Timestamp:
08/19/08 16:40:48 (5 years ago)
Author:
andersapt
Message:

Enabled validation of exact width/height as well as aspect ratio.

Rewrote and removed the for loop, since the validator will be running per form field anyway, no need to loop, and it was hardcoded to a form field named 'picture' (which was not the case for me).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfExtendedFileValidatorPlugin/trunk/lib/validator/sfExtendedFileValidator.class.php

    r10917 r10949  
    2525      return false; 
    2626    } 
    27     else 
     27     
     28    /* 
     29     * $imgData[0] retrieve width 
     30     * $imgData[1] retrieve height 
     31     */ 
     32    $imgData = @getimagesize($value['tmp_name']); 
     33     
     34    $min_width = $this->getParameter('min_width'); 
     35 
     36    if ($min_width !== null && $min_width > $imgData[0]) 
    2837    { 
    29       $imgs = $this->context->getRequest()->getFileNames(); 
     38      $error = $this->getParameter('min_width_error'); 
    3039 
    31       foreach ($imgs as $img) 
    32       { 
    33         $img = $this->context->getRequest()->getFilePath($img); 
     40      return false; 
     41    } 
    3442 
    35         /* 
    36          * $imgData[0] retrieve width 
    37          * $imgData[1] retrieve height 
    38          */ 
    39         $imgData = @getimagesize($img['picture']); 
     43    $max_width = $this->getParameter('max_width'); 
     44    if ($max_width !== null && $max_width < $imgData[0]) 
     45    { 
     46      $error = $this->getParameter('max_width_error'); 
    4047 
    41         $min_width = $this->getParameter('min_width'); 
     48      return false; 
     49    } 
    4250 
    43         if ($min_width !== null && $min_width > $imgData[0]) 
    44         { 
    45           $error = $this->getParameter('min_width_error'); 
     51    $min_height = $this->getParameter('min_height'); 
    4652 
    47           return false; 
    48         } 
     53    if ($min_height !== null && $min_height > $imgData[1]) 
     54    { 
     55      $error = $this->getParameter('min_height_error'); 
    4956 
    50         $max_width = $this->getParameter('max_width'); 
    51         if ($max_width !== null && $max_width < $imgData[0]) 
    52         { 
    53           $error = $this->getParameter('max_width_error'); 
     57      return false; 
     58    } 
    5459 
    55           return false; 
    56         } 
     60    $max_height = $this->getParameter('max_height'); 
    5761 
    58         $min_height = $this->getParameter('min_height'); 
     62    if ($max_height !== null && $max_height < $imgData[1]) 
     63    { 
     64      $error = $this->getParameter('max_height_error'); 
    5965 
    60         if ($min_height !== null && $min_height > $imgData[1]) 
    61         { 
    62           $error = $this->getParameter('min_height_error'); 
     66      return false; 
     67    } 
     68     
     69    $exact_width = $this->getParameter('exact_width'); 
    6370 
    64           return false; 
    65         } 
     71    if ($exact_width !== null && $exact_width != $imgData[0]) 
     72    { 
     73      $error = $this->getParameter('exact_width_error'); 
    6674 
    67         $max_height = $this->getParameter('max_height'); 
     75      return false; 
     76    } 
    6877 
    69         if ($max_height !== null && $max_height < $imgData[1]) 
    70         { 
    71           $error = $this->getParameter('max_height_error'); 
     78    $exact_height = $this->getParameter('exact_height'); 
    7279 
    73           return false; 
    74         } 
    75       } 
     80    if ($exact_height !== null && $exact_height != $imgData[1]) 
     81    { 
     82      $error = $this->getParameter('exact_height_error'); 
    7683 
    77       return true; 
     84      return false; 
    7885    } 
     86 
     87    $aspect = $this->getParameter('aspect'); 
     88 
     89    if ($aspect !== null && ( is_float($aspect) || is_int($aspect) ) && $aspect != $imgData[0]/$imgData[1]) 
     90    { 
     91      $error = $this->getParameter('aspect_error'); 
     92 
     93      return false; 
     94    } 
     95     
     96     // If we want to resave jpegs to noninterlaced (unfortunately this cannot be checked), resave the image using imageinterlace(0) and jpeg quality 90 
     97     // Todo: Check if the image is noninterlaced before resaving 
     98    if ( $imgData[2]==IMAGETYPE_JPEG && $this->getParameter('resave_to_noninterlaced') ) 
     99    { 
     100      $temp_resource = imagecreatefromjpeg($value['tmp_name']); 
     101      imageinterlace($temp_resource,0); 
     102      imagejpeg($temp_resource,$value['tmp_name'],90); 
     103      imagedestroy($temp_resource); 
     104    } 
     105     
     106    return true; 
    79107  } 
    80108 
     
    91119    $this->setParameter('max_height', null); 
    92120    $this->setParameter('max_height_error', 'File has a height too big'); 
    93  
     121    $this->setParameter('exact_width', null); 
     122    $this->setParameter('exact_width_error', 'File has an incorrect width'); 
     123    $this->setParameter('exact_height', null); 
     124    $this->setParameter('exact_height_error', 'File has an incorrect height'); 
     125    $this->setParameter('aspect', null); 
     126    $this->setParameter('aspect_error', 'File has an incorrect aspect ratio'); 
     127    $this->setParameter('resave_to_noninterlaced',false); 
     128     
    94129    $this->getParameterHolder()->add($parameters); 
    95130