Search code examples
imagesymfony1

In Symfony, where to resize an image uploaded while sfValidatorFile that automatically save?


In symfony 1.4, what is the best way to resize an image uploaded with a form when using sfWidgetFormInputFileEditable and sfValidatorFile and knowing that they already saved image without being asked ?

Where to put the code? I am a bit lost ...

Or should I use sfValidatorFile otherwise?

// In my form configure method:
$this->validatorSchema['image'] = new sfValidatorFile(array(
  'path'       => sfConfig::get('sf_upload_dir').'/images/',
  'mime_types' => 'web_images',
));

Thanks


Solution

  • A good place to do this would be overriding processValues in the form.

    class FooForm extends BaseFooForm {
    
      public function processValues($values)
        if ($values['image'] instanceof sfValidatedFile) { // file was uploaded
          $temp = $values['image']->getTempName();
          // do image manipulation here using GD or other
          $values['image'] = $pathToSavedResizedFile;
        }
        return parent::processValues($values);
      }
    
    }
    

    I illustrated the same technique in an older blogpost:

    http://www.gerryvandermaesen.com/posts/beyond-symfony-forms-custom-file-uploads