Image validator.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 May 2012 00:31:11 +0000 (02:31 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 May 2012 00:31:11 +0000 (02:31 +0200)
Max and min sizes for the picture which is
going to be downloaded on the telephone.

lib/form/doctrine/AdForm.class.php
lib/validator/sfValidatorFileImage.class.php [new file with mode: 0644]

index 6ad7420..fbd5226 100644 (file)
@@ -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 (file)
index 0000000..497a981
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * sfValidatorFileImage
+ *
+ * Original validator for symfony 1.0 : http://snippets.symfony-project.org/snippet/259
+ *
+ * @package    symfony
+ * @subpackage validator
+ * @author     Yoann Brieux <yoann |dot¤ brieux #at] gmail ~dot} com>
+ * @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;
+  }
+}
+