From: Gustavo Martin Morcuende Date: Wed, 30 May 2012 00:31:11 +0000 (+0200) Subject: Image validator. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=32dfd0ab04eb6a58bbcf4e06f955e652b3b8e9cc;p=mobi%2F.git Image validator. Max and min sizes for the picture which is going to be downloaded on the telephone. --- diff --git a/lib/form/doctrine/AdForm.class.php b/lib/form/doctrine/AdForm.class.php index 6ad7420..fbd5226 100644 --- a/lib/form/doctrine/AdForm.class.php +++ b/lib/form/doctrine/AdForm.class.php @@ -36,9 +36,16 @@ class AdForm extends BaseAdForm 'with_delete' => false)); - $this->validatorSchema['ad_mobile_image_link'] = new sfValidatorFile(array('mime_types' => 'web_images', - 'path' => sfConfig::get('app_default_picture_directory'), - 'required' => true)); + $this->validatorSchema['ad_mobile_image_link'] = new sfValidatorFileImage(array('mime_types' => 'web_images', + 'path' => sfConfig::get('app_default_picture_directory'), + 'required' => $this->isNew(), + 'is_only_image' => true, + 'max_height' => 200, + 'min_height' => 128, + 'max_width' => 200, + 'min_width' => 128, + 'mime_types' => array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif','application/x-shockwave-flash'))); + $this->widgetSchema->setLabels(array('company_categ_id' => 'Company Category')); diff --git a/lib/validator/sfValidatorFileImage.class.php b/lib/validator/sfValidatorFileImage.class.php new file mode 100644 index 0000000..497a981 --- /dev/null +++ b/lib/validator/sfValidatorFileImage.class.php @@ -0,0 +1,77 @@ + + * @version 0.1 + */ +class sfValidatorFileImage extends sfValidatorFile +{ + /** + * @param array $options An array of options + * @param array $messages An array of error messages + * + * @see sfValidatorFile + */ + protected function configure($options = array(), $messages = array()) + { + parent::configure($options, $messages); + + $this->addMessage('invalid_image', '%value% is an incorrect image file.'); + $this->addMessage('max_height', '"%value%" height is too long (%max_height% pixels max).'); + $this->addMessage('min_height', '"%value%" height is too short (%min_height% pixels min).'); + $this->addMessage('max_width', '"%value%" width is too long (%max_width% pixels max).'); + $this->addMessage('min_width', '"%value%" width is too short (%min_width% pixels min).'); + + $this->addOption('max_height'); + $this->addOption('min_height'); + $this->addOption('max_width'); + $this->addOption('min_width'); + $this->addOption('is_only_image',false); + } + + /** + * @see sfValidatorFile + */ + protected function doClean($value) + { + $clean = parent::doClean($value); + + $size = @getimagesize($clean->getTempName()); + + if (!$size && !$this->getOption('is_only_image')) + { + return $clean; + } + + if (!$size){ + throw new sfValidatorError($this, 'invalid_image', array('value' => $value['name'])); + } + + list($width, $height) = $size; + + if($this->getOption('max_height') < $height){ + throw new sfValidatorError($this, 'max_height', array('value' => $value['name'], 'max_height' => $this->getOption('max_height'))); + } + + if($this->getOption('min_height') > $height){ + throw new sfValidatorError($this, 'min_height', array('value' => $value['name'], 'min_height' => $this->getOption('min_height'))); + } + + if($this->getOption('max_width') < $width){ + throw new sfValidatorError($this, 'max_width', array('value' => $value['name'], 'max_width' => $this->getOption('max_width'))); + } + + if($this->getOption('min_width') > $width){ + throw new sfValidatorError($this, 'min_width', array('value' => $value['name'], 'min_width' => $this->getOption('min_width'))); + } + + return $clean; + } +} +