Hello, I’m Julian

Validate size of a uploaded Image in Symfony 1.3/1.4


The sfValidatorFile has no options to check the size of a image in pixel. For that I have written a simple validator that extends the sfValidatorFile-class.

Create the file jsValidatorImage.php in your /lib directory:

    class jsValidatorImage extends sfValidatorFile
    {
      protected function configure($options = array(), $messages = array())
      {
        parent::configure($options, $messages);

        $this->addOption('minx', false);
        $this->addOption('miny', false);
        $this->addOption('maxx', false);
        $this->addOption('maxy', false);

        $this->addMessage('not_a_image', 'The file is not a image.');
        $this->addMessage('minx', 'Das Bild muss mindestens %minx% Pixel breit sein.');
        $this->addMessage('maxx', 'Das Bild darf maximal %maxx% Pixel breit sein.');
        $this->addMessage('miny', 'Das Bild muss mindestens %miny% Pixel hoch sein.');
        $this->addMessage('maxy', 'Das Bild darf maximal %maxy% Pixel hoch sein.');
      }

      protected function doClean($value)
      {
        $validatedFile = parent::doClean($value);
        // check image size
        $imagedata=getimagesize($value['tmp_name']);
        if (!$imagedata){
          throw new sfValidatorError($this, 'not_a_image');
        }
        if ($this->hasOption('minx') && $this->getOption('minx')!==false && $imagedata[0] < $this->getOption('minx'))
        {
          throw new sfValidatorError($this, 'minx', array('minx' => $this->getOption('minx'), 'size' => (int) $imagedata[0]));
        }
        if ($this->hasOption('maxx') && $this->getOption('maxx')!==false && $imagedata[0] > $this->getOption('maxx'))
        {
          throw new sfValidatorError($this, 'maxx', array('maxx' => $this->getOption('maxx'), 'size' => (int) $imagedata[0]));
        }
        if ($this->hasOption('miny') && $this->getOption('miny')!==false && $imagedata[1] < $this->getOption('miny'))
        {
          throw new sfValidatorError($this, 'miny', array('miny' => $this->getOption('miny'), 'size' => (int) $imagedata[1]));
        }
        if ($this->hasOption('maxy') && $this->getOption('maxy')!==false && $imagedata[1] > $this->getOption('maxy'))
        {
          throw new sfValidatorError($this, 'maxy', array('maxy' => $this->getOption('maxy'), 'size' => (int) $imagedata[1]));
        }
        return $validatedFile;
      }
    }

In your [model]Form.class.php extend the "configure()"-function with something like this:

    public function configure()
    {
      ...
      $this->widgetSchema['img'] = new sfWidgetFormInputFileEditable(array(
        ...
      ));        
      $this->validatorSchema['img'] = new jsValidatorImage(array(
        'required'        => true, 
        'path'            => sfConfig::get('sf_upload_dir').'/images',              
        'mime_categories' => 'web_images',
        'minx'            => 100,
        'maxx'            => 120,
        'miny'            => 80,
        'maxy'            => 90

      ));
      ...
    }

The validator will now throw a error if the image-width is not between 100 and 120 pixel or the image-height is not between 80 and 90 pixel

Date: Thu, 24 Jun 2010 13:41:00
Author: Julian Stricker

PREVIOUS NEXT

Blog Comments powered by Disqus.